MODULE – 5
I/O Port Interfacing & Programming:
The 8051 has four parallel I/O ports:
P0, P1, P2, and P3
Each port is 8 bits wide
Used for input/output operations
Can drive LEDs, switches, displays, etc.
I/O Port Characteristics
Port Features
P0 Open-drain, requires external pull-up resistors for output
P1 Simple I/O port, no alternate functions
P2 Also used for higher address bus in external memory access
P3 Multi-function port (RxD, TxD, INT0, INT1, T0, T1, WR, RD)
I/O Programming in 8051 – Using C Language: The 8051 microcontroller has four 8-bit
parallel I/O ports (P0, P1, P2, P3), used for interfacing external devices like LEDs,
switches, buzzers, displays, and keypads. C language provides a structured and readable way
to program these ports.
Accessing Ports in C
In embedded C (e.g., with Keil μVision), the standard header file is:
#include <reg51.h>
This file contains definitions for SFRs (Special Function Registers), including the I/O ports:
P0, P1, P2, P3 → used to access the I/O ports.
Bit-Addressable I/O
You can access individual pins using sbit:
sbit LED = P1^0; // P1.0 named as LED
sbit SWITCH = P2^1; // P2.1 named as SWITCH
Port Programming Syntax
Operation Syntax Example
Write to port P1 = 0xFF;
Read from port x = P0;
Set specific bit `P2
Clear specific bit P2 &= ~0x01;
Toggle bit P3 ^= 0x08;
Common I/O Interfacing Examples
1. LED Interfacing: LEDs can be connected to any port pin.
sbit LED = P1^0;
void main() {
while(1) {
LED = 1; // LED ON
delay();
LED = 0; // LED OFF
delay();
}
}
2. Switch Interfacing
sbit SW = P2^0;
sbit LED = P1^1;
void main() {
while(1) {
if(SW == 1)
LED = 1;
else
LED = 0;
}
}
Delay Function (Software Delay)
void delay() {
int i, j;
for(i = 0; i < 100; i++)
for(j = 0; j < 1275; j++);
}
This is a simple loop-based delay and is not precise — used only for basic testing.
LCD interfacing
LCDs (Liquid Crystal Displays) are commonly used to display alphanumeric characters and
messages in embedded systems. The most popular is the 16×2 LCD, which displays 2 lines
of 16 characters each. LCDs can operate in 8-bit or 4-bit mode; 4-bit mode saves I/O pins.
In 4-bit mode, the 8051 microcontroller communicates with the 16×2 LCD using only four
data lines (D4–D7) along with three control lines: RS, RW, and E.
The data or command is split into two 4-bit nibbles, and each nibble is sent sequentially.
1. Initialization
Send a sequence of commands to prepare the LCD for 4-bit mode operation.
Example commands:
o 0x02: Set to 4-bit mode
o 0x28: 2-line display, 5×7 font
o 0x0C: Display ON, cursor OFF
o 0x06: Auto-increment cursor
o 0x01: Clear display
2. Sending a Command
To send a command (e.g., clear screen):
1. RS = 0 (select instruction register)
2. RW = 0 (write mode)
3. Send high nibble of command to D4–D7
4. Pulse E from high to low (to latch data)
5. Send low nibble
6. Pulse E again
3. Sending Data (Character)
To display a character (e.g., ‗A‘):
1. RS = 1 (select data register)
2. RW = 0 (write mode)
3. Send high nibble of ASCII for ‗A‘ (0x41)
4. Pulse E
5. Send low nibble
6. Pulse E
ASCII of ‗A‘ = 0x41 → High nibble = 0x4, Low nibble = 0x1
LCD now shows the character on screen.
Example: Displaying “HI”
Send characters ‗H‘ and ‗I‘ one by one using their ASCII values (0x48, 0x49)
For each:
o Set RS = 1
o Send high nibble → latch
o Send low nibble → latch
Enable (E) Pulse Timing
The Enable pin is crucial:
When data is ready on data lines and control lines are set,
A short high-to-low pulse on E (usually a few microseconds) tells LCD to accept
data.
Internal LCD Function
Internally, the LCD:
Stores characters in DDRAM (Display Data RAM)
Converts ASCII values to segment patterns
Updates the display at refresh rate
The various commands to the LCD is given below.
LCD pin descriptions
The function of each pin is given in Table 1.
VCC, VSS, and VEE : While VCC and VSS provide +5 V and ground, respectively, VEE is
used for controlling LCD contrast.
RS, register select : There are two very important registers inside the LCD. The RS pin is
used for their selection as follows. If RS = 0, the instruction command code register is
selected, allowing the user to send a command such as clear display or cursor at home. If RS
= 1, the data register is selected, allowing the user to send data to be displayed on the LCD.
R/W, read/write : R/W input allows the user to write information to the LCD or read
information from it. R/W = 1 when reading; R/W = 0 when writing.
E, enable : The enable pin is used by the LCD to latch information presented to its data pins.
When data is supplied to data pins, a high-to-low pulse must be applied to this pin in order
for the LCD to latch in the data present at the data pins. This pulse must be a minimum of
450 ns wide.
D0–D7 : The 8-bit data pins, D0–D7, are used to send information to the LCD or read the
contents of the LCD‘s internal registers. To display letters and numbers, we send ASCII
codes for the letters A–Z, a–z, and numbers 0–9 to these pins while making RS = 1. There are
also instruction command codes that can be sent to the LCD to clear the display or force the
cursor to the home position or blink the cursor. Table 2 lists the instruction command codes.
We also use RS = 0 to check the busy flag bit to see if the LCD is ready to receive
information. The busy flag is D7 and can be read when R/W = 1 and RS = 0, as follows: if
R/W = 1, RS = 0. When D7 = 1 (busy flag = 1), the LCD is busy taking care of internal
operations and will not accept any new information. When D7 = 0, the LCD is ready to
receive new information. (Note: It is recommended to check the busy flag before writing any
data to the LCD.)
Sending commands and data to LCDs with a time delay
To send any of the commands from Table 2 to the LCD, make pin RS = 0. For data, make RS
= 1. Then send a high-to-low pulse to the E pin to enable the internal latch of the LCD.
ORG 0H
MOV A, #38H ;init. LCD 2 lines,5x7 matrix
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A, #0EH ;display on, cursor on
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A, #01 ;clear LCD
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A, #06H ;shift cursor right
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A, #84H ;cursor at line 1,pos. 4
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A, #'N' ;display letter N
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A, #'O' ;display letter O
ACALL DATAWRT ;call display subroutine
AGAIN: SJMP AGAIN ;stay here
COMNWRT: ;send command to LCD
MOV P1, A ;copy reg A to port1
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
DATAWRT: ;write data to LCD
MOV P1,A ;copy reg A to port1
SETB P2.0 ;RS=1 for data
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
DELAY: MOV R3, #50 ;50 or higher for fast CPUs
HERE2: MOV R4, #255 ;R4=255
HERE: DJNZ R4, HERE ;stay until R4 becomes 0
DJNZ R3, HERE2
RET
END
Sending code or data to the
LCD with checking busy flag
The above code showed how to send commands to the LCD without checking the busy flag.
Notice that we must put a long delay between issuing data or commands to the LCD.
However, a much better way is to monitor the busy flag before issuing a command or data to
the LCD.
MOV A, #38H ;init. LCD 2 lines,5x7 matrix
ACALL COMMAND ;issue command
MOV A,#0EH ;LCD on, cursor on
ACALL COMMAND ;issue command
MOV A, #01H ;clear LCD command
ACALL COMMAND ;issue command
MOV A, #06H ;shift cursor right
ACALL COMMAND ;issue command
MOV A, #86H ;cursor: line 1, pos. 6
ACALL COMMAND ;command subroutine
MOV A, #'N' ;display letter N
ACALL DATA_DISPLAY
MOV A, #'O' ;display letter O
ACALL DATA_DISPLAY
HERE: SJMP HERE ;STAY HERE
COMMAND: ACALL READY ;is LCD ready?
MOV P1, A ;issue command code
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0 ,latch in
RET
DATA_DISPLAY:
ACALL READY ;is LCD ready?
MOV P1, A ;issue data
SETB P2.0 ;RS=1 for data
CLR P2.1 ;R/W=0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0, latch in
RET
READY: SETB P1.7 ;make P1.7 input port
CLR P2.0 ;RS=0 access command reg
SETB P2.1 ;R/W=1 read command reg read command reg and
check busy flag
BACK: CLR P2.2 ;E=0 for L-to-H pulse
ACALL DELAY ;give LCD some time
SETB P2.2 ;E=1 L-to-H pulse
To read the command register we make R/W = 1 and RS = 0, and a L-to-H pulse for the E pin
will provide us the command register. After reading the command register, if bit D7 (the busy
flag) is high, the LCD is busy and no information (command or data) should be issued to it.
Only when D7 = 0 can we send data or commands to the LCD. Notice in this method that no
time delays are used since we are checking the busy flag before issuing commands or data to
the LCD.
Write an 8051 C program to send letters „M,‟ „D,‟ and „E‟ to the LCD using delays.
Solution:
#include <reg51.h>
sfr ldata = 0x90; //P1=LCD data pins (Fig. 2)
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
void main()
{
lcdcmd(0x38);
MSDelay(250);
lcdcmd(0x0E);
MSDelay(250);
lcdcmd(0x01);
MSDelay(250);
lcdcmd(0x06);
MSDelay(250);
lcdcmd(0x86); //line 1, position 6
MSDelay(250);
lcddata('M');
MSDelay(250);
lcddata('D');
MSDelay(250);
lcddata('E');
}
void lcdcmd(unsigned char value)
{
ldata = value; // put the value on the pins
rs = 0;
rw = 0;
en = 1; // strobe the enable pin
MSDelay(1);
en = 0;
return;
}
void lcddata(unsigned char value)
{
ldata = value; // put the value on the pins
rs = 1;
rw = 0;
en = 1; // strobe the enable pin
MSDelay(1);
en = 0;
return;
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
DAC INTERFACING
The digital-to-analog converter (DAC) is a device widely used to convert digital pulses to
analog signals. The first criterion for judging a DAC is its resolution, which is a function of
the number of binary inputs. The common ones are 8, 10, and 12 bits. The number of data bit
inputs decides the resolution of the DAC since the number of analog output levels is equal to
2n, where n is the number of data bit inputs. Therefore, an 8-input DAC such as the
DAC0808 provides 256 discrete voltage (or current) levels of output. Similarly, the 12-bit
DAC provides 4096 discrete voltage levels. There are also 16-bit DACs, but they are more
expensive.
MC1408 DAC (or DAC0808)
In the MC1408 (DAC0808), the digital inputs are converted to current (Iout), and by
connecting a resistor to the Iout pin, we convert the result to voltage. The total current
provided by the Iout pin is a function of the binary numbers at the D0–D7 inputs of the
DAC0808 and the reference current (Iref), and is as follows:
Iout = Iref (D7/2 + D6/4 + D5/8 + D4/16 + D3/32 + D2/64 + D1/128 + D0/256)
Here, D0 is the LSB, D7 is the MSB for the inputs, and Iref is the input current that must be
applied to pin 14. The Iref current is generally set to 2 mA. Figure 18 shows the generation of
current reference (setting Iref = 2 mA) by using the standard 5 V power supply and 1 K and
1.5 K-ohm standard resistors. Some DACs also use the zener diode (LM336), which
overcomes any fluctuation associated with the power supply voltage.
Example: Assuming that R = 5K and Iref = 2 mA, calculate Vout for the following binary
inputs: (a) 10011001 binary (99H) (b) 11001000 (C8H).
Solution:
(a) Iout = 2 mA (153/256) = 1.195 mA and Vout = 1.195 mA × 5K = 5.975 V
(b) Iout = 2 mA (200/256) = 1.562 mA and Vout = 1.562 mA × 5K = 7.8125 V
Figure: 8051 Connection to DAC0808
Converting Iout to voltage in DAC0808
Ideally we connect the output pin Iout to a resistor, convert this current to voltage, and
monitor the output on the scope. In real life, however, this can cause inaccuracy since the
input resistance of the load where it is connected will also affect the output voltage. For this
reason, the Iref current output is isolated by connecting it to an op-amp such as the 741 with
Rf = 5 K ohms for the feedback resistor. Assuming that R = 5 K ohms by changing the binary
input, the output voltage changes.
Generating a sine wave
To generate a sine wave, we first need a table whose values represent the magnitude of the
sine of angles between 0 and 360 degrees. The values for the sine function vary from −1.0 to
+1.0 for 0- to 360-degree angles. Therefore, the table values are integer numbers representing
the voltage magnitude for the sine of theta. This method ensures that only integer numbers
are output to the DAC by the 8051 microcontroller. Table 7 shows the angles, the sine values,
the voltage magnitudes, and the integer values representing the voltage magnitude for each
angle (with 30-degree increments). To generate Table, we assumed the full-scale voltage of
10 V for DAC output (as designed in Figure). Full-scale output of the DAC is achieved when
all the data inputs of the DAC are high. Therefore, to achieve the full-scale 10 V output, we
use the following equation.
Vout = 5 V + (5 × sin θ)
To find the value sent to the DAC for various angles, we simply multiply the Vout voltage by
25.60 because there are 256 steps and full-scale Vout is 10 V.
Therefore, 256 steps/10 V = 25.6 steps per volt.
Verify the values given for the following angles: (a) 30° (b) 60°.
Solution:
(a) Vout = 5 V + (5 V × sin θ) = 5 V + 5 × sin 30° = 5 V + 5 × 0.5 = 7.5 V
DAC input values = 7.5 V × 25.6 = 192 (decimal)
(b) Vout = 5 V + (5 V × sin θ) = 5 V + 5 × sin 60° = 5 V + 5 × 0.866 = 9.33 V
DAC input values = 9.33 V × 25.6 = 238 (decimal)
Programming DAC in C
#include <reg51.h>
sfr DACDATA = P1;
void main()
{
unsigned char WAVEVALUE[12] = {128,192,238,255, 238,192,128,64, 17,0,17,64};
unsigned char x;
while(1)
{
for(x=0;x<12;x++)
{
DACDATA = WAVEVALUE[x];
}
}
}
ADC devices
Analog-to-digital converters are among the most widely used devices for data acquisition.
Digital computers use binary (discrete) values, but in the physical world everything is analog
(continuous). Temperature, pressure (wind or liquid), humidity, and velocity are a few
examples of physical quantities that we deal with every day. A physical quantity is converted
to electrical (voltage, current) signals using a device called a transducer. Transducers are also
referred to as sensors. Sensors for temperature, velocity, pressure, light, and many other
natural quantities produce an output that is voltage (or current). Therefore, we need an
analog-to-digital converter to translate the analog signals to digital numbers so that the
microcontroller can read and process them. An ADC has n-bit resolution where n can be 8,
10, 12, 16 or even 24 bits. The higher- resolution ADC provides a smaller step size, where
step size is the smallest change that can be discerned by an ADC. The ADC chips are either
parallel or serial. In parallel ADC, we have eight or more pins dedicated to bringing out the
binary data, but in serial ADC we have only one pin for data out.
ADC0804 chip
The ADC0804 IC is an 8-bit parallel ADC in the family of the ADC0800 series from
National Semiconductor (www.national.com). It is also available from many other
manufacturers. It works with +5 V and has a resolution of 8 bits. In the ADC0804, the
conversion time varies depending on the clocking signals applied to the CLK IN pin, but it
cannot be faster than 110 μs. The following is the ADC0804 pin description.
CS
Chip select is an active-low input used to activate the ADC0804 chip. To access the
ADC0804, this pin must be low.
RD (read)
This is an input signal and is active low. The ADC converts the analog input to its binary
equivalent and holds it in an internal register. RD is used to get the converted data out of the
ADC0804 chip. When CS = 0, if a high-to-low pulse is applied to the RD pin, the 8-bit digital
output shows up at the D0–D7 data pins. The RD pin is also referred to as output enable
(OE).
WR (write; a better name might be “start conversion”)
This is an active-low input used to inform the ADC0804 to start the conversion process. If CS
= 0 when WR makes a low-to-high transition, the ADC0804 starts converting the analog
input value of Vin to an 8-bit digital number. The amount of time it takes to convert varies
depending on the CLK IN and CLK R values explained below. When the data conversion is
complete, the INTR pin is forced low by the ADC0804.
CLK IN and CLK R
CLK IN is an input pin connected to an external clock source when an external clock is used
for timing. However, the 804 has an internal clock generator. To use the internal clock
generator (also called self-clocking) of the ADC0804, the CLK IN and CLK R pins are
connected to a capacitor and a resistor, as shown in Figure. In that case, the clock frequency
is determined by the equation.
f =1/1.1 RC
Typical values are R = 10K ohms and C = 150 pF. Substituting in the above equation, we get
f = 606 kHz. In that case, the conversion time is 110 μs.
INTR (interrupt)
This is an output pin and is active low. It is a normally high pin and when the conversion is
finished, it goes low to signal the CPU that the converted data is ready to be picked up. After
INTR goes low, we make CS = 0 and send a high-to-low pulse to the RD pin to get the data
out of the ADC0804 chip.
Vin (+) and Vin (−)
These are the differential analog inputs where Vin = Vin (+) − Vin (−). Often the Vin (−) pin
is connected to ground and the Vin (+) pin is used as the analog input to be converted to
digital.
VCC
This is the +5 V power supply. It is also used as a reference voltage when the Vref/2 input
(pin 9) is open (not connected). This is discussed next.
Vref /2
Pin 9 is an input voltage used for the reference voltage. If this pin is open (not connected), the
analog input voltage for the ADC0804 is in the range of 0 to 5 V (the same as the Vcc pin).
However, there are many applications where the analog input applied to Vin needs to be other
than the 0 to +5 V range. Vref /2 is used to implement analog input voltages other than 0 to 5
V. For example, if the analog input range needs to be 0 to 4 V, Vref/2 is connected to 2 V.
Table shows the Vin range for various Vref/2 inputs.
D0–D7
D0–D7 (D7 is the MSB) are the digital data output pins since ADC0804 is a parallel ADC
chip. These are tri-state buffered and the converted data is accessed only when CS = 0 and
RD is forced low.
To calculate the output voltage, use the following formula.
Dout = Vin / step size
Where,
Dout = digital data output (in decimal), Vin = analog input voltage,
and step size (resolution) is the smallest change, which is (2 × Vref/2)/256 for ADC0804.
The following steps must be followed for data conversion by the ADC0804 chip.
1. Make CS = 0 and send a low-to-high pulse to pin WR to start the conversion.
2. Keep monitoring the INTR pin. If INTR is low, the conversion is finished and we can go to
the next step. If INTR is high, keep polling until it goes low.
3. After the INTR has become low, we make CS = 0 and send a high-to-low pulse to the RD
pin to get the data out of the ADC0804 IC chip.
The timing for this process is shown in Figure
Figure: Read and Write Timing for ADC0804
Programming ADC0804 in C
The 8051 C version of the above program is given below.
#include <reg51.h>
sbit RD = P2^5;
sbit WR = P2^6;
sbit INTR = P2^7;
sfr MYDATA = P1;
void main()
{
unsigned char value;
MYDATA = 0xFF; //make P1 and input
INTR = 1; //make INTR and input
RD = 1; //set RD high
WR = 1; //set WR high
while(1)
{
WR = 0; //send WR pulse
WR = 1; //L-to-H(Start Conversion)
while(INTR == 1); //wait for EOC
RD = 0; //send RD pulse
value = MYDATA; //read value
ConvertAndDisplay(value); //(Chap 7 and 12)
RD = 1;
}
}
STEPPER MOTOR INTERFACING
A stepper motor is a widely used device that translates electrical pulses into mechanical
movement. In applications such as disk drives, dot matrix printers, and robotics, the stepper
motor is used for position control. Stepper motors commonly have a permanent magnet rotor
(also called the shaft) surrounded by a stator (see Figure). There are also steppers called
variable reluctance stepper motors that do not have a PM rotor. The most common stepper
motors have four stator windings that are paired with a centertapped common as shown in
Figure. This type of stepper motor is commonly referred to as a four-phase or unipolar
stepper motor. The center tap allows a change of current direction in each of two coils when a
winding is grounded, thereby resulting in a polarity change of the stator. Notice that while a
conventional motor shaft runs freely, the stepper motor shaft moves in a fixed repeatable
increment, which allows one to move it to a precise position. This repeatable fixed movement
is possible as a result of basic magnetic theory where poles of the same polarity repel and
opposite poles attract. The direction of the rotation is dictated by the stator poles. The stator
poles are determined by the current sent through the wire coils. As the direction of the current
is changed, the polarity is also changed causing the reverse motion of the rotor. The stepper
motor discussed here has a total of six leads: four leads representing the four stator windings
and two commons for the center-tapped leads. As the sequence of power is applied to each
stator winding, the rotor will rotate. There are several widely used sequences where each has
a different degree of precision.
Figure: Rotor Alignment and Stator Windings Configuration
Table: Normal Four-Step Sequence
Table: Stepper Motor Step Angles
Step angle
This depends on the internal construction of the motor, in particular the number of teeth on
the stator and the rotor. The step angle is the minimum degree of rotation associated with a
single step. Various motors have different step angles. Table shows some step angles for
various motors. In Table, notice the term steps per revolution. This is the total number of
steps needed to rotate one complete rotation or 360 degrees (e.g., 180 steps × 2 degrees =
360). It must be noted that perhaps contrary to one‘s initial impression, a stepper motor does
not need more terminal leads for the stator to achieve smaller steps. All the stepper motors
discussed in this section have four leads for the stator winding and two COM wires for the
center tap. Although some manufacturers set aside only one lead for the common signal
instead of two, they always have four leads for the stators. Next, we discuss some associated
terminology in order to understand the stepper motor further.
Steps per second and rpm relation
The relation between rpm (revolutions per minute), steps per revolution, and steps per second
is as follows.
Steps per second = rpm × steps per revolution/60
Describe the 8051 connection to the stepper motor of Figure and code a program to
rotate it continuously.
Solution:
The following steps show the 8051 connection to the stepper motor and its programming.
1. Use an ohmmeter to measure the resistance of the leads. This should identify which COM
leads are connected to which winding leads.
2. The common wire(s) are connected to the positive side of the motor‘s power supply. In
many motors, +5 V is sufficient.
3. The four leads of the stator winding are controlled by four bits of the 8051 port (P1.0–
P1.3). However, since the 8051 lacks sufficient current to drive the stepper motor windings,
we must use a driver such as the ULN2003 to energize the stator. Instead of the ULN2003,
we could have used transistors as drivers, as shown in Figure. However, notice that if
transistors are used as drivers, we must also use diodes to take care of inductive current
generated when the coil is turned off. One reason that using the ULN2003 is preferable to the
use of transistors as drivers is that the ULN2003 has an internal diode to take care of back
EMF.
MOV A, #66H ;load step sequence
BACK: MOV P1, A ;issue sequence to motor
RR A ;rotate right clockwise
ACALL DELAY ;wait
SJMP BACK ;keep going
...
DELAY
MOV R2, #100
H1: MOV R3, #255
H2: DJNZ R3, H2
DJNZ R2, H1
RET
Change the value of DELAY to set the speed of rotation. We can use the single-bit
instructions SETB and CLR instead of RR A to create the sequences.
Half-Step eight-Step Sequence
Holding torque
The following is a definition of holding torque: ―With the motor shaft at standstill or zero
rpm condition, the amount of torque, from an external source, required to break away the
shaft from its holding position. This is measured with rated voltage and current applied to the
motor.‖ The unit of torque is ounce-inch (or kg-cm).
Wave drive four-step sequence
In addition to the eight-step and the four-step sequences discussed earlier, there is another
sequence called the wave drive four-step sequence. It is shown in Table. Notice that the
eight-step sequence of Table is simply the combination of the wave drive four-step and
normal four-step sequences, respectively. Experimenting with the wave drive fourstep is left
to the reader.
Unipolar versus bipolar stepper motor interface
There are three common types of stepper motor interfacing: universal, unipolar, and bipolar.
They can be identified by the number of connections to the motor. A universal stepper motor
has eight, while the unipolar has six and the bipolar has four. The universal stepper motor can
be configured for all three modes, while the unipolar can be either unipolar or bipolar.
Wave Drive Four-Step Sequence
Stepper motor control with 8051 C
The 8051 C version of the stepper motor control is given below.
#include <reg51.h>
void main()
{
while(1)
{
P1 = 0x66;
MSDelay(100);
P1 = 0xCC;
MSDelay(100);
P1 = 0x99;
MSDelay(100);
P1 = 0x33;
MSDelay(100);
}
}
A switch is connected to pin P2.7. Write a C program to monitor the status of sw
and perform the following:
(a) If sw = 0, the stepper motor moves clockwise.
(b) If sw = 1, the stepper motor moves counterclockwise
Solution:
#include <reg.h>
sbit SW=P2^7;
void main()
{
SW = 1;
while(1)
{
if(SW == 0)
{
P1 = 0x66;
MSDelay(100);
P1 = 0xCC;
MSDelay(100);
P1 = 0x99;
MSDelay(100);
P1 = 0x33;
MSDelay(100);
}
else
{
P1 = 0x66;
MSDelay(100);
P1 = 0x33;
MSDelay(100);
P1 = 0x99;
MSDelay(100);
P1 = 0xCC;
MSDelay(100);
}
}
}
void MSDelay(unsigned int value)
{
unsigned int x, y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}
DC MOTOR INTERFACING AND PWM
A direct current (DC) motor is another widely used device that translates electrical pulses into
mechanical movement. In the DC motor we have only + and - leads. Connecting them to a
DC voltage source moves the motor in one direction. By reversing the polarity, the DC motor
will move in the opposite direction. One can easily experiment with the DC motor. For
example, small fans used in many motherboards to cool the CPU are run by DC motors. By
connecting their leads to the + and - voltage source, the DC motor moves. While a stepper
motor moves in steps of 1 to 15 degrees, the DC motor moves continuously. In a stepper
motor, if we know the starting position we can easily count the number of steps the motor has
moved and calculate the final position of the motor. This is not possible in a DC motor. The
maximum speed of a DC motor is indicated in rpm and is given in the data sheet. The DC
motor has two rpms: no-load and loaded. The manufacturer‘s data sheet gives the no-load
rpm. The no-load rpm can be from a few thousand to tens of thousands. The rpm is reduced
when moving a load and it decreases as the load is increased. For example, a drill turning a
screw has a much lower rpm speed than when it is in the no-load situation. DC motors also
have voltage and current ratings. The nominal voltage is the voltage for that motor under
normal conditions, and can be from 1 to 150 V, depending on the motor. As we increase the
voltage, the rpm goes up. The current rating is the current consumption when the nominal
voltage is applied with no load and can be from 25 mA to a few amps. As the load increases,
the rpm is decreased, unless the current or voltage provided to the motor is increased, which
in turn increases the torque. With a fixed voltage, as the load increases, the current (power)
consumption of a DC motor is increased. If we overload the motor it will stall, and that can
damage the motor due to the heat generated by high current consumption.
Figure: DC Motor Rotation (Permanent Magnet Field)
PWM: The speed of the motor depends on three factors: (a) load, (b) voltage, and (c) current.
For a given fixed load, we can maintain a steady speed by using a method called pulse width
modulation (PWM). By changing (modulating) the width of the pulse applied to the DC
motor, we can increase or decrease the amount of power provided to the motor, thereby
increasing or decreasing the motor speed. Notice that although the voltage has a fixed
amplitude, it has a variable duty cycle. That means the wider the pulse, the higher the speed.
PWM is so widely used in DC motor control that some microcontrollers come with the PWM
circuitry embedded in the chip. In such microcontrollers, all we have to do is load the proper
registers with the values of the high and low portions of the desired pulse, and the rest is
taken care by the microcontroller. This allows the microcontroller to do other things. For
microcontrollers without PWM circuitry, we must create the various duty cycle pulses using
software, which prevents the microcontroller from doing other things. The ability to control
the speed of the DC motor using PWM is one reason that DC motors are preferable over AC
motors. AC motor speed is dictated by the AC frequency of the voltage applied to the motor
and the frequency is generally fixed. As a result, we cannot control the speed of the AC
motor when the load is increased.
Interface Program
#include <reg51.h>
sbit MOTOR = P1^0;
unsigned char duty_cycle = 128;
void delay_us(unsigned int us)
{
while(us--);
}
void delay_ms(unsigned int ms)
{
unsigned int i;
for(i = 0; i < ms; i++)
delay_us(112);
}
void pwm_software()
{
unsigned char i;
for (i = 0; i < 255; i++)
{
if (i < duty_cycle)
MOTOR = 1;
else
MOTOR = 0;
delay_us(10);
}
}
void main()
{
while (1)
{
for (duty_cycle = 0; duty_cycle < 255; duty_cycle++)
{
pwm_software();
delay_ms(50);
}
}
}
Supporting Information on Stepper Motor
1. Wave Drive (One Phase ON at a Time)
Wave Drive is a method of controlling a stepper motor where only one phase
(winding) is energized at a time in a sequential manner to rotate the motor shaft.
A stepper motor has multiple electromagnetic coils arranged in phases (usually 4
phases: A, B, C, D).
In wave drive, the phases are energized one at a time, in a specific sequence,
causing the rotor to align with the magnetic field of the active coil.
As each phase is activated in sequence, the rotor "steps" forward.
Truth Table (4-Step Sequence)
Step A B C D
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1
The step angle is equal to the motor‘s basic step angle. For example, if the motor‘s step
angle is 1.8°, then each step in wave drive moves the motor by 1.8°.
The step angle (θ) is the angle the stepper motor shaft rotates for each input pulse. It defines
the resolution of the motor.
Smaller step angles = higher resolution and smoother motion.
Step Angle Formula
For a standard stepper motor:
θ = 3600 / N
Where,
θ = Step angle in degrees
N = Number of steps per revolution
Steps per Revolution Formula
θ = 360 / Number of Rotor Teeth × Number of Phases
Example: A 4-phase stepper motor has 50 rotor teeth.
θ = 3600 / 4×50 = 3600 / 200 = 1.80
→ The motor will need 200 steps to complete one full revolution.
2. Full Step Drive (Two Phases ON)
Full Step Drive is a mode of stepper motor excitation where two adjacent windings
(phases) are energized simultaneously to move the rotor from one step position to the next.
This results in higher torque compared to Wave Drive, while maintaining the same step
angle. The rotor aligns with the resultant magnetic field produced by two simultaneously
energized stator windings. Energizing two coils increases the magnetic pull, resulting in
greater holding and running torque. The motor steps through its basic step angle θ (e.g.,
1.8° per step for a 200-step motor).
Truth Table (4-Step Sequence)
Step A B C D
1 1 1 0 0
2 0 1 1 0
3 0 0 1 1
4 1 0 0 1
3. Half Step Drive (Alternating One and Two Phases ON)
Half Step Drive is a stepper motor control method that alternates between energizing
one coil and two coils at a time. This provides twice the resolution compared to full
step mode, resulting in smoother rotation. It combines the sequences of wave drive
and full step drive.
Energizing:
o One coil produces a step.
o Two adjacent coils (at the next step) produces an intermediate step.
This results in twice as many steps per revolution as full step mode.
Truth Table (8-Step Sequence)
Step A B C D
1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1
Summary Table
Feature Wave Drive Full Step Drive Half Step Drive
Phases ON 1 2 Alternating 1 & 2
Step Angle θ θ θ/2
Torque Low High Medium (more uniform)
Power Consumption Low Medium Medium to High
Complexity Low Medium High
Smoothness Low Moderate High
Steps per Cycle 4 4 8