'Demonstrates the use of a stepper motor and SN754410 motor controller func main() initstepper(100,10) 'The main routine mainloop: 'The following commands will move the motor in various directions. 'with a 200 step motor it will always pause in the same location. stepper(1,50) stepper(0,100) stepper(1,200) stepper(0,150) goto mainloop endfunc '-------------------------------------------------------- 'Sets up variables for stepper function ' '-------------------------------------------------------- func initstepper(tdirdelay,tspeed) 'Demonstrates the use of a stepper motor and SN754410 motor controller gconst M1A 0 gconst M1B 1 gconst M2A 2 gconst M2B 3 gconst ME 4 output M1A output M1B output M2A output M2B output ME 'This turns on the motor controller high ME 'This is a dealy used when the motor reverses. It keeps the motor from loosing steps 'at high speed when reversing direction because of inertia. Set it to 1 id you are 'using the motor to drive a bot global dirdelay as integer dirdelay = tdirdelay 'Phase is used by the program to keep track of where it is global phz as integer phz=0 'Step speed. The higher the number the slower the movement. global speed as integer speed = tspeed endfunc '------------------------------------------------------ 'move stepper motor a number of steps 'dir: 0=bwd 1=fwd 'ct: number of steps '----------------------------------------------------- func stepper(dir,ct) if dir = 1 then while ct <> 0 'Step the motor forward gosub phasefwd wend pause dirdelay exit 0 else while ct <> 0 'Step the motor backward gosub phasebkwd wend pause dirdelay exit 0 endif 'The phase commands cycle through phase steps forward or backwards phasefwd: phz = phz + 1 if phz <= 4 then goto phasefwdcont phz=1 phasefwdcont: gosub dobranch ct = ct -1 return phasebkwd: phz = phz - 1 if phz > 1000 then goto itsless if phz >=1 and phz < 255 then goto phasebkwdcont itsless: phz = 4 phasebkwdcont: gosub dobranch ct = ct -1 return 'The following routines do the actual stepping dobranch: pause speed branch phz,dostep0,dostep1,dostep2,dostep3,dostep4 return dostep0: return dostep1: high M1A '-v low M1B low M2A '+v high M2B return dostep2: low M1A '+v high M1B low M2A '+v high M2B return dostep3: low M1A '+v high M1B high M2A '-v low M2B return dostep4: high M1A '-v low M1B high M2A '-v low M2B return endfunc