LCD Interfacing With 8051 and LCD With KEYPAD Interfacing With 8051

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5
At a glance
Powered by AI
The code samples provided are for interfacing an LCD display and keypad with an 8051 microcontroller. The LCD is initialized and characters/strings are written to specific positions on the display. A keypad scanning routine is also shown to read pressed keys and display the corresponding ASCII value on the LCD.

The code samples show how to initialize an LCD display and write text/characters to specific positions on the 2 line display. A keypad scanning routine is also provided to read pressed keys and display the ASCII value.

The LCD is initialized by sending control codes to set it up as a 2 line, 5x7 matrix display. Characters/strings are written by calling subroutines that set the data/command lines and pulse the enable signal. Delays are also added between writes for the LCD.

TASK 5

Submitted by

RAHUL TRIPATHY (15BEC0253)

Q1. LCD Interfacing:

code:

ORG 0H
MOV TMOD,#10H
MOV A,#38H ; INIT THE LCD
ACALL COMWRT
ACALL DELAY
MOV A,#0EH
ACALL COMWRT
ACALL DELAY
MOV A,#01H
ACALL COMWRT
ACALL DELAY
MOV A,#06H
ACALL COMWRT
ACALL DELAY
MOV A,#3CH ; TO ACTIVATE SECOND LINE
ACALL COMWRT
ACALL DELAY
MOV A,#0C0H
ACALL COMWRT
ACALL DELAY
; NOW WE ARE READY TO SEND THE DATA "CODE"
MOV A, #'C'
ACALL DATAWRT
ACALL DELAY
MOV A, #'O'
ACALL DATAWRT
ACALL DELAY
MOV A, #'D'
ACALL DATAWRT
ACALL DELAY
MOV A, #'E'
ACALL DATAWRT
ACALL DELAY
HERE: SJMP HERE

COMWRT:
MOV P1,A ; PORT P1 DATA LINE AND PORT P2 COMMAND LINE
CLR P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET
DATAWRT:
MOV P1,A ; PORT P1 DATA LINE AND PORT P2 COMMAND LINE
SETB P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET

DELAY:
;TIMER 1 IN MODE 1 MAXIMUM DELAY

MOV TH1,#0H
MOV TL1,#0H
SETB TR1
BACK: JNB TF1,BACK
CLR TF1
CLR TR1
RET
END

Q2. LCD and KEYPAD INTERFACING APPLICATION.

CODE:

ORG 0H
START:
ACALL LCDINIT
ACALL GETKEY
AGAIN: SJMP AGAIN
LCDINIT: 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,#80H ;cursor at line 1, pos. 4
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#'K' ;display letter N
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#'E' ;display letter O
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#'Y' ;display letter O
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#':' ;display letter O
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
RET
COMNWRT:MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLR P2.2
RET
DATAWRT:MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLR P2.2
RET
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
;Keyboard subroutine. This program sends the ASCII
;Code for pressed key to P0.1
;P0.4-P0.7 connected to rows, P0.0-P0.3 to column

GETKEY: MOV P0,#0FH


K1:MOV P0,#0FH
MOV A,P0
ANL A,#0FH
MOV P0,A
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,K1
K2:ACALL DELAY
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,OVER
SJMP K2
OVER:ACALL DELAY
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,OVER1
SJMP K2
OVER1:CLR P0.4 ;ROW1 SELECTED
SETB P0.5
SETB P0.6
SETB P0.7
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW0
CLR P0.5 ;ROW2 SELECTED
SETB P0.7
SETB P0.6
SETB P0.4
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW1
CLR P0.6 ;ROW3 SELECTED
SETB P0.7
SETB P0.5
SETB P0.4
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW2
CLR P0.7 ;ROW4 SELECTED
SETB P0.4
SETB P0.6
SETB P0.5
MOV A,P0
ANL A,#0FH
CJNE A,#0FH,ROW3
SJMP K2
MOV R0,#04H
ROW0:MOV DPTR,#KCODE0
SJMP FIND
ROW1:MOV DPTR,#KCODE1
SJMP FIND
ROW2:MOV DPTR,#KCODE2
SJMP FIND
ROW3:MOV DPTR,#KCODE3
FIND:RRC A
JNC MATCH
INC DPTR
DJNZ R0,FIND
MATCH:MOV A,#84H ;display pressed key
ACALL COMNWRT ;
ACALL DELAY
CLR A ;set A=0 (match is found)
MOVC A,@A+DPTR ;get ASCII from table
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
LJMP K1 ;ASCII LOOK-UP TABLE FOR EACH ROW
ORG 300H
KCODE0: DB 'F','B','8','4' ;ROW 0
KCODE1: DB 'E','A','7','3' ;ROW 1
KCODE2: DB 'D','0','6','2' ;ROW 2
KCODE3: DB 'C','9','5','1' ;ROW 3
END

You might also like