Shop OBEX P1 Docs P2 Docs Learn Events
Program Efficency — Parallax Forums

Program Efficency

LightfootLightfoot Posts: 228
edited 2006-05-29 06:36 in General Discussion
The following program generates pulses on the RB.0 pin. The circuit uses a mosfet transistor to amplify the pulses for electric motor control. For speed adjustment, it takes a value on the RA port and converts it into pulse widths. It supports ten speeds including a full off (zero). Is their a way to shorten it?

DEVICE          SX18, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000

tris_b = 0 'Set RB port to outputs
plp_a = 0 'Enable pullup resistors for RA port
lowdur var byte 'Stores the length of time the signal is low.
ctr var byte 'Incrementing variable for loops.

Main:

if ra = 15 then 'The default value is 15 hence the pullups are enabled.
    rb.0 = 0 'Signal is completely low.
    goto Main 'Infinate loop.
endif
if ra = 0 then 'Zero represents the same value as 15.
    rb.0 = 0 'Signal is completly low.
    goto Main 'Infinate loop.
endif
if ra = 10 then 'Ten represents motor running at full speed.
    rb.0 = 1 'Signal is completely high.
    goto Main 'Infinate loop
endif

lowdur = 10 - ra 'Calculate the time the signal is low.

rb.0 = 1 'Enable RB.0 which is the PWM pin.

for ctr = 0 to ra 'Loop number of times pulse is high.
    pauseus 100 'Wait 100 microseconds.
next ctr

rb.0 = 0 'Disable RB.0

for ctr = 0 to lowdur 'Loop number of times pulse is low.
    pauseus 100 'Wait 100 microseconds.
next ctr

goto Main 'Infinate loop




Thank you [noparse]:)[/noparse]

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Well well, I'm seeing things, three of them.

-Stanley Blystone

Comments

  • Clock LoopClock Loop Posts: 2,069
    edited 2006-05-29 02:52
    Instead of using multiple IF value = X then

    You can use lookup or lookdown, and also you can use branch..

    Not too sure if lookup lookdown and branch would make your ASSEMBLY code faster (shorter) but you can try.

    Also, if you want to speed up your program, you need to think about how you can make the program without using the PAUSE statement. PAUSEUS allows smaller pauses...

    Also you can use COUNTING to do things that require a pause, instead of an actual pause statement.

    Like for instance...

    I can say
    Main:
    
    HIGH led
    PAUSE 50...
    LOW led
    
    GOTO Main
    
    



    Or I can do this
    Main:
    
        If nopause = 50 then
             HIGH led
             nopause = 0
        Else 
             LOW led
             nopause = nopause + 1
        Endif
    
    GOTO Main
    
    



    The second example actually allows the entire program to loop constantly with no pausing at all. Thus you can do other things that might require you to not be pausing. You would need to tweak the correct max count to get your desired timing, and this timing could be thrown off a bit if you have other statements that allow different routes through your program.. It really depends on how EXACT you need your timing to be.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.

    Post Edited (BPM) : 5/29/2006 3:01:42 AM GMT
  • LightfootLightfoot Posts: 228
    edited 2006-05-29 04:54
    I was thinking maybe eliminating the IF statements for starters.

    Thanks again [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Well well, I'm seeing things, three of them.

    -Stanley Blystone
  • Clock LoopClock Loop Posts: 2,069
    edited 2006-05-29 06:36
    Three of Them said...
    I was thinking maybe eliminating the IF statements for starters.

    Thanks again [noparse]:)[/noparse]

    In that case, use lookdown and lookup.

    Use the sxb help and look into lookdown and lookup, But because you don't really have alot of if then statements, i am not sure which code would be faster.

    With lookdown and lookup you will be doing most of your code in subroutines.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.
Sign In or Register to comment.