Shop OBEX P1 Docs P2 Docs Learn Events
Stepper motor code - why code like this? — Parallax Forums

Stepper motor code - why code like this?

JimGJimG Posts: 84
edited 2006-11-06 06:35 in BASIC Stamp
Below is a piece of sample code that is available from Parallax to work with their stepper motor.· I am not criticising their code but wonder why they go through all the gyrations with modulus to get the values 0, 1, 2, or 3 to determine which phase of the motor to run.· I seems to me that you could set constants for each phase, i.e. Phase0 CON 0, and be done with it.· Why do they go through all of the other work...other than maybe just to show a way of doing it demonstrating features of PBasic?· Can anyone help?

Note: I am just strating to fiddle (technical term) with steppers and need to make things a simple as possible my poor small brain!

' {$STAMP BS2}
' {$PBASIC 2.5}
Phase VAR OUTB ' phase control outputs
StpsPerRev CON 48 ' whole steps per rev
idx VAR Byte ' loop counter
stpIdx VAR Nib ' step pointer
stpDelay VAR Byte ' delay for speed control
Steps DATA %0011, %0110, %1100, %1001
Setup:
DIRB = %1111 ' make P4..P7 outputs
stpDelay = 15 ' set step delay
Main:
· FOR idx = 1 TO StpsPerRev ' one revolution
··· GOSUB Step_Fwd ' rotate clockwise
· NEXT
· PAUSE 500 ' wait 1/2 second
· FOR idx = 1 TO StpsPerRev ' one revolution
··· GOSUB Step_Rev ' rotate counter-clockwise
· NEXT
· PAUSE 500 ' wait 1/2 second
· GOTO Main
END
Step_Fwd:
· stpIdx = stpIdx + 1 // 4 ' point to next step
· DEBUG "Stepping forward..."
· DEBUG ? stpidx
· GOTO Do_Step
Step_Rev:
· stpIdx = stpIdx + 3 // 4 ' point to previous step
· GOTO Do_Step
Do_Step:
· READ (Steps + stpIdx), Phase ' output new phase data
· PAUSE stpDelay ' pause between steps
· RETURN

Jim

Comments

  • FranklinFranklin Posts: 4,747
    edited 2006-11-04 17:49
    The reason for the gyrations is so the count will go 0, 1, 2, 3, 0, 1....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • JimGJimG Posts: 84
    edited 2006-11-04 19:08
    Stephen,

    I see how it outputs but don't understand why you couln't drop in 4 lines of code with the phases addressed and be done with it?

    Jim
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-11-04 20:09
    Hi Jim, there is always more than one way to write a program as you probably know, the best programs are usually the ones that are easy to follow compact and flexible. The example you chose has all three attributes. Two For Next loops three subroutines all with only one line of code which is pretty compact, I thought it was simple to follow through and understand what was going on, the stepidx step pointer really is an excellent way to keep track and with one simple adjustment to the StpsPerRev you can alter the degree of movement quiet simply. If you are able to write and improve on the code you see it can be very satisfying, and this is the place to share your achievements.

    take care

    Jeff T.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-11-05 09:16
    Jim -

    The program supplied by Parallax is quite a universal program, and not just written for one particular stepper motor. The key to this, expressed by Jeff, is repeated here for emphasis:

    " ... with one simple adjustment to the StpsPerRev you can alter the degree of movement quite simply."

    Not every stepper has the same number of steps per revolution. Thus, this becomes a rather universal program in that respect. Could it be simpler - sure, but it might well lose some of its universality and flexability.

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 11/5/2006 9:31:18 AM GMT
  • JimGJimG Posts: 84
    edited 2006-11-05 16:43
    Jeff/Bruce,

    I completely agree on the us of that variable. I code that way myself. It is the whole modulus activity that I am having trouble understanding. Well, not exactly understanding, I know how it works, it just seems like a lot of steps to get where we need to be. I haven't had time to see if I can come up with a simpler way of doing it so I need to shut up until I either have a simler way or accept what it being done as the best way.

    Thanks,

    Jim
  • metron9metron9 Posts: 1,100
    edited 2006-11-06 06:35
    Here is a routine from one of my stepper projects. It uses a bit rotate to move the stepper motor. Moving forward or backwards is done by rotating bits in a byte. The Byte is then output to the port pins to move the stepper.

    That's the way I do it in assembler for another chip I use.

    zerostepper: ;move stepper all the way counter clockwise
    ldi stepcount, 0xbe
    steploop1:
     ldi steppulse, 0x08        'initialize byte
     out portb, steppulse      'OUTPUT TO PORT
     rcall steppause             'pause
    
     lsr steppulse                'rotate bits left
     out portb, steppulse
     rcall steppause
    
     lsr steppulse
     out portb, steppulse
     rcall steppause
    
     lsr steppulse
     out portb, steppulse
     rcall steppause
    
     dec stepcount
    brne steploop1
    ldi cursteppos, 0x00
    ret ;----------------------------
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
Sign In or Register to comment.