LCD
INTERFACE
1
LCD INTERFACE
Display units are the most important output devices
in embedded projects and electronics products.
LCD is finding widespread use replacing LEDs
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
2
LCD INTERFACE
216 LCD is one of the most used display unit.
As per the name the 2x16 has 2 lines with 16
characters on each lines.
It supports all the ASCII chars and is basically used
for displaying the alpha numeric characters.
Here each character is displayed in a matrix of 75
pixels.
Apart from alpha numeric chars it also provides the
3
LCD INTERFACE
4
LCD INTERFACE
❑
𝑾
5
LCD INTERFACE
6
LCD INTERFACE
7
LCD INTERFACE
The 216 LCD has two built in registers namely data
register and command register.
Command Register - stores the command
instructions given to the LCD. A command is an
instruction given to LCD to do a predefined task
like initializing, clearing the screen, setting the
cursor position, controlling display etc.
Data Register - stores the data to be displayed on
the LCD. The data is the ASCII value of the
character to be displayed on the LCD.
8
LCD INTERFACE
9
LCD INTERFACE
For programming LCD follow these steps:
• STEP1: Initialization of LCD.
• STEP2: Sending command to LCD.
• STEP3: Writing the data to LCD.
Step1: LCD initialization (common for almost all
applications)
1. Send 38H to the 8 bit data line for initialization
2. Send 0FH for making LCD ON, cursor ON and cursor
blinking ON.
3. Send 06H for incrementing cursor position.
10
LCD INTERFACE
Step2: Sending command to LCD
Send the command data to command register
Make low.
Make RS=0 if data byte is a command
Pulse E from high to low with some delay.
Repeat above steps for sending another command.
Step3: Writing data to LCD
Place data byte on the data register.
Make low.
make RS=1 if the data byte is a data to be displayed.
Pulse E from high to low with some delay.
Repeat above steps for sending another data.
11
LCD INTERFACE
Write an 8051 assembly language program to display
the message “H” and “I” on LCD display.
;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
12
LCD INTERFACE
ORG 0000H
MOV A, #38H ; INITIALIZE 2x16 LCD
ACALL COMNWRT ; call command subroutine
ACALL DELAY ; give LCD some time
MOV A, #0FH ; display on, cursor on, cursor blinking 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/ increment
ACALL COMNWRT ; call command subroutine
ACALL DELAY ; give LCD some time
13
LCD INTERFACE
MOV A, #84H ; cursor at line 1, pos. 4
ACALL COMNWRT ; call command
subroutine
ACALL DELAY ; give LCD some time
MOV A, #’H’ ; display letter H
ACALL DATAWRT ; call display
subroutine
ACALL DELAY ; give LCD some time
MOV A, #’I’ ; display letter I
ACALL DATAWRT ; call display
subroutine 14
LCD INTERFACE
COMNWRT: ; send command to
LCD
MOV P1, A ; copy reg A to
port 1
CLR P2.0 ; RS=0 for command
CLR P2.1 ; =0 for write
SETB P2.2 ; E=1 for high
pulse
ACALL DELAY ; give LCD some 15
LCD INTERFACE
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
ACALL DELAY ; give LCD some time
CLR P2.2 ; E=0 for H-to-L pulse
RET
16
LCD INTERFACE
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 ; stay until R3 becomes 0
RET
END
17
LCD INTERFACE
Write an 8051 assembly language program to display
the message “HELLO” on LCD display using DPTR.
; P1.0-P1.7=D0-D7, P2.0=RS, P2.1= , P2.2=E
ORG 0000H
MOV DPTR, #MYCOM
C1: CLR A
MOVC A,@A+DPTR
ACALL COMNWRT
ACALL DELAY
INC DPTR
JZ SEND_DAT
SJMP C1
18
LCD INTERFACE
SEND_DAT:
MOV DPTR, #MYDATA
D1: CLR A
MOVC A,@A+DPTR
ACALL DATAWRT
ACALL DELAY
INC DPTR
JZ AGAIN
SJMP D1
AGAIN: SJMP AGAIN
19
LCD INTERFACE
COMNWRT: ; send command to
LCD
MOV P1, A ; copy reg A to P1
CLR P2.0 ; RS=0 for command
CLR P2.1 ; =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 20
LCD INTERFACE
DATAWRT: ; write data to LCD
MOV P1, A ; copy reg A to port 1
SETB P2.0 ; RS=1 for data
CLR P2.1 ; =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
21
LCD INTERFACE
DELAY: MOV R3, #250 ; 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
ORG 300H
MYCOM: DB 38H, 0EH, 01, 06, 84H, 0 ; commands
and null
22
KEYPAD
INTERFACE
23
KEYPAD INTERFACE
Keyboards are organized in a matrix of rows and
columns
The CPU accesses both rows and columns through
ports
When a key is pressed, a row and a column make
contact
A 4x4 matrix connected to two ports
24
KEYPAD INTERFACE
25
KEYPAD INTERFACE
25
KEYPAD INTERFACE
It is the function of the microcontroller to scan the
keyboard continuously to detect and identify the key
pressed
To detect a pressed key, the microcontroller grounds
all rows by providing 0 to the output latch, then it
reads the columns
If the data read from columns is D3D2D1D0 = 1111,
no key has been pressed and the process continues
till key press is detected
If one of the column bits has a zero, this means that a 26
KEYPAD INTERFACE
For example, if D3D2D1D0 = 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
27
KEYPAD INTERFACE
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 it finds out which column the pressed
key belongs to
Identify the row and column of the pressed key for
(a) D3D2D1D0 = 1110 for the row, D3D2D1D0= 1011 for the
column
(b)D3D2D1D0 = 1101 for the row, D3D2D1D0= 0111 for the
column 28
KEYPAD INTERFACE
Program 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
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
3. To detect which row key press belongs to, it grounds one
row at a time, reading the columns each time
4. To identify the key press, it rotates the column bits, one
30
KEYPAD INTERFACE
31
KEYPAD INTERFACE
32
KEYPAD INTERFACE
32
HAPPENS IN FOUR STEPS
First, when no buttons are pressed, all of the column pins
are held HIGH
32
KEYPAD INTERFACE
When a button is pressed, the column pin is pulled LOW since the current
from the HIGH column flows to the LOW row pin
32
KEYPAD INTERFACE
The microcontroller now knows which column the button is in, so now it
just needs to find the row the button is in
It does this by switching each one of the row pins LOW, and at the same
time reading all of the column pins to detect which column pin goes to
LOW
32
KEYPAD INTERFACE
When the column pin goes LOW again, the microcontroller has found the
row pin that is connected to the button
32
2x2 Keypad Program
Write an ASM program to send the ASCII code for pressed
key to P0
P1.0-P1.1 connected to rows, P2.0-P2.1 to column
2x2 Keypad Program
ORG 0030H
MOV P2,#0FFH ;Make P2 an input port
K1: MOV P1,#0 ; Ground all rows at Once
MOV A,P2 ;Read all column
ANL A,#00000011B ;Masked unused bit
CJNE A, #00000011B,K1 ;Check all keys released
K2: ACALL DELAY ;Call 20ms delay
MOV A,P2 ; See if any key is pressed
ANL A,#00000011B ;Masked unused bit
CJNE A,#00000011B, OVER ;Key pressed, wait
SJMP K2 ; Check till key pressed
2x2 Keypad Program
OVER: MOV P1,#11111110B ;Ground ROW 0
MOV A,P2 ;Read all columns
ANL A,#00000011B ; Masked unused bit
CJNE A,#00000011B, ROW_0 ;ROW 0,find col
MOV P1,#11111101B ;Ground ROW 1
MOV A,P2 ; Read all columns
ANL A,#00000011B ; Masked unused bit
CJNE A,#00000011B, ROW_1 ; ROW 1, find the column
LJMP K2
2x2 Keypad Program
ROW_0: MOV DPTR, #KCODE0;
SJMP FIND
ROW_1: MOV DPTR, #KCODE1;
SJMP FIND
FIND: RRC A ; See if any CY bit low
JNC MATCH ; If zero get ASCII code
INC DPTR ; Point to next column
SJMP FIND ; Keep searching
MATCH: CLR A
MOVC A, @A+DPTR
MOV P0, A
LJMP K1 ;Loop
2x2 Keypad Program
ORG 300H
KCODE0: DB ‘1',‘0' ;ROW 0
KCODE1: DB ‘3',‘2' ;ROW 1
END
KEYPAD INTERFACE
Keyboard Program
;keyboard subroutine. This program sends the ASCII code for
pressed key to P0
;P1.0-P1.3 connected to rows, P2.0-P2.3 to column
MOV P2,#0FFH ;make P2 an input port
K1: MOV P1,#0 ;ground all rows at once
MOV A,P2 ;read all col(ensure keys open)
ANL A,00001111B ;masked unused bits
CJNE A,#00001111B,K1 ;till all keys release
KEYPAD INTERFACE
MOV A,P2 ;check key closure
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER1 ;key pressed,
find row
SJMP K2 ;if none, keep polling
KEYPAD INTERFACE
OVER1: MOV P1, #11111110B ;ground row 0
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_0 ;key row 0, find col.
MOV P1,#11111101B ;ground row 1
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_1 ;key row 1, find col.
MOV P1,#11111011B ;ground row 2
KEYPAD INTERFACE
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_2 ;key row 2, find col.
MOV P1,#11110111B ;ground row 3
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_3 ;key row 3, find col.
LJMP K2 ;if none, false input,
repeat
KEYPAD INTERFACE
ROW_0: MOV DPTR,#KCODE0 ;set
DPTR=start of row 0
SJMP FIND ;find col. Key belongs to
ROW_1: MOV DPTR,#KCODE1 ;set
DPTR=start of row
SJMP FIND ;find col. Key belongs to
ROW_2: MOV DPTR,#KCODE2 ;set
DPTR=start of row 2
SJMP FIND ;find col. Key belongs to
KEYPAD INTERFACE
INC DPTR ;point to next col. addr
SJMP FIND ;keep searching
MATCH: CLR A ;set A=0 (match is found)
MOVC A,@A+DPTR ;get ASCII from table
MOV P0,A ;display pressed key
LJMP K1
ORG 300H ;ASCII LOOK-UP TABLE FOR EACH ROW
KCODE0: DB ‘0’,’1’,’2’,’3’ ;ROW 0
KCODE1: DB ‘4’,’5’,’6’,’7’ ;ROW 1
KCODE2: DB ‘8’,’9’,’A’,’B’ ;ROW 2
KCODE3: DB ‘C’,’D’,’E’,’F’ ;ROW 3
END
KEYPAD & LCD INTERFACE