'
' Hardware Assignments
' --------------------
StepA var PORTA.0
StepB var PORTA.1
StepC var PORTA.2
StepD var PORTA.3
'
' RAM Assignments and Variables
' -----------------------------
CounterA var word
' General Counter
StepDelay var word
' Inter-Step Delay (uS)
StepReverse var bit
' 0=Drive Forward
' 1=Drive Reverse
StepOut var byte
' a Variable reflecting Stepper Channel Outputs
StepOnLine var bit
' 0=Stepper Off-Line (de-energised)
' 1=Stepper On-Line (energised)
StepMode var Byte
' 0=Single Drive A-B-C-D
' 1=Two Phase Drive AB-BC-CD-DA
' 2=Half-Step A-AB-B-BC-C-CD-D-DA
StepStatus var byte
' Step Status (a value between 0 and 7)
' used internally by the StepDrive routine.
' Other than presetting at Power-On... do not
' otherwise adjust this variable, as it keeps
' track of the current coil setting.
'
' Single Step One Phase has FOUR possibilities
' 0=A
' 2=B
' 4=C
' 6=D
' Single Step Two Phase has FOUR possibilities
' 1=AB
' 3=BC
' 5=CD
' 7=DE
' Half-Step has EIGHT Possibles
' those eight possibilities are the two single
' steps interwoven.
' 0=A
' 1=AB
' 2=B
' 3=BC
' 4=C
' 5=CD
' 6=D
' 7=DA
'
' Start Program
' =============
goto JumpStart
'
' Subroutine Nest Area
' ====================
'
' Subroutine Drives Stepper
' -------------------------
' From wherever the current step position happens to be
' this routine will drive Forwards or Backwards. If the
' Stepper happens to be parked in an illegal step for
' the current Mode, then it will ensure that the Stepper
' increments a one-half step first... this ensures that
' if the stepper is in position AB, and needs to increment
' TWO steps forwards in Mode 0, it increments AB-B-C
' rather than AB-C-D.
'
StepDrive:
'
' this section determines what the NEW StepStatus
' should be, depending on the Mode and Direction
' of step.
'
If StepReverse=0 then
If StepMode=0 then
Lookup StepStatus,[2,2,4,4,6,6,0,0],StepStatus
endif
If StepMode=1 then
Lookup StepStatus,[1,3,3,5,5,7,7,1],StepStatus
endif
If StepMode=2 then
Lookup StepStatus,[1,2,3,4,5,6,7,0],StepStatus
endif
else
If StepMode=0 then
Lookup StepStatus,[6,0,0,2,2,4,4,6],StepStatus
endif
If StepMode=1 then
Lookup StepStatus,[7,7,1,1,3,3,5,5],StepStatus
endif
If StepMode=2 then
Lookup StepStatus,[7,0,1,2,3,4,5,6],StepStatus
endif
endif
StepDriveRAW:
'
' All Stepper Coils Disabled
' --------------------------
Low StepA
Low StepB
Low StepC
Low StepD
If StepOnLine=1 then
'
' Energise Coils to New Settings
' ------------------------------
' This Lookup table represents [A,AB,B,BC,C,CD,D,DA]
Lookup StepStatus,[1,3,2,6,4,12,8,9],StepOut
StepA=StepOut.0
StepB=StepOut.1
StepC=StepOut.2
StepD=StepOut.3
endif
Return
'
' Initialise Hardware
' -------------------
JumpStart:
TRISB=%11110000
StepOnLine=0 ' Power-Off Stepper
Gosub StepDriveRAW
Pause 1000 ' Wait for PSU's and Hardware to settle
StepStatus=0 ' Preset initial Start Point
StepMode=0 ' Standard Phase Mode 0
StepReverse=0 ' Standard Forward Movement
StepOnLine=1 ' bring Stepper On-Line
Gosub StepDriveRAW
'
' Application Example
' -------------------
' Gentle Ramp Up Stepper to 200rpm
' Run for about 3 Seconds at 200rpm
' Fast Ramp Down Stepper to Zero
' Pause 5 seconds and repeat Reversing Direction
' once this has been done, the Stepper Mode will
' change and the proceedure loops over again.
' Once all three Stepper Modes have been run, the
' program loops and starts over from Mode 0.
' ------------------------------------------
' 200 step motor at 200 rpm=40000 steps/minute
' this is approx 666 steps (3.33 revs) per second
' Assuming NO program latenticity that's 1 step every 1.5mS
' The program will actually add some delay, probably around
' 100uS but for now, lets assume this is ZERO.
' --------------------------------------------
' This entire example is dependant on the mass and inertia
' of your stepper and it's load. The Delays may require
' serious consideration to make the Ramp-Up/down more
' applicable to the hardware and environment.
ExampleLoop:
'
' Slow Ramp-Up Stepper
' --------------------
For CounterA=0 to 2000 step 4
Gosub StepDrive
StepDelay=65500-(CounterA*32)
PauseUS StepDelay
Next CounterA
'
' Run at constant Speed of 200 rpm (or so) for 3 Seconds
' ------------------------------------------------------
For CounterA=0 to 2000
Gosub StepDrive
PauseUS 1500
Next CounterA
'
' Fast Ramp-Down Stepper
' ----------------------
For CounterA=2000 to 0 step -20
Gosub StepDrive
StepDelay=65500-(CounterA*32)
PauseUS StepDelay
Next CounterA
'
' Change Direction/Mode
' ---------------------
If StepReverse=0 then
StepReverse=1
else
StepReverse=0
StepMode=StepMode+1
If StepMode>2 then StepMode=0
endif
'
' Wait 5 seconds
' --------------
Pause 5000
Goto ExampleLoop
End