Controlling Stepper Motor Using 8051 Microcontroller
Controlling Stepper Motor Using 8051 Microcontroller
Introduction:
Stepper motors consist of a permanent magnet rotating shaft, called the rotor, and electromagnets on the stationary portion that surrounds the motor, called the stator. Figure 1 illustrates one complete rotation of a stepper motor. At position 1, we can see that the rotor is beginning at the upper electromagnet, which is currently active (has voltage applied to it). To move the rotor clockwise (CW), the upper electromagnet is deactivated and the right electromagnet is activated, causing the rotor to move 90 degrees CW, aligning itself with the active magnet. This process is repeated in the same manner at the south and west electromagnets until we once again reach the starting position.
Figure 1 1
Controlling Stepper Motor Using 8051 Microcontroller
In the above example, we used a motor with a resolution of 90 degrees or demonstration purposes. In reality, this would not be a very practical motor for most applications. The average stepper motor's resolution -- the amount of degrees rotated per pulse -- is much higher than this. For example, a motor with a resolution of 5 degrees would move its rotor 5 degrees per step, thereby requiring 72 pulses (steps) to complete a full 360 degree rotation.
Figure 2 You may double the resolution of some motors by a process known as "half-stepping". Instead of switching the next electromagnet in the rotation on one at a time, with half stepping you turn on both electromagnets, causing an equal attraction between, thereby doubling the resolution. As you can see in Figure 2, in the first position only the upper electromagnet is active, and the rotor is drawn completely to it.
Figure 3 2
Controlling Stepper Motor Using 8051 Microcontroller
In position 2, both the top and right electromagnets are active, causing the rotor to position itself between the two active poles. Finally, in position 3, the top magnet is deactivated and the rotor is drawn all the way right. This process can then be repeated for the entire rotation. There are several types of stepper motors. 4-wire stepper motors contain only two electromagnets, however the operation is more complicated than those with three or four magnets, because the driving circuit must be able to reverse the current after each step. For our purposes, we will be using a 6-wire motor.
Figure 4 Unlike our example motors which rotated 90 degrees per step, real-world motors employ a series of mini-poles on the stator and rotor to increase resolution. Although this may seem to add more complexity to the process of driving the motors, the operation is identical to the simple 90 degree motor we used in our example. An example of a multi-pole motor can be seen in Figure 3. In position 1, the north pole of the rotor's permanent magnet is aligned with the south pole of the stator's electromagnet. Note that multiple positions are aligned at once. In position 2, the upper electromagnet is deactivated and the next one to its immediate left is activated, causing the rotor to rotate a precise amount of degrees. In this example, after eight steps the sequence repeats. The specific stepper motor we are using for our experiments (ST-02: 5VDC, 5 degrees per step) has 6 wires coming out of the casing. If we follow Figure 4, the electrical equivalent of the stepper motor, we can see that 3 wires go to each half of the coils, and that the coil windings are connected in pairs. This is true for all four-phase stepper motors. Controlling Stepper Motor Using 8051 Microcontroller 3
However, if you do not have an equivalent diagram for the motor you want to use, you can make a resistance chart to decipher the mystery connections. There is a 13 ohm reistance between the center-tap wire and each end lead, and 26 ohms between the two end leads. Wires originating from seperate coils are not connected, and therefore would not read on the ohm meter
Figure 5 Due to a inductive surge created when a coil is toggled, a standard 1N4001 diode is usually placed across each transistor as shown in the figure, providing a safe way of dispersing the reverse current without damaging the transistor. Sometimes called a snubbing diode. The TIP120 transistors do not need an external snubbing diode becasue they have a built in diode. So the diodes shown in the drawing are the internal diodes in the TIP120 transistors. 4
Controlling Stepper Motor Using 8051 Microcontroller
The simplest way to operate a stepper motor with a 8051 is with the full step pattern shown in Table 1. Each part of the sequence turns on only one transistor at a time, one after the other. After the sequence is completed, it repeats infinitly until power is removed. Q1 + Q2 + Table 1 I have made this first program as small as possible, simply to demonstrate how easy it is to control a stepper motor. Also note the use of high and low commands to control the output lines, rather than peek and poke routines. For our purposes, high and low are sufficent. Q3 + Q4 +
Listing1:
.First stepper motor controller program .Rotate motor at set speed forever symbol delay=BO delay = 25 Loop: high 0 pause delay low 0 high 1 pause delay low 1 high 2 5 .turn on Q1 .wait 25 ms .turn off Q1 .turn on Q2 .wait 25 ms .turn off Q2 .turn on Q3
Controlling Stepper Motor Using 8051 Microcontroller
Listing 2:
.Second stepper motor controller program .Responds to user input by changing speed or direction symbol delay=BO delay = 100 .use b0 as the delay variable .the delay will begin at 100 ms
Forward:
high 0 pause delay low 0 6 .turn on Q1 .wait for 100 ms .turn off Q1
Controlling Stepper Motor Using 8051 Microcontroller
high 1 pause delay low 1 high 2 pause delay low 2 high 3 pause delay low 3 goto check
.turn on Q2 .wait for 100 ms .turn off Q2 .turn on Q3 .wait for 100 ms .turn off Q3 .turn on Q4 .wait for 100 ms .turn off Q4 .check the ststus of switches
Reverse:
high3 pause delay low 3 high 2 pause delay low 2 high 1 pause delay low 1 high 0 pause delay low 0 goto check 7 . turn on Q4 . wait for 100 ms . turn off Q4 .turn on Q3 .wait for 100 ms .turn off Q3 .turn on Q2 .wait for 100 ms . turn off Q2 .turn on Q1 .wait for 100 ms .turn off Q1 . check the status of switches
Controlling Stepper Motor Using 8051 Microcontroller
check: if pin4=0 then timeup if pin5=0 then timedn if pin6=0 then halt if pin7=0 then reverse goto forward . if nothing pressed continue
timeup:
delay = delay+5 pause 50 if delay>250 then max if pin4=0 then timeup goto check . check switches again . increase delay by 5 sec. .pause for 50 ms
timedn:
delay= delay - 5 pause 50 if delay<20 then min if pin5=0 then timedn goto check halt: if pin6=0 then halt goto check max: delay= 245 goto check 8 . cap the delay at 245 ms . check switches again
Controlling Stepper Motor Using 8051 Microcontroller
min: delay= 25 goto check . limit the delay to 25 ms . go back and check switches
.End Listing 2
As you can see by looking at the schematic (Figure 6), the "switches" used in this circuit are pieces of jumper wire on a breadboard. The inputs (B0-B3) are kept high via Vcc through a 10K resistor. Theses switches are activated by grounding them via another piece of jumper wire.
Figure 6 A note on the previous two listings: the minimum delay used in the programs was 25 ms. Depending on the clock speed of your crystal, 25 ms may be either too fast or too slow for the rotor to move. If you are having problems getting these programs to work, this is a likely culprit (further troubleshooting information).
Half Stepping:
As previously stated, half-stepping doubles the resolution of the motor. In our case, we are using a motor with a 5 degree / per step resolution, requiring 72 steps to complete one rotation. By half stepping, we could double this to 2.5 degrees / pulse, requiring 144 steps to complete one rotation. Table 2 shows the step pattern. 10
Controlling Stepper Motor Using 8051 Microcontroller
Step 1 2 3 4 5 6 7 8
Q1 + + +
Q2 + + + Table 2
Q3 + + + -
Q4 + + +
There are two ways in which you could impliment half-stepping in a 8051. One way would be to drive it directly, using the two previous listings, but replacing the high/low commands with the pins that corrispond to each step. The other (and easier) way would be to use a UCN 5804 Stepper Motor IC in conjunction with the 8051.
11
Figure 7 Since the input of the UCN 5804 is CMOS and TTL compatable, we can connect the outputs from the 8051 directly into the UCN 5804. Two outputs are needed; one to control the step input (pin 11), and one to control the output enable (pin 15) which enables the stepper motor while high and disables the stepper motor while low. Pins 9 and 10 control the stepping method (half or full steps). Pin 14 controls the step direction. Both of these controls can be manipulated by the 8051, but it is easier to control them directly through the use of jumpers acting as switches.
12
Figure 8 All the program has to do, is output a pulse and set the output enable low. This can be accomplished with a very simple program such as the following:
Listing 3:
.Stepper motor control with a UCN 5804 symbol delay = B0 low 1 delay = 10 delay = delay*1000 loop: pulseout 0,delay goto loop .make a delay variable .set the output enable low . set a pulsewidth of 10 ms .turn delay into microscends .send pulsewidth of delay to UCN5805 .repeat forever
.End Listing 3
Notice that I added an extra step; taking the defined "delay" value and multiplying it by 1,000. This is necessary because the pulseout command requires a pulse width in micro-seconds, 13
Controlling Stepper Motor Using 8051 Microcontroller
not milli-seconds. You can make the code even smaller by removing lines 1, 3, and 4 and replacing "delay" in line 5 with a set number in microseconds. However, I prefer to the method shown in the listing, because it makes it easier to change the delay parameter in the more familiar milliseconds without having to convert it to microseconds.
14