Hi,
There is no inherent feedback on a step-motor so without some kind of reference switch (or position encoder etc) it's not possible to know where "home" is.
Here's a somewhat expanded version of the code. It's a bit more "modular" than earlier, you can select drive-type and just set the direction and number of steps before calling the Drive subroutine. I've not tested it here (no hardware) so you're on your own there if you decide to use it.
As you might see, I've added a FindHome routined which moves one step at a time until it finds the switch. Just remove it if you're not going to use it.
Code:
'****************************************************************
'* Name : StepIndexer.pbp *
'* Author : Henrik Olsson *
'* Notice : Copyright (c) 2011 Henrik Olsson *
'* : All Rights Reserved *
'* Date : 2011-09-07 *
'* Version : 1.0 *
'* Notes : Very simple step motor indexer for unipolar drive.*
'* : *
'****************************************************************
Direction VAR BYTE ' Forward=1, Back=254
Distance VAR WORD ' Number of steps to move
DriveType VAR BYTE ' WaveDrive=0, HiTorque=1, HalfStep=2
StepCount VAR WORD ' Counts the number of steps moved
Delay VAR BYTE ' Delay between steps
HomeSwitch VAR PORTA.0 ' Reference/home switch connected here
Index VAR BYTE ' Index "pointer" used for the lookup tables.
Temp var BYTE ' General purpose
FORWARD CON 1
BACK CON 254
WaveDrive CON 0
HiTorque CON 1
HalfStep CON 2
TRISB = 0 ' All PortB pins are outputs.
TRISA.0 = 1 ' Reference/home switch connected to RA0
pause 1000
CLEAR
DriveType = HiTorque ' Use the two phase on high torque mode
'---------------------------------------------------------------------------
FindHome:
' Take one step at a time untill the reference switch connected
' to PortA.0 is found, stop there.
Direction = FORWARD ' Move forward
Distance = 1 ' step by step
Delay = 20 ' at roughly 50Hz
While HomeSwitch = 0
GOSUB Drive
Wend
'---------------------------------------------------------------------------
Pause 2500
Cycle:
Direction = FORWARD ' Go forward...
Distance = 512 ' for 512 steps...
Delay = 20 ' at roughly 50Hz...
Gosub Drive ' do it
Pause 2500
Direction = BACK ' Go back...
Distance = 512 ' for 512 steps...
Delay = 10 ' at roughly 100Hz...
GOSUB DRIVE ' do it.
Pause 2500
Goto Cycle
'---------------------------------------------------------------------------
Drive:
' Move Distance number of steps. Direction gets added to Index each iteration,
' this makes Index count up (when direction=FORWARD (1)) or down
' (when direction=BACK (254)). Then, depending on the desired drive type we
' either mask the lower two or lower three bits in temp and use those as the
' actual pointer into the Lookup-tables for the three different drive sequences
' (Wave drive, Hi-Torque and Half step). The result of the table lookup is put
' on PortB.
For StepCount = 1 to Distance
Index = Index + DIRECTION
Select Case DriveType
Case WaveDrive
Temp = Index & 3
Lookup Temp, [1, 2, 4, 8], PortB
Case HiTorque
Temp = Index & 3
Lookup Temp, [3, 6, 12, 9], PortB
Case HalfStep
Temp = Index & 7
Lookup Temp, [1, 3, 2, 6, 4, 12, 8, 9], PortB
END SELECT
Pause Delay
NEXT
Return
'---------------------------------------------------------------------------
/Henrik.
Bookmarks