Control of a Unipolar Stepper - PICAXE 18X

copyright, Peter H Anderson, Baltimore, MD, Jan, '04


A unipolar stepper is operated in the half step mode by energizing one winding and then two adjacent windings and then the second winding alone, etc as shown in the following table.

0	%0001		$01
1	%0011		$03
2	%0010		$02
3	%0110		$06
4	%0100		$04
5	%1100		$0C
6	%1000		$08
7	%1001		$09
0	etc

The direction of travel is determined by the order in which these patterns are output, Proceeding from state 0, state 1, etc to state 7 and then back to state 0 will turn the motor in one direction. However, if the patterns are output beginning at state 7 and successively outputting the patterns at state 6, 5, etc to 0 and then back to 7, the motor will turn the other way.

The speed of the travel is determined by the pause between aoutputting adjacent states.

In Program STEP_1.Bas, Outputs 0 - 3 (18X terminals 6 - 9) are used for controlling the stepper. Note that the PICAXE does not have the capability to directly interface with the stepper windings. A ULN2803 Octal Darlington Transistor, or four TIP41C NPN transistors or similar are typical interface devices. In running this test program, I used four LEDs.

Program Description

The StepIndex is intialized to 0. Input0 is read causing the program to branch to either CW or CCW.

For CW operation, the StepIndex is tested as to whether it is 0 and if so, it is set to 7. Otherwise, it is decremented. Thus, StepIndex advances from 0 to 7, 6, 5, 4, 3, 2, 1, 0, 7, etc. For CCW operation, the index is tested as to whether it is 7 and if so, the index is looped back to 0. Otherwise, it is incremented.

In subroutine OutStep, a lookup table is used to map the StepIndex into the corresponding StepPattern.

The current state of the outputs is read and the lower four bits are zeroed and the StepPattern is inserted in the lower four bit positions and this is then output.

	OUTS = OUTS & $f0 | StepPatt

This avoids modifying any of the high nibble outputs which may be in use for other applications..

Note that I decided to use the OUTS terminology in place of PINS;

	Symbol  OUTS = PINS
As an aside, I attempted to output using the POKE command;
    Poke $06, StepPatt
However, the PICAXE firmware appears to quickly return PORTB, associated with the outputs on the 18X, to its original state. The lesson; it appears the POKE command cannot be used to set outputs.

' STEP_1.Bas - PICAXE 18X
' Illustrates control of a uni-polar stepper motor.  Continually turns a stepper in a direction
' determined by the state on Input.0 (term 17).
' Pullup Resistor required on Input0.
' Uses 67 of 2048 bytes.
' copyright, Peter H Anderson, Baltimore, MD, Jan , '04

Symbol  StepPatt = B0
Symbol  StepIndex = B1

Symbol  OUTS = PINS

     StepIndex = 0

     If INPUT0 = 0 Then CW
     Goto CCW

     If StepIndex = 0 Then CW_1
     StepIndex = StepIndex - 1
     GoTo CW_2
     StepIndex = 7
     GoSub OutStep
     GoTo Top

     If StepIndex = 7 Then CCW_1
     StepIndex = StepIndex + 1
     GoTo CCW_2
     StepIndex = 0
     GoSub OutStep
     GoTo Top

     Lookup StepIndex, ($01, $03, $02, $06, $04, $0c, $08, $09), StepPatt
     OUTS = OUTS & $f0 | StepPatt
     Pause 250