' -----[ Title ]-----------------------------------------------------------
'
' File...... STEPDEMO.BAS
' Purpose... Stepper Motor Control Demo
' Author.... Jon Williams, CIS 
' Started... 28 November 1994
' Updated... 30 November 1994


' -----[ Program Description ]---------------------------------------------
'
' Provides basic stepper motor control using lookup tables stored in the
' EEPROM.  
'
' The StepX routines use a clever technique for updating cStep.  The mod-
' ulus operator (//) is used to keep cStep in the range of zero to three 
' (seven for half steps).  These tables demonstrate how it works: 
'
'       Forward                 Reverse (full)
'       --------------          --------------
'       0 + 1 // 4 = 1          0 + 3 // 4 = 3
'       1 + 1 // 4 = 2          3 + 3 // 4 = 2
'       2 + 1 // 4 = 3          2 + 3 // 4 = 1
'       3 + 1 // 4 = 0          1 + 3 // 4 = 0
' 
'                                              
' Connections:                                
' -------------------------------------------     N __    __ E
' Pin0 -> ULN2003.1, ULN2003.16 -> Coil 1 (N)         )  (    
' Pin1 -> ULN2003.2, ULN2003.15 -> Coil 2 (S)     + __)  (__ +
' Pin2 -> ULN2003.3, ULN2003.14 -> Coil 3 (E)         )  (
' Pin3 -> ULN2003.4, ULN2003.13 -> Coil 4 (W)     S __)  (__ W
'
'
' Step Sequences:
' --------------------------------------
' Full steps: NE, SE, SW, NW
' Half steps: N, NE, E, SE, S, SW, W, NW


' -----[ Revision History ]------------------------------------------------
'
' 11-28-1994 : Version 1.0
' 11-30-1994 : Added half-step table/routine


' -----[ Constants ]-------------------------------------------------------
'
SYMBOL  CW      = 1                     ' turn clockwise
SYMBOL  CCW     = 7                     ' turn counter-clockwise


' -----[ Variables ]-------------------------------------------------------
'
SYMBOL  cStep   = B0                    ' current step
SYMBOL  sDir    = B1                    ' step direction (CW or CCW)
SYMBOL  nSteps  = W1                    ' number of steps to execute   
SYMBOL  index   = W2                    ' loop counter
SYMBOL  table   = B6                    ' table address pointer
SYMBOL  speed   = W4                    ' speed control; bigger = slower


' -----[ Initialization ]--------------------------------------------------
'
.' step tables (coils = %WESN)
.'
Setup:  EEPROM (%0101, %0110, %1010, %1001)     ' full: NE, SE, SW, NW
.EEPROM (%0001, %0101, %0100, %0110)     ' half: N, NE, E, SE
.EEPROM (%0010, %1010, %1000, %1001)     ' half: S, SW, W, NW

.Dirs = %00001111                ' Pins 0 - 3 are outputs
.Pins = %00000000                ' clear the outputs


' -----[ Main Code ]-------------------------------------------------------
'
Main:   sDir = CW                       ' turn clockwise
.nSteps = 48                     ' one revolution @ 7.5/full step
.speed = 20                      ' about one rev/second (0.96s)
.GOSUB Step1                     ' execute full steps
.PAUSE 1000                      ' wait a second...
.sDir = CCW                      ' switch to counter clockwise
.GOSUB Step1                     ' go in reverse
.PAUSE 1000

.sDir = CW
.nSteps = 96                     ' one revolution in half-steps
.speed = 10                      ' maintain rotational speed
.GOSUB Step2                     ' turn in half-steps
.PAUSE 1000
.sDir = CCW
.GOSUB Step2
.PAUSE 1000

.GOTO Main                       ' do it all again


' -----[ Subroutines ]-----------------------------------------------------
'

' full steps
'
Step1:  sDir = sDir MAX 3               ' adjust for smaller table
.FOR index = 1 TO nSteps
.  cStep = cStep + sDir // 4     ' update current step pointer
.  READ cStep, Pins              ' output coil data to pins
.  PAUSE speed                   ' delay between steps
.NEXT index
.RETURN


' half steps
'
Step2:.FOR index = 1 TO nSteps
.  cStep = cStep + sDir // 8     ' update current step pointer
.  table = cStep + 4             ' get table address
.  READ table, Pins              ' output coil data to pins
.  PAUSE speed                   ' delay between steps
.NEXT index
.RETURN
