;############################################################### ; ; StepCW ; ; Moves one step Clockwise, then reapplies hold power ; ; Uses Finite state machine to determine which combination is next ; ; State Number Coil 2 Direction Coil 1 Direction ; ; 3 F F ; 1 B F ; 0 B B ; 2 F B ; ; to turn clockwise, move down the table ; to turn counterclockwise, move up the table ; ;############################################################### Declare Function stepcw() Select Case stepstate Case 0 SetMotor Coil2,"F",100 SetMotor Coil1,"B",100 stepstate = 2 Wait stepdelay Case 1 SetMotor Coil2,"B",100 SetMotor Coil1,"B",100 stepstate = 0 Wait stepdelay Case 2 SetMotor Coil2,"F",100 SetMotor Coil1,"F",100 stepstate = 3 Wait stepdelay Case 3 SetMotor Coil2,"B",100 SetMotor Coil1,"F",100 stepstate = 1 Wait stepdelay End Select End Function ;############################################################### ; ; StepCCW ; ; Moves one step CounterClockwise, then reapplies hold power ; ; Uses Finite state machine to determine which combination is next ; see StepCW for State table ;############################################################### Declare Function stepccw() Select Case stepstate Case 0 SetMotor Coil2,"B",100 SetMotor Coil1,"F",100 stepstate = 1 Wait stepdelay Case 1 SetMotor Coil2,"F",100 SetMotor Coil1,"F",100 stepstate = 3 Wait stepdelay Case 2 SetMotor Coil1,"B",100 SetMotor Coil2,"B",100 stepstate = 0 Wait stepdelay Case 3 SetMotor Coil1,"B",100 SetMotor Coil2,"F",100 stepstate = 2 Wait stepdelay End Select End Function ;############################################################### ; ; StepMotor ; ; Moves a number of steps, in one direction. ; Accelerates and decelerates symetrically. ; ;############################################################### Declare Function stepmotor(direction As Byte, steps As Word) Declare Word stepcount Declare Word decelzone decelzone = steps - decelband stepcount = 0 Do If direction = "F" Then Call stepcw() Else Call stepccw() End If If stepcount < decelband Then timetowait = decelband - stepcount Else If stepcount > decelzone Then timetowait = stepcount - decelzone Else timetowait = 0 End If End If If timetowait <> 0 Then Wait timetowait End If Increment stepcount stepdisplay = LowByte(stepcount) DisplayNumber stepdisplay If stepdisplay = 0 Then stepcount = stepcount - 256 End If Until stepcount = steps ; now put on correct holding current Select Case stepstate Case 0 SetMotor Coil2,"B",holdpower SetMotor Coil1,"B",holdpower Case 1 SetMotor Coil2,"B",holdpower SetMotor Coil1,"F",holdpower Case 2 SetMotor Coil2,"B",holdpower SetMotor Coil1,"F",holdpower Case 3 SetMotor Coil1,"F",holdpower SetMotor Coil2,"F",holdpower End Select End Function ;############################################################### ; ; Main ; ;############################################################### Declare Word timetowait Declare Byte stepstate Declare Byte stepdisplay Declare Byte holdpower = 10 ; percentage of full power required to hold motor in position Declare Constant decelband = 4 Declare Constant stepdelay = 1 ; this is to allow time for the magnetic field to build ; up in the coils ; might need to increase, depending on voltage supplied Declare Constant Coil1 = 3 ; which two motor ports are being used? Declare Constant Coil2 = 4 Begin Program ; put stepper motor into known state stepstate = 0 SetMotor Coil1,"B",100 SetMotor Coil2,"B",100 Wait stepdelay SetMotor Coil1,"B",holdpower SetMotor Coil2,"B",holdpower ; this is just testing code, change to achieve your own requirements. Loop Call stepmotor("F",2) Call stepmotor("F",4) Call stepmotor("F",10) Call stepmotor("F",16) Call stepmotor("F",10) Call stepmotor("F",4) Call stepmotor("F",2) Wait 200 Call stepmotor("F",48) Wait 200 Call stepmotor("B",96) Wait 200 Call stepmotor("F",192) Wait 200 Call stepmotor("B",96) Wait 200 Call stepmotor("F",48) Wait 200 End Loop End Program