Control of a UniPolar Stepping Motor - Part II

copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

This is an extension of Stepper_1.Bas

In Stepper_2.Bas, the notor turns on depression of a pushbutton on Pin 8. The processor reads Pins 5 and 6 (corresponding to bits 7 and 6 of PORTC) to determine the timing between outputting half steps. The processor then reads SW_DIR on Pin 7 to determine the direction. The motor in then advanced one half step in the specified direction with a specified time delay.

Note that unlike Stepper_1.Bas the Index of the current position in the pattern array is declared as being global.

The following code snippet is used to make the high nibble of PORTC inputs and the low nibble outputs. Note that the second statement inserts pullup resistors on the inputs.

      Register.DDRC = bx00001111 ' high nib are input, low nib outputs
      Register.PORTC = Register.PORTC OR bx11110000	
Note that the two switches on RC.7 and RC.6 are read by reading PORTC, and then shifting right by six bits such that the state of the two switches are in the two least significant bits of SWTime.
      SWTime = (Register.PINC \ 64) AND bx00000011
Thus, the SWTime is either, 0, 1, 2 or 3. Note that use of the Select Case construct to map these four states into a delay time.

There is a limitation on the speed of the motor as there is a considerable time overhead in reading the switches, making the appropriate decisions and the call to the turn motor routine on half step.

' Stepper_2.Bas
' If PB8 on Pin 8 is open, motor does not turn.  When depressed, motor
' turns in half step mode in the direction determined by SW_DIR (Pin 7) at
' speed determined by SW_SPEED1 and SW_SPEED2 (terms 5 and 6).
' copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const CW as Byte = 0
Const CCW as Byte = 1

Const PB8 as Byte = 8
Const SW_DIR as Byte = 7

Dim Index as Integer	' current postition in the stepping motor patt array

Sub Main()

   Dim Patts(1 to 8) as Byte
   Dim Dir as Byte, SwTime as Byte, TimeStep as Single

   Patts(1) = bx00000001	' define stepping motor patts
   Patts(2) = bx00000011
   Patts(3) = bx00000010
   Patts(4) = bx00000110
   Patts(5) = bx00000100
   Patts(6) = bx00001100
   Patts(7) = bx00001000
   Patts(8) = bx00001001

   Index = 1

      Register.DDRC = bx00001111	' high nib are input, low nib outputs
      Register.PORTC = Register.PORTC OR bx11110000	
                                        ' pullups on high nib

      Do				' loop until PB8 at zero
         Call PutPin(PB8, 3)
      Loop Until (GetPin(PB8) = 0)
      SWTime = (Register.PINC \ 64) AND bx00000011
					' two most significant bits
      Select Case (SwTime)		' determine the speed
         Case 0
               TimeStep = 0.05		' map SwTime into a delay
         Case 1
               TimeStep = 0.1
         Case 2
               TimeStep = 0.25
         Case Else
               TimeStep = 1.0
      End Select   

      Call PutPin(SW_DIR, 3)	' just to be sure pullup is in place
      Dir = GetPin(SW_DIR)               ' determine the direction	 

      Call TurnMotor(Patts, Dir, 1, TimeStep)	

End Sub

Sub TurnMotor(ByRef Patts() as Byte, ByVal Dir as Byte, _
              ByVal Steps as Integer, ByVal TimeStep as Single)

   Dim N as Integer

   For N = 1 To Steps
      Register.PORTC = (Register.PORTC AND bx11110000) OR Patts(Index)
      Call Delay(TimeStep)

      If (Dir = 0) Then
         Index = Index + 1
         If (Index > 8) Then
            Index = 1
         End If
         Index = Index - 1
         If (Index < 1) Then
            Index = 8
         End If
      End If         
End Sub