'{$STAMP BS2} ' ============================================================================== ' File...... Ex26 - Stepper.BS2 ' Purpose... Stepper Motor Control ' Author.... Parallax ' E-mail.... stamptech@parallaxinc.com ' Started... ' Updated... 01 MAY 2002 ' {$STAMP BS2} ' ============================================================================== ' ------------------------------------------------------------------------------ ' Program Description ' ------------------------------------------------------------------------------ ' This program demonstrates unipolar stepper motor control. The pot allows the ' program to control speed and direction of the motor. ' ------------------------------------------------------------------------------ ' Revision History ' This version revised by Perry Hoberman 11/03 for CTIN534 class to ' use only one side (instead of both) of a 10K potentiometer for control ' ------------------------------------------------------------------------------ ' ------------------------------------------------------------------------------ ' I/O Definitions ' ------------------------------------------------------------------------------ PotCW CON 0 'clockwise pot input Coils VAR OutB 'output to stepper coils ' ------------------------------------------------------------------------------ ' Variables ' ------------------------------------------------------------------------------ speed VAR Word ' delay between steps x VAR Byte ' loop counter sAddr VAR Byte ' EE address of step data rcLf VAR Word ' rc reading - left diff VAR Word ' difference between readings ' ------------------------------------------------------------------------------ ' EEPROM Data ' ------------------------------------------------------------------------------ ' ABAB ' ----- Step1 DATA %1100 'A on B on A\ off B\ off Step2 DATA %0110 'A off B on A\ on B\ off Step3 DATA %0011 'A off B off A\ on B\ on Step4 DATA %1001 'A on B off A\ off B\ on ' ------------------------------------------------------------------------------ ' Initialization ' ------------------------------------------------------------------------------ Initialize: DirB = %1111 'make stepper pins outputs speed = 5 'set starting speed sAddr = 0 ' ------------------------------------------------------------------------------ ' Program Code ' ------------------------------------------------------------------------------ Main: FOR x = 1 TO 48 ' 1 rev forward GOSUB Step_Fwd NEXT PAUSE 200 FOR x = 1 TO 48 ' 1 rev back GOSUB Step_Rev NEXT PAUSE 200 Step_Demo: HIGH PotCW PAUSE 1 RCTIME PotCW, 1, rcLf ' read potentiometer rcLf = rcLf MIN 1 MAX 460 if rcLf > 200 and rcLf < 260 then Step_Demo 'range 201-259 = dead zone if rcLf > 260 then Step_CCW Step_CW: speed = (rcLf ) + 5 min 5 max 200 'pot range 1 - 200 = CW motion debug "cw ",dec speed,cr GOSUB Step_Fwd ' do a step GOTO Step_Demo Step_CCW: speed = 200 - ((rcLf - 260) ) + 5 min 5 max 200 'pot range 260 - 460 = CCW motion debug "ccw ",dec speed, cr GOSUB Step_Rev GOTO Step_Demo ' ------------------------------------------------------------------------------ ' Subroutines ' ------------------------------------------------------------------------------ Step_Fwd: sAddr = sAddr + 1 // 4 ' point to next step READ (Step1 + sAddr), Coils ' output step data PAUSE speed ' pause between steps RETURN Step_Rev: sAddr = sAddr + 3 // 4 ' point to previous step READ (Step1 + sAddr), Coils PAUSE speed RETURN