Thursday, March 31, 2016

8051 LCD Interfacing

LCD can be used to display numbers, characters and graphics. Generally the following LCD pins are used to interface with 8051.

8051 LCD Interfacing

D0 – D7 (Data): The 8-bit data pins, DO – D7, are used to send information to the LCD or read the contents of the LCD’s internal registers.
RS (Register Select): There are two very important registers inside the LCD. The RS pin is used for their selection as follows. If RS = 0, the instruction command code register is selected, allowing the user to send a command such as clear display, cursor at home, etc. If RS = 1 the data register is selected, allowing the user to send data to be displayed on the LCD.

R/W (Read Write): R/W input allows the user to write information to the LCD or read information from it. R/W = 1 when reading; R/W = 0 when writing.

E (Enable): The enable pin is used by the LCD to latch information presented to its data pins. When data is supplied to data pins, a high-to-low pulse must be applied to this pin in order for the LCD to latch in the data present at the data pins.

Busy Flag: The busy flag is D7 and can be read when R/W = 1 and RS = 0, as follows: if R/W= 1, RS = 0. When D7 = 1 (busy flag = 1), the LCD is busy taking care of internal operations and will not accept any new information. When D7 = 0, the LCD is ready to receive new information.

To display letters and numbers, we send ASCII codes for the letters A – Z, a – z, and numbers 0 – 9 to data pins while making RS = 1.There are also instruction command codes that can be sent to the LCD. The command codes are in Hex.

Sending commands and data to LCD:
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. Sending command and data using delay function.

ORG 0H
MOV A, #38H ; initialize LCD command. 2 lines, 5x7 dot matrix display
ACALL COMMWR ; call command write subroutine
ACALL DELAY ; give LCD some time
MOV A, #0EH ; display on, cursor on command
ACALL COMMWR ; call command write subroutine
ACALL DELAY ; give LCD some time
MOV A, #01H ; clear LCD command
ACALL COMMWR ; call command write subroutine
ACALL DELAY ; give LCD some time
MOV A, #06H ; shift cursor right command
ACALL COMMWR ; call command write subroutine
ACALL DELAY ; give LCD some time
MOV A, #84H ; cursor at line 1 position 4
ACALL COMMWR ; call command write subroutine
ACALL DELAY ; give LCD some time
MOV A, #'N' ; display letter N
ACALL DATAWR ; call data write subroutine
ACALL DELAY ; give LCD some time
MOV A, #'O' ; display letter O
ACALL DATAWR ; call data write subroutine
ACALL DELAY ; give LCD some time
AGAIN :SJMP AGAIN ; stay here
; send command to LCD
COMMWR:
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 high to low pulse
RET
; send data to LCD
DATAWR:
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 high to low pulse
RET
; some delay
DELAY:MOV R3, #50
HERE2:MOV R4, #255
HERE:DJNZ R4, HERE; stay until R4 = 0
DJNZ R3, HERE2 ; stay until R3 = 0v
RET
END

Sending command and data using busy flag and delay:
ORG 0H
MOV A, #38H ; initialize LCD command. 2 lines, 5x7 dot matrix display
ACALL COMMWR ; call command write subroutine
MOV A, #0EH ; display on, cursor on command
ACALL COMMWR ; call command write subroutine
MOV A, #01H ; clear LCD command
ACALL COMMWR ; call command write subroutine
MOV A, #06H ; shift cursor right command
ACALL COMMWR ; call command write subroutine
MOV A, #84H ; cursor at line 1 position 4
ACALL COMMWR ; call command write subroutine
MOV A, #'N' ; display letter N
ACALL DATAWR ; call data write subroutine
MOV A, #'O' ; display letter O
ACALL DATAWR ; call data write subroutine
AGAIN :SJMP AGAIN ; stay here
; send command to LCD
COMMWR:
ACALL READY; is LCD ready to receive command?
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 high to low pulse
RET
; send data to LCD
DATAWR:
ACALL READY; is LCD ready to receive data?
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 high to low pulse
RET
;read command register and check busy flag
READY: SETB P1.7 ; make p1.7 input port
CLR P2.0 ; RS = 0 access command register
SETB P2.1; R/W = 1 read command register
BACK: CLR P2.2 ; E = 0 for high to low pulse
ACALL DELAY ; give LCD some time
SETB P2.2 ; E = 1 for high pulse
JB P1.7, BACK ; stay until busy flag = 0
RET
; some delay
DELAY:MOV R3, #50
HERE2:MOV R4, #255
HERE:DJNZ R4, HERE; stay until R4 = 0
DJNZ R3, HERE2 ; stay until R3 = 0v
RET
END

Send string to LCD:
ORG 0H
MOV DPTR, #MYCMD
LOOP1:CLR A
MOVC A, @A+DPTR
ACALL COMMWR
ACALL DELAY
JZ SEND_DATA
INC DPTR
SJMP LOOP1
SEND_DATA:MOV DPTR, #MYDATA
LOOP2:CLR A
MOVC A, @A+DPTR
ACALL DATAWR
ACALL DELAY
JZ SEND_DATA
INC DPTR
JZ AGAIN
SJMP LOOP2
AGAIN :SJMP AGAIN ; stay here
; send command to LCD
COMMWR:
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 high to low pulse
RET
; send data to LCD
DATAWR:
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 high to low pulse
RET
; some delay
DELAY:MOV R3, #50
HERE2:MOV R4, #255
HERE:DJNZ R4, HERE; stay until R4 = 0
DJNZ R3, HERE2 ; stay until R3 = 0v
RET
ORG 300H
MYCMD:DB 38H, 0EH, 01, 06, 84H, 0
MYDATA:DB "HELLO0"
END



Related topics:
8051 External Program Memory Interfacing   |   8051 External Data Memory Interfacing   |   8051 Memory Mapped IO   |   8051 LED Interfacing   |   8051 Switch Interfacing   |   8051 Keyboard Interfacing   |   8051 7-Segment Display Interfacing   |   8051 ADC Interfacing   |   8051 DAC Interfacing   |   8051 Relay Interfacing   |   8051 Sensor Interfacing   |   8051 Stepper Motor Interfacing   |   8051 DC Motor Interfacing

List of topics: 8051

No comments:

Post a Comment