Interfacing with a UCN5084 to Control of a Stepping Motor

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


Introduction

The UCN5804B from Allegro Micro is a stepping motor controller which provides the translation circuitry to generate the stepping motor patterns for full step either one-phase or two-phase) and half step operation. It includes drivers capable of sinking 1.5A, integrated transient protection diodes and thermal shut down circuitry.

We have the UCN5804B for $4.00.

In the following routines, the 5804 is strapped for operation in a half step mode (Half Step high and One-Phase low). Thus, the motor may be easily interfaced with a BasicX by simply controlling the Direction and Step Inputs. The motor advances in the defined direction 1/2 step for each clock pulse on the Step input.

Note that an output enable is provided which permits a number of 5804s to share the same Direction and Step leads. For example, an 74X138 might be used to select one of eight 5804 devices. That is, select the 5804 using three BasicX outputs and then turn the addressed motor using the common Direction and Step leads.

In program S5804_1.Bas, the BasicX continually reads three switches; Go_Sw, Dir_Sw and Fast_Sw. If Go_Sw is at a logic one, the motor is turned in the specified direction at the specified speed.

In program S5804_2.Bas, subroutines are used to turn the motor in a specified direction at a specified RPM a specified number of turns.


' S5804_1.Bas (BasicX-1)
'
' In this routine, the 5804 is strapped for half step operation and the
' interface between the BasicX and the 5804 is simply Dir and Clk.  The
' stepper advances one half step in the defined direction with each pulse
' on Clk.
'
' Input switch Go_Sw controls whether the motor turns or stays at rest.
' The Dir_Sw controls direction and Fast_Sw controls the speed.
'
' Compile with SerialPort.Bas
'
' copyright, Stacy Anderson, Baltimore, MD, Oct, '99

Const Clk as Byte = 21        	' Clk is the same as Step 
				' outputs on PortC
Const Dir as Byte = 22

Const Go_Sw as Byte = 39 	' input switches on PortA
Const Dir_Sw as Byte = 38       
Const Fast_Sw as Byte = 37

Sub Main ()

   Dim str as String
   Dim T as Single
  
   Call OpenSerialPort(2, 9600)	' for debugging
 
   Do
      Call PutPin(Dir_Sw, 3)	' make inputs with pullup resistors
      Call PutPin (Fast_Sw, 3)
      Call PutPin (Go_Sw, 3)
    
      If (GetPin(Go_Sw) = 1) then	' if go then determine speed
         If (GetPin(Fast_Sw)= 1) then	' set the appropriate delay time
           T = 0.01			' 100 pulses per second
         Else 
           T = 0.05	' 20 pulses per second
         End If

         If (GetPin(Dir_Sw) = 1) then	' and direction
            Call PutPin (Dir, 1)
         Else
            Call PutPin (Dir, 0)
         End If 

         str = "."			' used for debugging
         Call Putstr(str)

         Call PutPin (Clk, 1)		' send clock pulse
         Call PutPin (Clk, 0)
         Call Sleep (T)
      End If
   Loop 
End Sub


' S5804_2.Bas 
'
' Illustrates a subroutine to turn a stepping motor in a specified 
' direction, at a specified speed, a specified number of turns.
'
' Note that the motor we used was 7.5 degrees per full step or 48 full
' steps (or 96 half steps) per revolution.
'
' copyright, Peter H. Anderson, Baltimore, MD, Oct, '99

   
   Const CLK5804 as Byte = 21	' PortC
   Const DIR5804 as Byte = 22

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

Sub Main()

   Call OpenSerialPort(2, 9600)	' For debugging
	' CW, 60 RPM, 2 revolutions
   Call TurnStepper(CW, 60.0, 2.0)
   Call Sleep(2.0)
        ' CCW, 30 RPM, 5.25 revolutions
   Call TurnStepper(CCW, 30.0, 5.25)
   Call Sleep(2.0)

End Sub

Sub TurnStepper(ByVal Direction as Byte, ByVal RPM as Single, _
                ByVal NumTurns as Single)

   Const HalfStepsPerRev as Single = 96.0	
                  ' assumes 7.5 degs per step

   Dim NumHalfSteps as Single, t as Single

   NumHalfSteps = NumTurns * HalfStepsPerRev
   ' Call PutS(NumHalfSteps)	' for debugging
   ' Call NewLine()
   t = 1.0 / (RPM / 60.0 * HalfStepsPerRev)
   ' Call PutS(t)
   ' Call NewLine()
   
   If (Direction = CW) Then
      Call PutPin(DIR5804, 1)
   Else
      Call PutPin(DIR5804, 0)
   End If
  
   Do
      
      Call PutPin(CLK5804, 1)
      Call PutPin(CLK5804, 0)
      Call Sleep(t)
      
      NumHalfSteps = NumHalfSteps - 1.0

   Loop Until (NumHalfSteps <= 0.0)

End Sub