Electrical-Engineering Engineering Microcontroller Interfacing-And-Applications Notes
Electrical-Engineering Engineering Microcontroller Interfacing-And-Applications Notes
com
8051 Interfacing and Applications Microcontroller
This chapter basically gives an insite into the study of different interfacings listed above.
Further we will also study and understand their operation that is the working principle. We
will further discuss on how to develop these interfaces using assembly and C.
Page 1
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
sequence such that the motor rotates in a particular direction. Stepper motor shaft moves in a
fixed repeatable increment, which allows one to move it to a precise position. Direction of the
rotation is dictated by the stator poles. Stator poles are determined by the current sent through
the wire coils.
Step angle:
Step angle is defined as the minimum degree of rotation with a single step.
No of steps per revolution = 360° / step angle
Steps per second = (rpm x steps per revolution) / 60
Example: step angle = 2°
No of steps per revolution = 180
Switching Sequence of Motor:
As discussed earlier the coils need to be energized for the rotation. This can be done by
sending a bits sequence to one end of the coil while the other end is commonly connected.
The bit sequence sent can make either one phase ON or two phase ON for a full step
sequence or it can be a combination of one and two phase ON for half step sequence. Both
are tabulated below.
Full Step:
Two Phase ON
One Phase ON
Page 2
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Program:
MOV A,#66H
BACK: MOV P1,A
RR A
ACALL DELAY
SJMP BACK
Page 3
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Example 2: A switch is connected to pin P2.7. Write an ALP to monitor the status
of the SW. If SW = 0, motor moves clockwise and if SW = 1, motor moves
anticlockwise.
Program:
ORG 0000H
SETB P2.7
MOV A, #66H
MOV P1,A
TURN: JNB P2.7, CW
RL A
ACALL DELAY
MOV P1,A
SJMP TURN
CW: RR A
ACALL DELAY
MOV P1,A
SJMP TURN
DELAY: as previous example
Example 3: Write an ALP to rotate a motor 90° clockwise. Step angle of motor is
2°.
Solution:
Step angle = 2°
Steps per revolution = 180
No of rotor teeth = 45
For 90° rotation the no of steps is 45
Program:
ORG 0000H
MOV A, #66H
MOV R0, #45
BACK: RR A
MOV P1, A
ACALL DELAY
DJNZ R0, BACK
END
Page 4
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Example 4: Rotate the stepper motor continuously clockwise using half-step 8-step
sequence. Say the sequence is in ROM locations.
Program:
ORG 0000H
START: MOV R0, #08
MOV DPTR, #HALFSTEP
RPT: CLR A
MOVC A, @A+DPTR
MOV P1, A
ACALL DELAY
INC DPTR
DJNZ R0, RPT
SJMP START
ORG 0200H
HALFSTEP DB 09, 08, 0CH, 04, 06, 02, 03, 01
END
Program:
#include <reg51.h>
void main ()
{
while (1)
{
P1=0x66;
MSDELAY (200);
P1=0x33;
MSDELAY (200);
P1=0x99;
MSDELAY (200);
P1=0xCC;
MSDELAY (200);
}
}
void MSDELAY (unsigned char value)
{
unsigned int x,y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}
Page 5
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Program:
#include <reg51.h>
sbit SW=P2^7;
void main ()
{
SW=1;
while (1)
{
if(SW==0){
P1=0x66;
MSDELAY (100);
P1=0x33;
MSDELAY (100);
P1=0x99;
MSDELAY (100);
P1=0xCC;
MSDELAY (100);
}
else {
P1=0x66;
MSDELAY (100);
P1=0xCC;
MSDELAY (100);
P1=0x99;
MSDELAY (100);
P1=0x33;
MSDELAY (100);
}
void MSDELAY (unsigned char value)
{
unsigned int x,y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}
Page 6
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Page 7
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Example 6: A switch is connected to pin P2.7. Write an ALP to monitor the status of the
SW. If SW = 0, DC motor moves clockwise and if SW = 1, DC motor moves
anticlockwise.
Program:
ORG 0000H
CLR P1.0
CLR P1.1
CLR P1.2
CLR P1.3
SETB P2.7
MONITOR: JNB P2.7, CLOCK
SETB P1.0
CLR P1.1
CLR P1.2
SETB P1.3
SJMP MONITOR
CLOCK: CLR P1.0
SETB P1.1
SETB P1.2
CLR P1.3
SJMP MONITOR
END
Page 8
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Example 7: A switch is connected to pin P2.7. Write a C to monitor the status of the SW.
If SW = 0, DC motor moves clockwise and if SW = 1, DC motor moves anticlockwise.
Program:
# include <reg51.h>
sbit SW =P2^7;
sbit Enable = P1^0;
sbit MTR_1 = P1^1;
sbit MTR_2 = P1^2;
void main ( )
{
SW=1;
Enable = 0;
MTR_1=0;
MTR_2=0;
while( )
{
Enable =1;
if( SW==1)
{ MTR_1=1;
MTR_2=0;
}
else
{ MTR_1=0;
MTR_2=1;
}}}
Page 9
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Example 8: A switch is connected to pin P2.7. Write an C to monitor the status of the SW.
If SW = 0, DC motor moves 50% duty cycle pulse and if SW = 1, DC motor moves with
25% duty cycle pulse.
Program:
# include <reg51.h>
sbit SW =P2^7;
sbit MTR = P1^0;
void main ( )
{
SW=1;
MTR=0;
while( )
{
if( SW==1)
{ MTR=1;
Msdelay(25);
MTR=0;
Msdelay(75);
}
else
{ MTR=1;
Msdelay(50);
MTR=0;
Msdelay(50);
}
}
}
The interfacing diagrams for the above examples can be referred to the text.
Digital-to-Analog (DAC) converter:
The DAC is a device widely used to convert digital pulses to analog signals. In this section
we will discuss the basics of interfacing a DAC to 8051.
The two method of creating a DAC is binary weighted and R/2R ladder.
The Binary Weighted DAC, which contains one resistor or current source for each bit of the
DAC connected to a summing point. These precise voltages or currents sum to the correct
output value. This is one of the fastest conversion methods but suffers from poor accuracy
because of the high precision required for each individual voltage or current. Such high-
precision resistors and current-sources are expensive, so this type of converter is usually
limited to 8-bit resolution or less.
The R-2R ladder DAC, which is a binary weighted DAC that uses a repeating cascaded
structure of resistor values R and 2R. This improves the precision due to the relative ease of
producing equal valued matched resistors (or current sources). However, wide converters
perform slowly due to increasingly large RC-constants for each added R-2R link.
Page 10
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
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 2 n, where n is
the number of data bit inputs.
DAC0808:
The digital inputs are converter to current Iout, and by connecting a resistor to the Iout pin, we
can convert the result to voltage. The total current Iout is a function of the binary numbers at
the D0-D7 inputs of the DAC0808 and the reference current Iref , and is as follows:
Usually reference current is 2mA. Ideally we connect the output pin to a resistor, convert this
current to voltage, and monitor the output on the scope. But this can cause inaccuracy; hence
an opamp is used to convert the output current to voltage. The 8051 connection to DAC0808
is as shown in the figure 6 below.
Program:
MOV A, #00H
INCR: MOV P1, A
INC A
CJNE A, #255, INCR
DECR: MOV P1, A
DEC A
CJNE A, #00, DECR
SJMP INCR
END
Page 11
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Solution: Calculate the decimal values for every 10 degree of the sine wave. These
values can be maintained in a table and simply the values can be sent to port P1. The
sinewave can be observed on the CRO.
Program:
ORG 0000H
AGAIN: MOV DPTR, #SINETABLE
MOV R3, #COUNT
UP: CLR A
MOVC A, @A+DPTR
MOV P1, A
INC DPTR
DJNZ R3, UP
SJMP AGAIN
ORG 0300H
SINETABLE DB 128, 192, 238, 255, 238, 192, 128, 64, 17, 0, 17, 64, 128
END
Note: to get a better wave regenerate the values of the table per 2 degree.
Page 12
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
ADC804 chip:
ADC804 IC is an analog-to-digital converter. It works with +5 volts and has a resolution of 8
bits. Conversion time is another major factor in judging an ADC. Conversion time is defined
as the time it takes the ADC to convert the analog input to a digital (binary) number. In
ADC804 conversion time varies depending on the clocking signals applied to CLK R and
CLK IN pins, but it cannot be faster than 110μs.
0804
Pin Description of ADC804:
Typical values are R = 10K ohms and C =150pF. We get f = 606 kHz and the conversion
time is 110μs.
Vref/2 : It is used for the reference voltage. If this pin is open (not connected), the analog
input voltage is in the range of 0 to 5 volts (the same as the Vcc pin). If the analog
input range needs to be 0 to 4 volts, Vref/2 is connected to 2 volts. Step size is the
smallest change can be discerned by an ADC
Page 13
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
D0-D7: The digital data output pins. These are tri-state buffered. The converted data is
accessed only when CS =0 and RD is forced low. To calculate the output voltage, use
the following formula
Analog ground and digital ground: Analog ground is connected to the ground of the
analog Vin and digital ground is connected to the ground of the Vcc pin. To isolate
the analog Vin signal from transient voltages caused by digital switching of the output
D0 – D7. This contributes to the accuracy of the digital data output.
Vin(+) & Vin(-): Differential analog inputs where Vin = Vin (+) – Vin (-). Vin (-) is
connected to ground and Vin (+) is used as the analog input to be converted.
RD: Is “output enable” a high-to-low RD pulse is used to get the 8-bit converted data out
of ADC804.
INTR: It is “end of conversion” When the conversion is finished, it goes low to signal the
CPU that the converted data is ready to be picked up.
The following steps must be followed for data conversion by the ADC804 chip:
Page 14
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Figure 8 shows the read and write timing for ADC804. Figure 9 and 10 shows the self
clocking with the RC component for frequency and the external frequency connected to
XTAL2 of 8051.
Figure 10: 8051 Connection to ADC0804 with Clock from XTAL2 of 8051
Now let us see how we write assembly as well as C program for the interfacing diagram
shown in figure 10.
Page 15
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
MYDATA EQU P1
MOV P1, #0FFH
SETB P2.7
BACK: CLR P2.6
SETB P2.6
HERE: JB P2.7, HERE
CLR P2.5
MOV A, MYDATA
SETB P2.5
SJMP BACK
Programming ADC0804 in C
#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;
INTR=1;
RD=1;
WR=1;
While (1)
{
WR=0;
WR=1;
While (INTR == 1);
RD=0;
Value =Mydata;
RD=1;
}
}
ADC0808/0809 chip:
ADC808 has 8 analog inputs. It allows us to monitor up to 8 different transducers using only
single chip. The chip has 8-bit data output just like the ADC804. The 8 analog input channels
are multiplexed and selected according to the values given to the three address pins, A, B,
and C. that is; if CBA=000, CH0 is selected; CBA=011, CH3 is selected and so on. The pin
details of ADC0808 are as shown in the figure 11 below. (Explanation can be done as is with
ADC0804).
Page 16
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
MYDATA EQU P1
ORG 0000H
MOV MYDATA, #0FFH
SETB P2.7
CLR P2.4
CLR P2.6
CLR P2.5
BACK: CLR P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
SETB P2.4
ACALL DELAY
SETB P2.6
ACALL DELAY
CLR P2.4
CLR P2.6
HERE: JB P2.7, HERE
HERE1: JNB P2.7, HERE1
SETB P2.5
ACALL DELAY
MOV A, MYDATA
CLR P2.5
SJMP BACK
Note: replace the assembly instructions with equivalent C statements for programming
ADC0808 in C
Page 17
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
LCD Interfacing:
LCD is finding widespread use replacing LEDs for the following reasons:
The declining prices of LCD
The ability to display numbers, characters, and graphics
Incorporation of a refreshing controller into the LCD, thereby relieving the CPU of
the task of refreshing the LCD
Ease of programming for characters and graphics
Pin Description:
Page 18
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
LCD timing diagram for reading and writing is as shown in figure 14 and 15.
Page 19
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Example 11: Write an ALP to initialize the LCD and display message “YES”. Say
the command to be given is :38H (2 lines ,5x7 matrix), 0EH (LCD on, cursor on),
01H (clear LCD), 06H (shift cursor right), 86H (cursor: line 1, pos. 6)
Program:
;calls a time delay before sending next data/command ;P1.0-P1.7 are connected to
LCD data pins D0-D7 ;P2.0 is connected to RS pin of LCD ;P2.1 is connected to
R/W pin of LCD ;P2.2 is connected to E pin of 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,#86H ;cursor at line 1, pos. 6
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
Page 20
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
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
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
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 ;
BACK:SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0 H-to-L pulse
JB P1.7,BACK ;stay until busy flag=0
RET
END
Page 22
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Programming LCD in C
Example 13: Write an 8051 C program to send letters ‘P’, ‘I’, and ‘C’ to the LCD using
the busy flag method.
Solution:
#include <reg51.h>
sfr ldata = 0x90; //P1=LCD data pins
sbit rs = P2^0;
sbit rw = P2^1;
sbit en = P2^2;
sbit busy = P1^7;
void main(){
lcdcmd(0x38);
lcdcmd(0x0E);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x86); //line 1, position 6
lcddata(‘P’);
lcddata(‘I’);
lcddata(‘C’);
}
Page 23
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
void lcdready(){
busy = 1; //make the busy pin at input
rs = 0;
rw = 1;
while(busy==1){ //wait here for busy flag
en = 0; //strobe the enable pin
MSDelay(1);
en = 1;
}
}
void Msdelay(unsigned int itime){
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
Keyboard Interfacing:
Keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and
columns through ports. Therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be
connected to a microprocessor. When a key is pressed, a row and a column make a contact.
Otherwise, there is no connection between rows and columns. A 4x4 matrix connected to two
ports. The rows are connected to an output port and the columns are connected to an input
port.
Scanning and Identifying the Key:
Page 24
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
If one of the column bits has a zero, this means that a key press has occurred For
example, if D3 – D0 = 1101, this means that a key in the D1 column has been pressed
After detecting a key press, microcontroller will go through the process of identifying
the key
Starting with the top row, the microcontroller grounds it by providing a low to row D0
only. It reads the columns, if the data read is all 1s, no key in that row is activated and
the process is moved to the next row
It grounds the next row, reads the columns, and checks for any zero. This process
continues until the row is identified.
After identification of the row in which the key has been pressed. Find out which
column the pressed key belongs to
Algorithm for detection and identification of key activation goes through the following
stages:
1. To make sure that the preceding key has been released, 0s are output to all rows at once,
and the columns are read and checked repeatedly until all the columns are high
When all columns are found to be high, the program waits for a short amount of time
before it goes to the next stage of waiting for a key to be pressed
2. To see if any key is pressed, the columns are scanned over and over in an infinite loop until
one of them has a 0 on it
Remember that the output latches connected to rows still have their initial zeros
(provided in stage 1), making them grounded
After the key press detection, it waits 20 ms for the bounce and then scans the
columns again
(a) It ensures that the first key press detection was not an erroneous one due a spike
noise
(b) The key press. If after the 20-ms delay the key is still pressed, it goes back into the
loop to detect a real key press
3. To detect which row key press belongs to, it grounds one row at a time, reading the
columns each time
If it finds that all columns are high, this means that the key press cannot belong to that
row. Therefore, it grounds the next row and continues until it finds the row the key
press belongs to
Upon finding the row that the key press belongs to, it sets up the starting address for
the look-up table holding the scan codes (or ASCII) for that row
4. To identify the key press, it rotates the column bits, one bit at a time, into the carry flag and
checks to see if it is low
Upon finding the zero, it pulls out the ASCII code for that key from the look-up table
otherwise, it increments the pointer to point to the next element of the look-up table
The flowchart for the above algorithm is as shown below:
Page 25
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Note: The assembly as well as the C program can be written in accordance to the algorithm of
the flowchart shown.
Page 26
www.getmyuni.com
8051 Interfacing and Applications Microcontroller
Summary
This chapter gives the details of six different devices that can be interfaced to 8051. These are
widely used in many applications. Initially, we discussed about the stepper motor, giving the
details on the working, sending sequence and hence writing assembly and C program. In
continuation to that we also learnt how to interface DC motor, and DC motors with PWM.
The chapter also covers the study of devices such as DAC, parallel ADC and serial ADC,
LCD and Keyboard along with the interfacing of these devices to 8051. We further, studied
how to write assembly and C program for all the above said interfaces which will help in
developing applications.
Page 27