Interfacing 16×2 LCD with 8051
Figure: LCD display 16X2
Pin Description:
16×2 Liquid Crystal Display which will display the 32 characters at a time in two
rows (16 characters in one row).
PIN
NAME FUNCTION
NO
1 VSS Ground pin
2 VCC Power supply pin of 5V
3 VEE Used for adjusting the contrast commonly attached to the
potentiometer.
4 RS Command or Data pass select
5 R/W for reading the data R/W pin should be high (R/W=1)
to write the data to LCD R/W pin should be low (R/W=0)
Interfacing 16×2 LCD with 8051
PIN
NAME FUNCTION
NO
6 E Enable pin is for starting or enabling the module. A high to low
pulse of about 450ns pulse is given to this pin.
7 DB0-DB7 Data bus
15 LED+ Back light of the LCD which should be connected to Vcc
16 LED- Back light of LCD which should be connected to ground.
Follow these simple steps for displaying a character or data
E=1 to 0; enable pin should be high to low
RS=1; Register select should be high
R/W=0; Read/Write pin should be low.
To send a command to the LCD just follows these steps:
E=1 to 0; enable pin should be high to low
RS=0; Register select should be low
R/W=0; Read/Write pin should be low.
Command:
COMMAND FUNCTION
0F For switching on LCD, blinking the cursor.
1 Clearing the screen
2 Return home.
4 Decrement cursor
6 Increment cursor
E Display on and also cursor on
Interfacing 16×2 LCD with 8051
COMMAND FUNCTION
80 Force cursor to beginning of the first line
C0 Force cursor to beginning of second line
38 Use two lines and 5x7 matrix
83 Cursor line 1 position 3
3C Activate second line
0C3 Jump to second line position 3
0C1 Jump to second line position1
Sample Code:
ORG 00H;INITIALIZATION
MOV SP, #07H
MOV PSW, #00H
MOV P1,#0FFH
LCD_IN: MOV A, #38H ;init. LCD 2 lines, 5x7 matrix
LCALL COMNWRT ;call command subroutine
LCALL DELAY ;give LCD some time
MOV A, #0EH ;display on, cursor on
LCALL COMNWRT ;call command subroutine
LCALL DELAY ;give LCD some time
MOV A, #01 ;clear LCD
LCALL COMNWRT ;call command subroutine
LCALL DELAY ;give LCD some time
MOV A, #06H ;shift cursor right
LCALL COMNWRT ;call command subroutine
LCALL DELAY ;give LCD some time
MOV A, #80H ;cursor at line 1 position 1
LCALL COMNWRT ;call command subroutine
LCALL DELAY ;give LCD some time
MOV A, #'H' ;display letter H
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
MOV A, #'E' ;display letter E
LCALL DATAWRT ;call display subroutine
Interfacing 16×2 LCD with 8051
LCALL DELAY
MOV A, #'L' ;display letter L
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
MOV A, #'L' ;display letter L
LCALL DATAWRT ;call display subroutine
MOV A, #'0' ;display letter O
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
;give LCD some time
MOV A, #0C0H ;cursor at line 1 position 1
LCALL COMNWRT ;call command subroutine
LCALL DELAY ;give LCD some time
MOV A, #'B' ;display letter B
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
MOV A, #'A' ;display letter A
LCALL DATAWRT ;call display subroutine
LCALL DELAY
MOV A, #'N' ;display letter N
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
MOV A, #'G' ;display letter G
LCALL DATAWRT ;call display subroutine
MOV A, #'L' ;display letter L
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
MOV A, #'A' ;display letter A
LCALL DATAWRT ;call display subroutine
LCALL DELAY
MOV A, #'D' ;display letter D
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
MOV A, #'E' ;display letter E
LCALL DATAWRT ;call display subroutine
LCALL DELAY
MOV A, #'S' ;display letter S
LCALL DATAWRT ;call display subroutine
LCALL DELAY ;give LCD some time
MOV A, #'H' ;display letter H
Interfacing 16×2 LCD with 8051
LCALL DATAWRT ;call display subroutine
SJMP EXIT
COMNWRT: ;send command to LCD
MOV P1, A ;copy reg A to port 1
CLR P3.3 ;RS=0 for command
CLR P3.4 ;R/W=0 for write
SETB P3.5 ;E=1 for high pulse
ACALL DELAY ;give LCD some time
CLR P3.5 ;E=0 for H-to-L pulse
RET
DATAWRT: ;write data to LCD
MOV P1, A ;copy reg A to port1
SETB P3.3 ;RS=1 for data
CLR P3.4 ;R/W=0 for write
SETB P3.5 ;E=1 for high pulse
ACALL DELAY ;give LCD some time
CLR P3.5 ;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
EXIT:
END