Interfacing
Interfacing
•Assembling and running an 8051 program, Data types and Assembler directives,
•Interfacing: LCD & Keyboard Interfacing, ADC, DAC & Sensor Interfacing,
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
LCD AND KEYBOARD INTERFACING
• 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 refreshing the LCD
• Ease of programming for characters and graphics
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
PIN DESCRIPTION OF LCD
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
LCD CONNECTIONS
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
• 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
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
• 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
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
• MOV A,#’N’ ;display letter N
• ACALL DATAWRT ;call display subroutine
• ACALL DELAY ;give LCD some time
• MOV A,#’O’ ;display letter O
• 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
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
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
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
• END
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
KEYBOARD INTERFACING
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
KEYBOARD PROGRAM
This program sends the ASCII code for pressed key to P0.1
;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
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
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
KEYBOARD PROGRAM
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
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET
KEYBOARD PROGRAM
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 Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal,
ECE Department, GCET
KEYBOARD PROGRAM
ORG 300H
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
Unit-5(LCD & KeyBoard Interfacing) , Asst. Prof. Sakshi Mittal, ECE Department, GCET