The schematic for the SN754410 with connections to the PIC18F248 is given in the Schematics Section below. The ancillary circuitry for the PIC18F248 also includes connection via RS232 to a PC (enables PC control of motor speed and direction). For initial testing, a SPST momentary button (with some switch debounce circuitry) is used to signal individual steps to the stepper motor.
Power Supply
A typical "wall-wart" power-supply is used (a surplus laptop charger in this case) in conjunction with a voltage regulator (LM317T) to provide the regulated 5V required by the PIC microcontroller. Since the stepper motor was only being controlled for test purposes (i.e. check circuit connections and software code) a seperate motor voltage source was not used (which would have allowed higher currents/voltages producing more torque etc if required by the particular application).
Circuit Operation
The internal workings/operation of a stepper motor are detailed in numerous references, the Microchip application note is a good starting point if required (1). The minimum understanding required is that a stepper motor has various windings that need to be energised in a set pattern to produce a varying magnetic field that interacts with the rotor shaft to produce movement in discrete steps. The direction the current is feed into the stepper motor windings is important, as magnetic fields of opposite direction need to be produced in sequence. Therefore, circuitry to allow current to flow in either direction across the stepper motor windings as required is necessary. This is generally a H-bridge arrangement. Again, the operation of a H-bridge circuit is detailed in numerous references, for example here (4). The SN754410 contains four half H-bridge drivers, which means a single SN754410 can control a bipolar stepper motor.
Figure 1 below, based on (5), summarises the connection of the PIC microcontroller to the SN754410 H-bridge driver and in turn to the bipolar stepper motor. Four control signals (the SN754410 also has enable pins that can be used to enable/disable pairs of H-bridge drivers, if this was desired, an additional two control pins from the PIC would be required) from the PIC microcontroller control which outputs of the SN754410 will be energised. The outputs from the SN754410 then determine the current flow and direction within the stepper motor windings. This arrangement allows logic level signals (5V and a few mA) to control appropriate currents/voltage to the stepper motor. Sending the appropriate pattern of control signals from the PIC microcontroller, the stepper motor can be rotated in the desired direction/speed and or held at a particular position.
There are three possible "appropriate patterns" (ignoring microstepping as such) with which the PIC microcontroller can control the stepper motor via the SN754410. These are called wave drive, full step (or two phase) drive and half-step drive which are summarised on the following diagram (based on (6)). Wave drive has the lowest torque but could have utility if only very low power was required/available. Full step drive is the normal sequence for a bipolar motor and has the highest torque. Whereas, half-step has the benefit of giving a better effective step angle, but with the disadvantage of uneven torque. Stepper motor direction is simply reversed by sending the sequences in reverse order. Stepper motor speed is determined by the frequency with which the sequence is clocked to the SN754410.
Software/Firmware
The required firmware is relatively straight forward. An array is defined to store the bit patterns corresponding to the various sequences required by the type of drive being used (wave, full-step or half-step). The value (i.e. bit pattern) from an element in the array is placed into one of the output ports of the PIC microcontroller that is physically connected to the SN754410. This instructs the SN754410 to energise the stepper motor coils appropriately.
Therefore, in order to make the stepper motor move, the value from the elements in the array just need to be placed onto the output port in succession. Reversing the order in which the values are output reverses the direction of the stepper motor. Increasing and or decreasing the frequency with which the values are output controls the rotation speed.
One item of note is the use of the modulus (%) function e.g, "currentStep=(currentStep+1) % 4;" in the code. This is just a method for conveniently ensuring that the array "loops around" to the start, and therefore produces a continuous sequential pattern for driving the stepper motor. The "index variable" (the variable "currentStep" in the code fragment) stores the current position within the drive stepping pattern array. A seperate variable could be used in conjunction with this to record the current angular position (in this particular case, each step of the stepper motor was 0.9o).
The remainder of the code in the code fragment below deals with "button debouncing" and not involved per se with stepper motor control. A SPST momentary contact button was used as a convenient/easy method for signalling when a step of the stepper motor should be performed.
#include <spectro.h>
#zero_ram //all variables automatically initialised to 0
#define LEDred PIN_A0 //visually show PIC operating
int8 mode_button; //for button debouncing
void main() {
int16 debounceCount;
//comment out motorStep[] definition as appropriate
int motorSteps[4] = {0x0A,0x02,0x04,0x01}; //wave drive
//int motorSteps[4] = {0x0A,0x06,0x05,0x09}; //full step drive
//int motorSteps[8] = {0x0A,0x02,0x06,0x04,0x05,0x01,0x09,0x08}; //half-step drive
int currentStep;
output_high(LEDred); //turn on Red LED to indicate PIC program running
output_c(motorSteps[currentStep]); //energise stepper motor to 1st step position
delay_ms(1000); //wait for circuit to settle (avoids problems with debounce)
while(TRUE){
if (debounceCount == 0){
if (mode_button) {// button has been previously pressed on
if (!input_state(PIN_B1)) {
mode_button = 0; //clear previous pressed on state
}
} else {
if (input_state(PIN_B1)) { //check if PIN_B1 pressed on
mode_button = 1 ; //1=down
}
}
}
if (mode_button) {
delay_ms(100); //give time for stepper motor to complete the step
//currentStep=(currentStep+1) % 8; //for half-step drive
currentStep=(currentStep+1) % 4; //for wave and full-step drive
output_c(motorSteps[currentStep]);
}
}
}
Only Logged-In Members can add comments