Module - 5: 8051 Interfacing: Syllabus
Module - 5: 8051 Interfacing: Syllabus
LCD Interfacing:
LCD operation
In recent years the LCD is finding widespread use replacing LEDs (seven-segment LEDs or other multi-segment
LEDs) due to the following reasons:
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, # '0' ; display letter 0
ACALL DATAWRT ; call display subroutine
AGAIN: SJMP AGAIN ; stay here
COMNWRT: ; send command to LCD
MOV P1, A ; copy reg A to port 1
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 port 1
SETB P2. 0 ; RS=1 for data
CLR P2. 1 ; R/W =0 for write
SETB P2. 2 ; E=1 for high pulse
Dept. of EEE, SJBIT
4
Microcontroller 15EE52
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 become 0
DJNZ R3, HERE2
RET
END
Where AAAAAAA = 0000000 to 0100111 for line 1 and AAAAAAA = 1000000 to 1100111 for line 2.
LCD addressing
16x2LCD 80 81 82 83 84 85 86 through 8F
C0 C1 C2 C3 C4 C5 C6 through CF
20x1 LCD 80 81 82 83 through 93
20x2 LCD 80 81 82 83 through 93
C0 C1 C2 C3 through D3
20x4 LCD 80 81 82 83 through 93
C0 C1 C2 C3 through D3
94 95 96 97 through A7
D4 D5 D6 D7 through E7
40x2 LCD 80 81 82 83 through A7
C0 C1 C2 C3 through E7
Note: All data is in hex.
Ex-2: Write an 8051 C program to send letters ‘M’, ‘D’, and ‘E’ to the LCD monitoring the busy flag.
Solution:
#include <reg51.h>
sfr 1data = 0x90; //P1 = LCD data pins (Fig. 12-2)
sfr rs = P2^0;
sfr rw= P2^1;
sfr en= P2^2;
sfr busy= P1^7;
void main ( )
{
1cdcmd (0x38);
1cdcmd (0x0E);
1cdcmd (0x01);
1cdcmd (0x06);
1cdcmd (0x86); //line 1, position 6
1cddata (‘M’);
1cddata (‘D’);
1cddata (‘E’);
}
Keyboard Interfacing
Keyboards and LCDs are the most widely used input/output devices of the 8051.
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.
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, the microcontroller waits 20 ms for the bounce and then scans the columns
again. This serves two functions: (a) it ensures that the first key press detection was not an erroneous one due to a spike
noise, and (b) the 20-ms delay prevents the same key press from being interpreted as a multiple key press. If after the
20-ms delay the key is still pressed, it goes to the next stage to detect which row it belongs to; otherwise, it goes back
into the loop to detect a real key press.
To detect which row the key press belongs to, the microcontroller 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 the ASCII value) for that row
and goes to the next stage to identify the key.
To identify the key press, the microcontroller rotates the column bits, one bit at a time, into the carry flag and checks to
see if it is low. Up on 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.
While the key press detection is standard for all keyboards, the process for determining which key is pressed varies.
The ADC0808 chip allows us to monitor up to 8 different analog inputs using only a single chip. The ADC0808/0809
has an 8-bit data output. The 8 analog input channels are multiplexed and selected according to below table using three
address pins, A, B, and C.
Table: ADC0808/0809 analog channel selection
Selected Analog Channel C B A
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1
In the ADC0808/0809, Vref(+) and Vref(-) set the reference voltage. If Vref(-) = Gnd and Vref (+) = 5V, the step size is 5
V /256 =19.53 mV. Therefore, to get a 10 mV step size we need to set Vref(+) = 2.56V and Vref(-) = Gnd. From the
above figure, notice the ALE pin. We Use A, B, and C addresses to select IN0 – IN7, and activate ALE to latch in the
address. SC is for start conversion. EOC is for end-of-conversion, and OE is for output enable (READ). Table below
shows the step size relation to the Vref voltage.
Programming ADC0808/0809 in C
#include <reg51.h>
sbit ALE = P2^4;
sbit OE = P2^5;
sbit SC = P2^6;
sbit EOC = P2^7;
sbit ADDR_A = P2^0;
sbit ADDR_B = P2^1;
sbit ADDR_C = P2^2;
sfr MYDATA = P1;
void main ( )
{
unsigned char value; //make P1 an input
MYDATA = 0xFF; //make EOC an input
ALE = 0; //clear ALE
OE = 0; //clear OE
SC = 0; // clear SC
while (1)
{
ADDR_C = 0; //C=0
ADDR_B = 0; //B=0
ADDR_A = 1; //A=1 (Select Channel 1)
MSDelay(1); //delay for fast DS89C4x0
ALE = 1;
MSDELAY(1);
SC = 1;
Serial ADC:
For many applications where space is a critical issue, using large number of parallel pins for data is not feasible for
which serial ADC are becoming widely used.
MAX1112 ADC:
The MAX1112 is an 8-bit serial ADC chip with 8 channels of analog input. It has a single D out pin to bring out the
digital data after it has been converted:
CH0-CH7: CH0 - CH7 are 8 channels of the analog inputs. In the single-ended mode, each of the channels can be used
for an analog input where the COM pin is used as a ground reference for all the channels. In single- ended mode, 8
channels of input allow us to read 8 different analog inputs. We select the input channel by sending in the control byte
via the DIN pin. In differential mode, we have 4 sets of 2-channel differentials. CH0 and CH1 go together, and CH2 -
CH3, and so on.
CS: Chip select is an active low Input used to select the MAX112chip. To send in the control byte via the D IN pin, CS
must be low. When CS is high the DOUT is high impedance.
Dept. of EEE, SJBIT
16
Microcontroller 15EE52
SCLK: Serial clock input. SCLK is used to bring data out and send in the control byte, one bit at a time.
DOUT: Serial data out. The digital data is clocked out one bit at a time on the H-to-L edge (falling edge) of SCLK.
DIN: Serial data in the control byte is clocked in one bit at a time on the L-to-H edge (rising edge) of SCLK.
SSTRB: Serial strobe output. In internal clock mode-this indicates end-of-conversion. It goes high when the
conversion is complete.
AGND, DGND (analog ground and digital ground): Both are input pins providing ground for both the analog and
the digital signals.
SHDN: Shutdown is an input and is normally not connected (or is connected to V DD). If low, the ADC is shut down to
save power. This is shut down by hardware. The control byte causes shutdown by software.
REFIN: Reference voltage input. This voltage dictates the step size.
REFOUT: Internal Reference Generator output. A 1 µF bypass capacitor is placed between this pin and AGND.
Start The MSB (D7) must be high to define the beginning of the control byte.
SEL2 SEL1 SEL0 CHANNEL SELECTION (SINGLE-ENDED MODE0)
0 0 0 CHAN0
0 0 1 CHAN1
0 1 0 CHAN2
0 1 1 CHAN3
1 0 0 CHAN4
1 0 1 CHAN5
1 1 0 CHAN6
1 1 1 CHAN7
UN/BIP 1 = unipolar: Digital data output is binary 00 – FFH
0 = bipolar: Digital data output is in 2’s complement.
SGL/DIF 1= single-ended: 8 channels of single-ended with COM as reference
0 = differential: Two channels (eg., CH0-CH1) are differential.
PD1 1= fully operational
0= power-down: Power down to save power using software.
PD0 1= external clock mode: The conversion speed is dictated by SCLK.
0 = internal clock mode: The conversion speed is dictated internally, and the SSTRB pin
goes high to indicate end-of-conversion (EOC).
Dept. of EEE, SJBIT
17
Microcontroller 15EE52
Selecting a channel:
We select the analog input channel using the control byte. The MSB (D7) of the control byte must be high. The control
byte is fed into the DIN pin one bit at a time using SCLK. The DIN pin clocks in the control byte on the rising edge of
SCLK.
Ex: Find the MAX1112 control byte for (a) CH0, and (b) CH3. Assume single-ended, unipolar, internal clock,
and fully operational modes.
Solution:
CS BIT P2.0
SCLK BIT P2.1
DIN BIT P2.2
DOUT BIT P2.3
#include <reg51.h>
sbit CS = P2^0;
sbit SCLK= P2^1;
sbit DIN = P2^2;
sbit DOUT = P2^3;
sbit MSBRA = ACC^7;
void main ( )
{
unsigned char conbyte = 0x9E; //Chan 1
unsigned char x;
ACC = conbyte;
CS = 0;
for (x = 0; x<8; x++)
{
SCLK = 0; //Send D7 of Reg A to Din
DIN = MSBRA;
Delay ( );
SCLK = 1; // latch in the bit
Delay ( );
ACC = ACC<< 1; // next bit
}
CS = 1; // deselect MAX1112
SCLK = 0; // make SCLK low during conversion
}
CS BIT P2.0
SCLK BIT P2.1
DIN BIT P2.2
DOUT BIT P2.3
#include <reg51.h>
sbit CS = P2^0;
sbit SCLK= P2^1;
sbit DIN = P2^2;
sbit DOUT = P2^3;
sbit LSBRA = ACC^0;
void main ( )
{
unsigned char x;
Ex: ALP to select the channel and read the ADC data
CS BIT P2.0
SCLK BIT P2.1
DIN BIT P2.2
DOUT BIT P2.3
ORG 0H
; sending in control byte
MAIN: MOV A, #9EH ; channel 1
MOV R3, #8 ; load count
CLR CS ; CS=0
H1: RLC A ; give bit to CY
MOV DIN, C ; send bit to DIN
CLR SCLK ; low SCLK for L-H pulse
ACALL DELAY ; delay
SETB SCLK ;latch data
ACALL DELAY ; delay
DJNZ R3, H1 ; repeat for all 8 bits
SETB CS ; deselect ADC, conv starts
CLR SCLK ; SCLK = 0 during conversion
SETB DOUT ; make it an input
; Reading data out
CLR CS ; CS = 0
SETB SCLK
ACALL DELAY ; need delay for DS89C4x0
CLR SCLK ; first H-to-L
ACALL DLELAY ; read data out on 2nd H-L
MOV R3, #8 ;
Ex: C Program to select the channel and read the ADC data
#include <reg51.h>
sbit CS = P2^0;
sbit SCLK= P2^1;
sbit DIN = P2^2;
sbit DOUT = P2^3;
sbit MSBRA = ACC^7;
sbit LSBRA = ACC^0;
void main ( )
{
unsigned char conbyte = 0x9E; //Chan 1
unsigned char x;
while (1)
{
ACC = conbyte; //select the channel
CS = 0;
for (x=0; x<8; x++)
{
SCLK = 0;
DIN = MSBRA; //send D7 of Reg A to Din
Delay ( );
SCLK = 1; //latch in the bit
Delay ( );
ACC = ACC << 1; //next bit
}
CS = 1; //deselect MAX1112
SCLK = 0; // make SCLK low during conversion
CS = 0; //read the data
SCLK = 1; // an extra H-to-L pulse
Delay( );
SCLK = 0; // get all 8 bits
Delay();
For (x=0, x<8, x++)
{
SCLK = 1;
Delay ( );
Dept. of EEE, SJBIT
22
Microcontroller 15EE52
SCLK = 0;
Delay ( )
LSBRA = DOUT; //bring in bit from DOUT pin to D0 of Reg A
ACC = ACC << 1; //keep shifting data for all 8 bits
}
CS = 1; //deselect ADC
P1=ACC; //display data on P1
}
}
DAC INTERFACING:
Digital-to-analog (DAC) converter:
The digital-to-analog converter (DAC) is a device widely used to convert digital pulses to analog signals.
Two methods of creating a DAC are: binary weighted and R/ 2R ladder. The majority of integrated circuit DACs uses
the R/2R method since it can achieve a much higher degree of precision. 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.
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:
𝐷7 𝐷6 𝐷5 𝐷4 𝐷3 𝐷2 𝐷1 𝐷0
𝐼𝑜𝑢𝑡 = 𝐼𝑟𝑒𝑓 ( + + + + + + + )
2 4 8 16 32 64 128 256
Where 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.0 mA. Figure above shows the generation of current reference (setting Iref = 2 mA) by using
the standard 5-V power supply and 1K and l.5K-ohm standard, resistors.
Ex-3: For the above circuit, find the maximum output amplitude of the saw tooth waveform obtained with the
following program.
(a) Here, the method to generate the saw tooth wave is to increment the A register from 0 to FF to 0 continuously, In
this case, the maximum value of the digital number is FFH =11111111B.
𝐷7 𝐷6 𝐷5 𝐷4 𝐷3 𝐷2 𝐷1 𝐷0
𝐼𝑜𝑢𝑡 = 𝐼𝑟𝑒𝑓 ( + + + + + + + )
2 4 8 16 32 64 128 256
V OUT = 1.99mA x 5K = 9.96 V ;The peak amplitude of the saw tooth waveform is 9.96 V.
(b) In this case, the maximum value of the digital number is 64 H = 01100100B. With the same calculation,
IOUT = 0.78125MA
VOUT = 3.906 V
Ex: In the circuit, connect a switch SW to pin P0.0. Write a program to do the following:
When SW = 0, the DAC output gives a staircase waveform.
When SW = 1, the DAC output gives a triangular waveform.
Solution:
Staircase waveform for 5 steps: 255/5 = 51.
So the increment of each step is 51. After maximum value is reached, the output drops to zero and the next cycle starts.
SW EQU P0.0
ORG 0000H
Dept. of EEE, SJBIT
24
Microcontroller 15EE52
SETB SW ; make SW an input port
CHECK: MOV C, SW ; move SW to carry
JC TRIANG ; if SW = 1, jump to TRIANG
START: MOV A, #00 ; this is for the staircase, A = 0
MOV P1, A ; move A to P1
ACALL DELAY ; delay
RPT: ADD A, #51 ; add 51 to get the next step
MOV P1, A ; output value to port 1
ACALL DELAY ; delay
CJNE A, #255, RPT ; check if A =255. If not, go to next step
SJMP CHECK ; the staircase has reached maximum value
; check SW before continuing
TRIANG: MOV A, #00 ; this is for the triangular wave, A = 0
INCR: MOV P1, A ; output value on P1
INC A ; increment A for upward transition
CJNE A, #255, INCR ; check if A = 255, if not keep incrementing
DECR: MOV P1, A ; output value on P1
DEC A ; decrement A for downward transition
CJNE A, #00, DECR ; if A = 0, the minimum, step out
SJMP CHECK ; check SW before starting the next cycle
DELAY:
RPT1: MOV R1, #20 ; this delay routine gives a delay ‘T’
RPT2: MOV R2, #200
RPT3: DJNZ R0, RPT3
DJNZ R0, RPT2
DJNZ R0, RPT1
RET
END
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];
}
}
}
Temperature sensors:
Temperature is converted to electrical signals using a transducer called a thermistor. A thermistor responds to
temperature change by changing resistance, but its response is not linear as shown in below table. Complexity
associated with writing software for such nonlinear devices has led many manufacturers to
a linear temperature sensor. Simple and widely used linear temperature sensors include the LM34 and LM35 series
from National Semiconductor Corp.
Table: Thermistor Resistance vs. Temperature
Temperature (C) Tf (K ohms)
0 29.490
25 10.000
50 3.893
75 1.700
100 0.817
Figure below shows the connection of a temperature sensor to the ADC0848. Here LM336-2.5 zener diode is used to
fix the voltage across the 10K pot at 2.5 volts. The use of the LM336-2.5 should overcome any fluctuations in the
power supply.
Ex: ALP to read temperature, convert it and put it on P0 with some delay
CONVERSION:
MOV B, #10
DIV AB
MOV R7, B ; least significant byte
MOV B, #10
DIV AB
MOV R6, B
MOV R5, A ; most significant byte
RET
DATA_DISPLAY:
MOV P0, R7
ACALL DELAY
MOV P0, R6
ACALL DELAY
MOV P0, R5
ACALL DELAY
RET
DELAY:
MOV R0, #0FFh
ITR2: MOV R1, #0FFh
ITR1: DJNZ R1, ITR1
DJNZ R0, ITR2
RET
Ex: C program to read temperature, convert it and put it on P0 with some delay
#include <reg51.h>
sbit RD = P2^5;
sbit WR= P2^6;
sbit INTR = P2^7;
sfr MYDATA = P1; //P1 connected to D0-D7 of 848
void ConvertAndDisplay (unsigned char value);
void MSDelay (unsigned int value);
void main ( )
{
MYDATA = 0x FF; //make P1 and input
Dept. of EEE, SJBIT
29
Microcontroller 15EE52
INTR = 1; //make INTR and input
RD = 1; //set RD high
WR = 1; //set WR high
{
WR = 0; //send WR pulse
WR = 1;
While (INTR = = 1); //wait for EOC
RD = 0; //send RD pulse
Value = MYDATA; //read value from ADC0848
ConvertAndDisplay (value);
RD = 1;
}
}
void ConvertAndDisplay (unsigned char value)
{
unsigned char x, d1, d2, d3;
x = value/10;
d1 = value%10;
d2 = x %10
d3 = x/10
P0 = d1; //LSBYTE
MSDelay (250);
P0 = d2;
MSDelay (250);
P0 = d3; //MSBYTE
MSDelay (250);
}
void MSDelay (unsigned int value)
{
unsigned char x, y;
for (x=0; x<value; x++)
for (y=0;y<1275;y++);
}