Sunteți pe pagina 1din 20

INTERFACING LCD

Prof. B.M.DAXINI
Instrumentation & Control Dept
AITS Rajkot

Prof.B.M.Daxini IC Dept
INTERFACING LCD TO 8051 LCD Operation

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

Prof.B.M.Daxini IC Dept
LCD Pin Descriptions
PIN SYMBOL I/O Description
1 VSS -- Ground
2 VCC -- +5V Power Supply
3 VEE -- Power Supply to Control Contrast
4 RS I RS=0 to select Command Reg
RS=1 to select Data Reg
5 R/W I R/W=0 for Write
R/W=1 for Read
6 E I/O Enable (H to L pulse 450 ns)
7 DB0 I/O The 8-bit data bus
8 DB1 I/O The 8-bit data bus
9 DB2 I/O The 8-bit data bus
10 DB3 I/O The 8-bit data bus
11 DB4 I/O The 8-bit data bus
12 DB5 I/O The 8-bit data bus
13 DB6 I/O The 8-bit data bus
14 DB7 I/O The 8-bit data bus

Prof.B.M.Daxini IC Dept
LCD Command Code
Code (hex) Command to LCD Instruction
Register
1 Clear display screen

2 Return home

4 Decrement cursor (shift cursor to left)

5 Shift display right

6 Increment cursor (shift cursor to right)

7 Shift display left

8 Display off, cursor off

A Display off, cursor on

C Display on, cursor off

Prof.B.M.Daxini IC Dept
LCD Command Code (Continue)
Code(hex) Command to Instruction Register

E Display on, cursor blinking

F Display on, cursor blinking

10 Shift cursor position to left

14 Shift cursor position to right

1B Shift the entire display to the left

1C Shift the entire display to the right

80 Force cursor to beginning to 1st line

C0 Force cursor to beginning to 2nd line

38 2 lines and 5x7 matrix

Prof.B.M.Daxini IC Dept
INTERFACING LCD TO

There are two ways to send characters (command/data) to the LCD.


1) Use a delay before sending the next one.
2) Use of busy flag to see if the LCD is ready for the next one.

Prof.B.M.Daxini IC Dept
Sending Codes and Data to LCDs with Time Delay
LCD_DATA EQU PORTD
LCD_CTRL EQU PORTB
RS EQU RB0
RW EQU RB1
EN EQU RB2

CLRS TRISD ; PORT D = OUTPUT


CLRF TRISB ; PORT B = OUTPUT
BCF LCD_CTRL,EN ; enable idle low
CALL DELAY ; wait for initialization
MOVLW 0X38 ; initialize LCD 2 lines, 5X7 matrix
CALL COMNWRT ; call command subroutine
CALL DELAY ; give LCD sometime
MOVLW 0X0E ; display ON, cursor ON
CALL COMNWRT ; call command subroutine
CALL DELAY ; give LCD some time
MOVLW 0X01 ; clear LCD
CALL COMNWRT ; call command subroutine
CALL DELAY ; give LCD some time
MOVLW 0X06 ; shift cursor Right

Prof.B.M.Daxini IC Dept
Sending Codes and Data to LCDs with Time Delay (continue)
CALL COMNWRT ; call command subroutine
CALL DELAY ; give LCD some time
MOVLW 0X84 ; cursor at line 1 and position 4
CALL COMNWRT ; call command subroutine
CALL DELAY ; give LCD some time
MOVLW AN ; display letter N
CALL DATAWRT ; call display subroutine
CALL DELAY ; give LCD some time
MOVLW AO ; display letter O
CALL DATAWRT ; call display subroutine
AGAIN BRA AGAIN ; stay here

COMNWRT ; Send command to LCD


MOVWF LCD_DATA ; copy WREG to LCD DATA pin
BCF LCD_CTRL,RS ; RS = 0 for command
BCF LCD_CTRL,RW ; R/W = 0 for write
BSF LCD_CTRL,EN ; E = 1 for high pulse
CALL DELAY ; make a wide En pulse
BCF LCD_CTRL,EN ; E = 0 for H-to-L pulse
RETURN

Prof.B.M.Daxini IC Dept
Sending Codes and Data to LCDs with Time Delay (continue)
DATAWRT ; Write data to LCD
MOVWF LCD_DATA ; copy WREG to LCD DATA pin
BSF LCD_CTRL,RS ; RS = 1 for data
BCF LCD_CTRL,RW ; R/W = 0 for write
BSF LCD_CTRL,EN ; E = 1 for high pulse
CALL DELAY ; make a wide En pulse
BCF LCD_CTRL,EN ; E = 0 for H-to-L pulse
RETURN

DELAY AS DONE PREVIOUSLY

Prof.B.M.Daxini IC Dept
Sending Codes and Data to LCDs with Busy Flag
We use RS = 0 to read the busy flag bit to see if the LCD is ready to receive information. The busy flag is D7
and can be read when R/W = 1and RS = 0 as follows:

If R/W = 1, RS = 0 and D7 (busy flag) = 1, the LCD is busy taking care of internal operations and will not
accept any new information.

When R/W = 1, RS = 0 and D7 (busy flag) = 0,the LCD is ready to receive new information.

LCD_DATA EQU PORTD


LCD_CTRL EQU PORTB
RS EQU RB0
RW EQU RB1
EN EQU RB2
CLRS TRISD ; PORT D = OUTPUT
CLRF TRISB ; PORT B = OUTPUT
BCF LCD_CTRL,EN ; enable idle low
CALL DELAY ; wait for initialization
MOVLW 0X38 ; initialize LCD 2 lines, 5X7 matrix
CALL COMMAND ; call command subroutine

Prof.B.M.Daxini IC Dept
Sending Codes and Data to LCDs with Busy Flag (continue)
CALL DELAY ; give LCD sometime
MOVLW 0X0E ; display ON, cursor ON
CALL COMMAND ; call command subroutine
CALL READY ; is LCD ready?
MOVLW 0X01 ; clear LCD command
CALL COMMAND ; call command subroutine
CALL READY ; is LCD ready?
MOVLW 0X06 ; shift cursor right
CALL COMMAND ; call command subroutine
CALL READY ; is LCD ready?
MOVLW 0X86 ; cursor line 1, pos 6
CALL COMMAND ; call command subroutine
CALL READY ; is LCD ready?
MOVLW AN ; display letter N
CALL DATA_DISPLAY ; call display subroutine
CALL READY ; is LCD ready?
MOVLW AO ; display letter O
CALL DATA_DISPLAY ; call display subroutine
HERE BRA HERE ; stay here
Sending Codes and Data to LCDs with Busy Flag
COMMAND
MOVWF LCD_DATA ; issue command code
BCF LCD_CTRL,RS ; RS = 0 for command
BCF LCD_CTRL,RW ; R/W = 0 for write
BSF LCD_CTRL,EN ; E = 1 for high pulse
CALL DELAY ; make a wide En pulse
BCF LCD_CTRL,RS ; E = 0 for H-to-L pulse
RETURN

DATA_DISPLAY
MOVWF LCD_DATA ; issue command code
BSF LCD_CTRL,RS ; RS = 1 for data
BCF LCD_CTRL,RW ; R/W = 0 for write
BSF LCD_CTRL,EN ; E = 1 for high pulse
CALL DELAY ; make a wide En pulse
BCF LCD_CTRL,RS ; E = 0 for H-to-L pulse
RETURN

Prof.B.M.Daxini IC Dept
Sending Codes and Data to LCDs with Busy Flag
READY
SETF TRISD ; make PORTD input port for LCD data
BCF LCD_CTRL,RS ; RS = 0 access command reg
BSF LCD_CTRL,RW ; R/W = 2 read command reg
BCF LCD_CTRL,EN ; E = 0 for L-to-H pulse
CALL DELAY ; make a wide En pulse
BSF LCD_CTRL,EN ; E = 1 for L-to-H pulse
BTFSC LCD_DATA,7 ; stay until busy flag = 0
BRA BACK ;
CLRF TRISD ; make PORTD output port for LCD data
RETURN

END
LCD Datasheet
One can put data at any location in the LCD and the following shows address locations
and how they are accessed

RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0


0 0 1 A A A A A A A

AAAAAAA=000_0000 to 010_0111 for line1


AAAAAAA=100_0000 to 110_0111 for line2

LCD Addressing for the LCDs of 402 size

DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0


Line 1 (Min) 1 0 0 0 0 0 0 0
Line 1 (Max) 1 0 1 0 0 1 1 1
Line 2 (Min) 1 0 0 0 0 0 0 0
Line 2 (Max) 1 0 1 0 0 1 1 1

Prof.B.M.Daxini IC Dept
LCD TIMING DIAGRAM FOR READ OPERATION

Prof.B.M.Daxini IC Dept
LCD TIMING DIAGRAM FOR WRITE OPERATION

Prof.B.M.Daxini IC Dept
Sending Codes and Data to LCD using TBLRD instruction
LCD_DATA EQU PORTD
LCD_CTRL EQU PORTB
RS EQU RB0
RW EQU RB1
EN EQU RB2

CLRS TRISD ; PORT D = OUTPUT


CLRF TRISB ; PORT B = OUTPUT
BCF LCD_CTRL,EN ; enable idle low
CALL DELAY ; wait for initialization
MOVLW UPPER(MYCOM)
MOVWF TBLPTRU
MOVLW HIGH(MYCOM)
MOVWF TBLPTRH
MOVLW LOW(MYCOM)
MOVWF TBLPTRL

C1 TBLRD*+
MOVF TABLAT,W

Prof.B.M.Daxini IC Dept
IORLW 0X0
BZ SEND_DAT
CALL COMNWRT
CALL DELAY
BRA C1

SEND_DAT MOVLW UPPER(MYDATA)


MOVWF TBLPTRU
MOVLW HIGH(HIDATA)
MOVWF TBLPTRH
MOVLW LOW(MYDATA)
MOVWF TBLPTRL

DT1 TBLRD*+
MOVF TABLAT,W
IORLW 0X0
BZ OVER
CALL DATAWRT
CALL DELAY
BRA DT1
OVER BRA OVER
COMNWRT ; Send command to LCD
MOVWF LCD_DATA ; copy WREG to LCD DATA pin
BCF LCD_CTRL,RS ; RS = 0 for command
BCF LCD_CTRL,RW ; R/W = 0 for write
BSF LCD_CTRL,EN ; E = 1 for high pulse
CALL DELAY ; make a wide En pulse
BCF LCD_CTRL,EN ; E = 0 for H-to-L pulse
RETURN

DATAWRT ; Write data to LCD


MOVWF LCD_DATA ; copy WREG to LCD DATA pin
BSF LCD_CTRL,RS ; RS = 1 for data
BCF LCD_CTRL,RW ; R/W = 0 for write
BSF LCD_CTRL,EN ; E = 1 for high pulse
CALL DELAY ; make a wide En pulse
BCF LCD_CTRL,EN ; E = 0 for H-to-L pulse
RETURN
DELAY --------------------
ORG 500H
MYCOM DB 0X38,0X0E,0X01,0X06,0X84,0 ;commands and null
MYDATA DB HELLO,0 ; data and null

END

S-ar putea să vă placă și