0% found this document useful (0 votes)
39 views42 pages

Mod 5 LED LCD Keypad ADC - DAC Full

This document covers the interfacing of input and output devices with the 8051 microcontroller, focusing on LEDs, LCDs, and keypads. It explains the principles of operation, configurations, and programming for these devices, including assembly language examples for displaying data on an LCD and reading input from a keypad. The document also provides detailed pin descriptions, command codes, and timing requirements for effective interfacing.

Uploaded by

jegaxil828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views42 pages

Mod 5 LED LCD Keypad ADC - DAC Full

This document covers the interfacing of input and output devices with the 8051 microcontroller, focusing on LEDs, LCDs, and keypads. It explains the principles of operation, configurations, and programming for these devices, including assembly language examples for displaying data on an LCD and reading input from a keypad. The document also provides detailed pin descriptions, command codes, and timing requirements for effective interfacing.

Uploaded by

jegaxil828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

MICROPROCESSORS AND

MICROCONTROLLERS (BECE204L)

MODULE 5: I/O Interfacing with 8051


LCD, LED AND
KEYBOARD
INTERFACING
Chapter 12
Interfacing in 8051
 Interfacing is the process of connecting devices together so that they can exchange the information and
that proves to be easier to write the programs.

 Interfacing is one of the important concepts in microcontroller 8051 because the microcontroller is a
CPU that can perform some operation on a data and gives the output. However to perform the
operation we need an input device to enter the data and in turn output device displays the results of
the operation.

 There are different type of input and output devices as for our requirement such as LEDs, LCDs,
7segment, ADC, keypad, motors, sensors etc.
Interfacing LED with 8051
 LEDs are most commonly used in many applications for indicating the output.
 They find huge range of applications as indicators during test to check the validity of
results at different stages.
 They are very cheap and easily available in a variety of shape, color and size

 LED principle of operation


 A simple LEDs also servers as a basic display devices, it On and OFF state express
meaning full information about a device.
 The common available LEDs have a 1.7v voltage drop that means when we
apply above 1.7V, the diode conducts. The diode needs 10mA current to glow
with full intensity
 LEDs can be interfaced to the microcontroller in either common anode or
common cathode configuration. Here the LEDs are connected in common anode
configuration because the common cathode configuration consumes more
power
LED Interfacing with 8051 Microcontroller

7-Segment Display
ALP to display 0 to 9 in 7-segment
display which is connected to Port 1
LCD INTERFACING
 LCD is finding widespread use replacing LEDs.
 The declining prices of LCD.
 It has ability to display numbers, characters, and
graphics.
 Ease of programming for characters and graphics.

 Incorporation of a refreshing controller into the LCD, thereby relieving the CPU of the task
of refreshing the LCD.
 16×2 LCD means that the are two rows in which 16 characters can be displayed per line, and
each character takes 5×7 matrix space on LCD.
 The most common LCD controller is HITACHI 44780 which provides a simple interface
between the microcontroller and an LCD.
 The commonly used alphanumeric displays are 16 * 1 (16 characters & single line), 16 * 2
(16 characters & double line) and 20 * 4 (20 characters per line & four lines).
Interfacing LCD with 8051
Table 12-1: Pin Descriptions for LCD

Pin Symbol I/O Description


1 VSS -- Ground
2 VCC -- +5V power supply
3 3 VEE -- Power supply to control contrast
4 RS I RS = 0 to select command register,
RS = 1 to select data register
5 R/W I R/W = 0 for write, R/W = 1 for read
6 E I/O Enable
7 DB0 I/O The 8-bit data bus
8 DB1 I/O The 8-bit data bus
9 DB2 I/O The 8-bit data bus
10 DB3 I/O The 8-bit data bus
11 DB4 I/O The 8-bit data bus
12 DB5 I/O The 8-bit data bus
13 DB6 I/O The 8-bit data bus
14 DB7 I/O The 8-bit data bus
Figure 12-3. LCD Timing for Read ( L-to-H for E line)

tD = Data output delay time


tAS = Setup time prior to E (going high) for both RS and R/W = 140 ns (minimum)
tAH = Hold time after E has come down for both RS and R/W = 10 ns (minimum)
Note: Read requires an L-to-H pulse for the E pin.
Figure 12-4. LCD Timing for Write (H-to-L for E line)

tPWH = Enable pulse width = 450 ns (minimum)


tDSW = Data setup time = 195 ns (minimum)
tH = Data hold time = 10 ns (minimum)
tAS = Setup time prior to E (going high) for both RS and R/W = 140 ns (minimum)
tAH = Hold time after E has come down for both RS and R/W = 10 ns (minimum)
LCD Data Sheet

RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0


0 0 1 A A A A A A A

where AAAAAAA = 0000000 to 0100111 for line 1


and AAAAAAA = 1000000 to 1100111 for line 2.

DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0


Line 1 (min) 1 0 0 0 0 0 0 0
Line 1 (max) 1 0 1 0 0 1 1 1
Line 2 (min) 1 1 0 0 0 0 0 0
Line 2 (max) 1 1 1 0 0 1 1 1
LCD Command Codes
Hex code Description Hex code Description
01 Clear display screen 0E Display on, cursor on

02 Return home 0F Display on, cursor blinking

04 Shift cursor from right to left 10 Shift cursor position to left

06 Shift cursor from left to right 14 Shift cursor position to the right

05 Shift display right 18 Shift the entire display to left

07 Shift display left 1C Shift the entire display to the right

08 Display off. cursor off 80 Force cursor to the beginning of 1″ line

0A Display off. cursor on C0 Force cursor to the beginning of 2 line

0C Display on. cursor off 38 2 lines and 5×7 matrix


LCD INTERFACING
1. 38H command is used to select LCD of two rows and 5×7 matrix display. To
display each character on LCD, the dot matrix pattern 5×7 is used.
2. 0EH is used to display ON and cursor ON.
3. 01H is used to clear the previous data displayed on LCD.
4. Next command is related to the address where we need to display the character.
we need to write the address of the character to the command register.
5. Then we can write the ASCII value of character to the data register.
6. After displaying the character at the address automatically the courser position
is incremented by one. Courser will point to the next address.
7. So, nest time no need to write the address in the command register.
Figure 12-5. Cursor Addresses for Some LCDs

16 x 2 LCD 80 81 82 83 84 85 86 through 8F
C0 C1 C2 C3 C4 C5 C6 through CF
20 x 1 LCD 80 81 82 83 through 93
20 x 2 LCD 80 81 82 83 through 93
C0 C1 C2 C3 through D3
20 x 4 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
40 x 2 LCD 80 81 82 83 through A7
C0 C1 C2 C3 through E7

Note: All data is in hex.


LCD INTERFACING
Sending Data/Commands to LCDs with Time Delay

Question: Write a program to display “NO” on LCD from line 1 position 4.

Solution: To send any of the commands 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. This is shown in the code below.

; 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 0000H
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 blinking
ACALL COMNWRT ;call command subroutine ACALL DELAY ;give LCD some time
ACALL DELAY ;give LCD some time MOV A,#’N’ ;display letter N
MOV A,#01H ;clear LCD ACALL DATAWRT ;call display subroutine
ACALL COMNWRT ;call command subroutine ACALL DELAY ;give LCD some time
ACALL DELAY ;give LCD some time MOV A,#’O’ ;display letter O
MOV A,#06H ;shift cursor right ACALL DATAWRT ;call display subroutine
ACALL COMNWRT ;call command subroutine AGAIN: SJMPAGAIN ;stay here
ACALL DELAY ;give LCD some time
MOV A,#84H ;cursor at line 1, position 4
ACALL COMNWRT ;call command subroutine
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 ;it must be 450 ns wide
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
DATAWRT: ;write data to LCD HERE: DJNZ R4,HERE ;stay until R4 becomes 0
MOV P1,A ;copy reg A to port 1 DJNZ R3,HERE2
SETB P2.0 RET
;RS=1 for data
CLR P2.1 END
;R/W=0 for write
SETB P2.2 ;E=1 for high pulse ;it must be 450 ns wide
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
Sending Information to LCD Using MOVC
Question: Write a program to display “HELLO” on LCD from line 1 position 6.

Solution: ORG 0000H ;P1.0-P1.7 = D0-D7, P2.0 = RS, P2.1 = R/W, P2.2=E
MOVA,#38H
ACALL COMNWRT
ACALL DELAY
MOVA,#0EH
ACALL COMNWRT
ACALL DELAY
MOVA,#01H
ACALL COMNWRT
ACALL DELAY
MOVA,#06H
ACALL COMNWRT
ACALL DELAY
MOVA,#86H
ACALL COMNWRT
Interfacing Keypad with 8051
 A keypad is used to take input from the user for further processing.
 Here a 4 by 3 matrix keypad consisting of switches arranged in rows and columns is
interfaced to the microcontroller.
 A 16 by 2 LCD is also interfaced for displaying the output
Interfacing concept
 The interfacing concept of keypad is very simple. Every number of keypad is
assigned two unique parameters that are row and column (R, C).
 Hence every time a key is pressed the number is identifying by detecting the row
and column numbers of keypad.
 Initially all the rows are set/grounded to zero (‘0’) by the controller and columns are
scanned to check if any key is pressed. In case of no key is pressed the output of all
columns will be high (‘1’).

 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
Interfacing Keypad with 8051
Figure 12-6. Matrix Keyboard Connection to Ports
Interfacing Keypad with 8051
Figure 12-7. Flowchart for Program 12-4
Assembly Language Program to interface 2X2 matrix Keyboard with 8051
MOV P2, #0FFH ; make P2 input (column) ScanRow: MOV P1, #11111110B ; ground row 0
L1: MOV P1, #00H ; make P2 output (row) MOV A, P2; read all columns
MOV A, P2 ; read all column ANL A, #03H
CJNE A, #03H, Row_0; key row 0, find column
ANL A, #03H; MOV P1, #11111101B ; ground row 1
CJNE A, #03H, L2 MOV A, P2
SJMP L1 ANL A, #03H
L2: ACALL Delay ; call 20 ms delay CJNE A, #03H, Row_1; key row 1, find column
MOV A, P2 ; LJMP L1
ANL A, #03H ; check if any key is pressed
CJNE A, #03H, ScanRow ; key pressed
SJMP L1 scan row
Assembly Language Program to interface 2X2 matrix Keyboard with 8051
Row_0: MOV DPTR, #KCODE0 ; set DPTR = start of Row 0
SJMP FIND ; Find column key belongs to
Row_1: MOV DPTR, #KCODE1
SJMP FIND
FIND: RRC A ; Check any carry bit is low
JNC MATCH ; if zero get ASCII code
INC DPTR
SJMP FIND ; keep searching

MATCH: CLR A
MOVC A, @A+DPTR ; get ASCII from table
MOV P0, A ; display pressed key
LJMP L1

ORG 300H
KCODE0: DB ‘0’, ‘1’ ;ROW 0
KCODE1: DB ‘2’, ‘3’ ; ROW 1
END
Assembly Language Program to interface 4X4 matrix Keyboard with 8051
ScanRow: MOV P1, #11111110B ; ground row 0
MOV P2, #0FFH ; make P2 input (column) MOV A, P2; read all columns
L1: MOV P1, #00H ; make P2 output (row) ANL A, #0FH
MOV A, P2 ; read all column CJNE A, #0FH, Row_0; key row 0, find column
MOV P1, #11111101B ; ground row 1
ANL A, #0FH;
MOV A, P2
CJNE A, #0FH, L2 ANL A, #0FH
SJMP L1 CJNE A, #0FH, Row_1; key row 1, find column
MOV P1, #11111011B ; ground row 2
L2: ACALL Delay ; call 20 ms delay MOV A, P2
ANL A, #0FH
CJNE A, #0FH, Row_2; key row 2, find column
MOV A, P2 ;
MOV P1, #11110111B ; ground row 2
ANL A, #0FH ; check if any key is pressed MOV A, P2
CJNE A, #0FH, ScanRow ; key pressed ANL A, #0FH
SJMP L1 scan row CJNE A, #0FH, Row_3 ; key row 3, find column
LJMP L1
Assembly Language Program to interface 4X4 matrix Keyboard with 8051
Row_0: MOV DPTR, #KCODE0 ; set DPTR = start of Row 0
SJMP FIND ; Find column key belongs to
Row_1: MOV DPTR, #KCODE1
SJMP FIND
Row_2: MOV DPTR, #KCODE2
SJMP FIND
Row_3: MOV DPTR, #KCODE3
SJMP FIND

FIND: RRC A ; Check any carry bit is low


JNC MATCH ; if zero get ASCII code
INC DPTR
SJMP FIND ; keep searching

MATCH: CLR A
MOVC A, @A+DPTR ; get ASCII from table
MOV P0, A ; display pressed key
LJMP L1
Interfacing ADC with 8051
 ADCs (analog-to-digital converters) are among the most widely used devices for data
acquisition
 A physical quantity, like temperature, pressure, humidity, and velocity, etc., is
converted to electrical (voltage, current) signals using a device called a transducer, or
sensor
 An analog-to-digital converter is used to translate the analog signals to digital
numbers, so microcontroller can read them

ADC804
 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. It 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
Interfacing ADC with 8051
The voltage at Vref/2 (pin9) of ADC0804 can be externally adjusted to convert smaller input voltage spans to
full 8 bit resolution. Vref/2 (pin9) left open means input voltage span is 0-5V and step size is 5/255=19.6V.
Interfacing ADC with 8051
 The circuit initiates the ADC to convert a given analogue
input , then accepts the corresponding digital data and
displays it on the LED array connected at P0.
 For example, if the analogue input voltage Vin is 5V then
all LEDs will glow indicating 11111111 in binary which is
the equivalent of 255 in decimal.
 AT89s51 is the microcontroller used here. Data out pins
(D0 to D7) of the ADC0804 are connected to the port
pins P1.0 to P1.7 respectively.
 LEDs D1 to D8 are connected to the port pins P0.0 to
P0.7 respectively. Resistors R1 to R8 are current limiting
resistors.
 In simple words P1 of the microcontroller is the input
port and P0 is the output port.
 Control signals for the ADC (INTR, WR, RD and CS) are
available at port pins P3.4 to P3.7 respectively. Resistor
R9 and capacitor C1 are associated with the internal
clock circuitry of the ADC.
 Preset resistor R10 forms a voltage divider which can be
used to apply a particular input analogue voltage to the
ADC.
 Push button S1, resistor R11 and capacitor C4 forms a
debouncing reset mechanism.
 Crystal X1 and capacitors C2,C3 are associated with the
clock circuitry of the microcontroller.
Interfacing ADC with 8051
Interfacing DAC with 8051
 Digital to Analog converter (DAC) is a device, that is widely used for converting digital pulses to analog
signals.
 There are two methods of converting digital signals to analog signals. These two methods are binary
weighted method and R/2R ladder method.
 MC1408 (DAC0808) Digital to Analog Converter used. This chip uses R/2R ladder method. This method
can achieve a much higher degree of precision.
 DACs are judged by its resolution. The resolution is a function of the number of binary inputs. The most
common input counts are 8, 10, 12 etc. Number of data inputs decides the resolution of DAC. So if there
are n digital input pin, there are 2n analog levels. So 8 input DAC has 256 discrete voltage levels
DAC0808
 Digital inputs are converted to current.
 The output current is known as Iout by connecting a resistor to the output to convert into voltage.
 The total current provided by the Iout pin is basically a function of the binary numbers at the input
pins D0 - D7 (D0 is the LSB and D7 is the MSB) of DAC0808 and the reference current Iref provided into
the pin 14
 Practically Iref current input is isolated by connecting it to an Op-Amp with Rf = 5KΩ as feedback
resistor
Interfacing DAC with 8051
Interfacing DAC with 8051

Generating a Sine Wave


using DAC and 8051
Write a program to send data in DAC to generate a sine wave using 8051.
Write a program to send data in DAC
to generate a square wave, triangular
wave and staircase wave using 8051.
Interfacing Sensors and Signal Conditioning Circuit with 8051
 A sensor is a device that produces an output signal for the purpose of sensing a
physical phenomenon
 A thermistor responds to temperature change by changing resistance, but its
response is not linear
 The complexity associated with writing software for such nonlinear devices has
led many manufacturers to market the linear temperature sensor

 Sensors of the LM34/LM35 series are precision integrated-circuit temperature


sensors whose output voltage is linearly proportional to the Fahrenheit/Celsius
temperature
 The LM34/LM35 requires no external calibration since it is inherently calibrated
 It outputs 10 mV for each degree of Fahrenheit/Celsius temperature

 Signal conditioning is a widely used term in the world of data acquisition


 It is the conversion of the signals (voltage, current, charge, capacitance, and
resistance) produced by transducers to voltage, which is sent to the input of an A-D
converter
 Signal conditioning can be a current-to voltage conversion or a signal amplification
 The thermistor changes resistance with temperature, while the change of resistance
must be translated into voltage in order to be of any use to an ADC

You might also like