100% found this document useful (1 vote)
132 views

LCD Keyboard Interfacing: Unit-V

The document discusses interfacing a keyboard and LCD to a microcontroller. It provides code to initialize the LCD, write characters to it, and includes timing diagrams for LCD read/write operations. It also includes code for scanning keyboard rows and reading pressed keys, debouncing the keys, and identifying the pressed row and column.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
132 views

LCD Keyboard Interfacing: Unit-V

The document discusses interfacing a keyboard and LCD to a microcontroller. It provides code to initialize the LCD, write characters to it, and includes timing diagrams for LCD read/write operations. It also includes code for scanning keyboard rows and reading pressed keys, debouncing the keys, and identifying the pressed row and column.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Microcontroller

Unit-V

LCD
KEYBOARD INTERFACING
VSS

VEE
VD

RS

D
R
D

3
1

5
3

6
1
2

4
5

9
7
8

1
1
0
1
1

2
1
3
1
VSS
VDD
VEE

INTERFACING
RS
R
W
E LCD
D0
D1
D2
D3
D4
D5
D6
D7
LCD Operation
LCD Pin Description
LCD Command Codes

LM016L
1
LCD
1
VSS
2
VDD
3
VEE
4
5 RS
RW
6
E
7
8 D0
9 D1
10 D2
11 D3
12 D4
13 D5
14 D6
D7
-
To send any commands to the LCD, make pin RS=0, for data, make RS=1. then
send a high-to-low pulse to the pin to enable the internal latch of theLCD.
; 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 000H
MOV A, #38H ; init LCD 2 lines, 5 x 7 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, #'H'
-
; display letter "N"
ACALL DATAWRT ; call display subroutine
ACALL DELAY ; give LCD some time
MOV A, #'I' ; display letter "O"
ACALL DATAWRT ; call display subroutine
AGAIN: SJMP AGAIN ; stay here

COMNWRT: ;send command to LCD


MOV P1, A ; copy register 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 register A to port1
SETB 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
DELAY: MOV R3, #50 ; 50 or higher for fast CPU,s
HERE2: MOV R4, #255 ; R4=255
HERE: DJNZ R4, HERE ; stay until R4 becomes 0
DJNZ R3, HERE2
RET
END.
-
To send any commands to the LCD, make pin RS=0, for data, make RS=1. then
send a high-to-low pulse to the pin to enable the internal latch of the LCD.
(Using the busy Flag).

; Check busy flag before sending data, command to LCD


; P1=data pin, P2.0=RS, P2.1=R/W, P2.2=E pins

MOV A, #38H ; INIT LCD 2lines, 5 x 7 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 ; issue 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
DELAY:
MOV R3, #50 ; 50 or higher for fast CPU,s
HERE2: MOV R4, #255 ; R4=255
HERE1: DJNZ R4, HERE1 ; stay until R4 becomes 0
DJNZ R3, HERE2
RET

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
JB P1.7, BACK ; stay until busy flag=0
RET
END
LCD Timing for Read
LCD Timing for Write
-
The program shows how to use the MOVC instruction to send data and
commands to an LCD.
; calls a time delay before sending next data/command
; P1.0-P1.7=D0-D7, P2.0=RS, P2.1=R/W, P2.2=E pins
ORG 0
MOV DPTR, #MYCOM
C1: CLR A
MOVC A, @A+DPTR
ACALL COMNWRT ; Call command subroutine
ACALL DELAY ; give LCD sometime
JZ SEND_DAT
INC DPTR
SJMP C1
SEND_DAT:
MOV DPTR, #MYCOM
D1: CLR A
MOVC A, @A+DPTR
ACALL DATAWRT ; Call command subroutine
ACALL DELAY ; give LCD sometime
JZ AGAIN
INC DPTR
SJMP D1
AGAIN: SJMP AGAIN
COMNWRT: -
;send command to LCD
MOV P1, A ; copy register 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 register A to port1
SETB 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
DELAY: MOV R3, #50 ; 50 or higher for fast CPU,s
HERE2: MOV R4, #255 ; R4=255
HERE: DJNZ R4, HERE ; stay until R4 becomes 0
DJNZ R3, HERE2
RET
COMNWRT: ;send command to LCD
ORG 300H
MYCOM: DB 38H,0EH,01,06,84H,0 ; commands and null
MYDATA:DB “HELLO”, 0 ; data and null
END
1

3
A 1 2 3
B 4 5 6

KEYBOARD C

D
7 8

0
9

#
INTERFACING
1

A 1 2 3
B 4 5 6
C 7 8 9
D
0 #
Keyboard Interfacing
To send any commands to the LCD, make pin RS=0, for data, make RS=1. then
send a high-to-low pulse to the pin to enable the internal latch of theLCD.
; 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

MOV P2,#0FFH ;make P2 an input port


K1: MOV P1,#0 ;ground all rows at once
MOV A,P2 ;read all columns
;(ensure keys open)
ANL A,00001111B ;masked unused bits
CJNE A,#00001111B,K1 ;till all keys release
K2: ACALL DELAY ;call 20 msec delay
MOV A,P2 ;see if any key is pressed
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER ;key pressed, find row
SJMP K2 ;check till key pressed
OVER: ACALL DELAY ;wait 20 msec debounce time
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
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
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
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
ROW_3: MOV DPTR,#KCODE3 ;set DPTR=start of row 3
FIND: RRC A ;see if any CY bit low
JNC MATCH ;if zero, get ASCII code
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
;ASCII LOOK-UP TABLE FOR EACH ROW
ORG 300H
KCODE0: DB ‘0’,’1’,’2’,’3’ ;ROW 0
KCODE1: DB ‘4’,’5’,’6’,’7’ ;ROW 1
KCODE2: DB ‘8’,’9’,’A’,’B’ ;ROW2
KCODE3: DB ‘C’,’D’,’E’,’F’ ;ROW 3
END

You might also like