Saturday, January 1, 2011

INTERFACING LCD TO 89C51

Liquid crystal display is very important device in embedded system. It offers high flexibility to user as he can display the required data on it. But due to lack of proper approach to LCD interfacing many of them fail. Many people consider LCD interfacing a complex job but according to me LCD interfacing is very easy task, you just need to have a logical approach. This page is to help the enthusiast who wants to interface LCD with through understanding. Copy and Paste technique may not work when an embedded system engineer wants to apply LCD interfacing in real world projects.You will be knowing about the booster rockets on space shuttle. Without these booster rockets the space shuttle would not launch in geosynchronous orbit.  
Similarly to understand LCD interfacing you need to have booster rockets attached! To get it done right you must have general idea howto approach any given LCD.This page will help you develop logical approach towards LCD interfacing.

First thing to begin with is to know what LCD driver/controller is used in LCD.Yes, your LCD is dumb it does not know to talk with your microcontroller. LCD driver is a link between the microcontroller and LCD. You can refer the datasheet of LCD to know the LCD driver for e.g. JHD 162A is name of LCD having driver HD44780U.You have to interface the LCD according to the driver specification. To understand the algorithm of LCD interfacing user must have datasheet of both LCD and LCD driver. Many people ignore the datasheets and end up in troubles. If you want to interface LCD successfully you must have datasheets.
Why people ignore datasheets? Most of us do not like to read 100 pages of datasheet. But for a accurate technical specification datasheets are must. I will show you a technique to manipulate a datasheet within minutes.
First thing to find out in datasheet is the features viz. operating voltage, type of interface, maximum speed for interface in MHz, size of display data RAM, number of pixels, bits per pixel, number of row and columns. You must have the pin diagram of LCD.Pin diagram of LCD driver can be omitted.
Study the type of communication protocol whether it is parallel or serial interface. Check how LCD discriminates data bytes and command bytes, which pins on LCD are used for communication. Study Interface timing diagram given in the datasheet.
From datasheet of LCD driver find out whether hardware reset is required at startup, what is the time of reset pulse, is it active low and which pins of LCD are to be toggled.
Major task in LCD interfacing is the initialization sequence. In LCD initialization you have to send command bytes to LCD. Here you set the interface mode, display mode, address counter increment direction, set contrast of LCD, horizontal or vertical addressing mode, color format. This sequence is given in respective LCD driver datasheet. Studying the function set of LCD lets you know the definition of command bytes. It varies from one LCD to another. If you are able to initialize the LCD properly 90% of your job is done.
Next step after initialization is to send data bytes to required display data RAM memory location. Firstly set the address location using address set command byte and than send data bytes using the DDRAM write command. To address specific location in display data RAM one must have the knowledge of how the address counter is incremented.

Schematic


Algorithm to send data to LCD:

1.Make R/W low
2.Make RS=0 ;if data byte is command
RS=1 ;if data byte is data (ASCII value)
3.Place data byte on data register
4.Pulse E (HIGH to LOW)
5.Repeat the steps to send another data byte

LCD Initialization:

This is the pit fall for beginners.Proper working of LCD depend on the how the LCD is initialized. We have to send few command bytes to initialize the lcd. Simple steps to initialize the LCD
1.Specify function set:
Send 38H for 8-bit,double line and 5x7 dot character format.
2.Display On-Off control:
Send 0FH for display and blink cursor on.
3.Entry mode set:
Send 06H for cursor in increment position and shift is invisible.
4. Clear display:
Send 01H to clear display and return cursor to home position.



Addresses of cursor position for 16x2 HD44780 LCD

line1 80H 81H 82H 83H 84H 85H 86H 87H 88H 89H 8AH 8BH 8CH 8DH 8EH 8FH 
line2 C0H C1H C2H C3H C4H C5H C6H C7H C8H C9H CAH CBH CCH CDH CEH CFH 

Assembly Language Code

; Interfacing LCD 16x2 in 4-bit mode.
; Port0 to higher nibble data pins of LCD
; Crystal 3.579545 MHz
; AT89C51
;P2.0 to RS pin
;P2.1 to Enable Pin


ORG 0000H
AJMP MAIN
ORG 0030H
MAIN:
MOV SP,#60H ;STACK POINTER 
ACALL LCD_INIT ;Initialize lcd
MOV DPTR,#MESSAGE1 
CALL LCD_STRING ;Display message on LCD
CALL NEXT_LINE ;Place cursor to;second Line
MOV DPTR,#MESSAGE2 
CALL LCD_STRING ;Display message on LCD
HERE: AJMP HERE


LCD_INIT: ;initialize LCD in 4-bit mode
ANL P0,#0F0H
CALL LOOP
MOV DPTR,#LCDCODE 
MOV A,#0H
MOV R6,#0H
MOV R7,#0H
CLR P2.0 ;RS COMMAND
NEXT: ;8-bit code is split into two 4-bit nibbles.
INC R6
MOVC A,@A+DPTR
MOV R7,A
ANL A,#0F0H
SWAP A
ANL P0,#0F0H
ORL P0,A
ACALL ENABLE ;PULSE E sending first nibble
MOV A,R7
ANL A,#0FH
ANL P0,#0F0H
ORL P0,A
ACALL ENABLE ;PULSE E sending second nibble
MOV A,R6
CJNE R6,#09H,NEXT
RET


LCD_STRING:
MOV P0,#00H
SETB P2.0 ;RS DATA 
MOV A,#00H
MOV R6,#00H
NC: ;checks the end of message string
MOVC A,@A+DPTR
CJNE A,#2FH,NC1
RET
NC1:
LCALL LCD_WRITE
INC R6
MOV A,R6
AJMP NC


LCD_WRITE:
SETB P2.0 ;RS DATA
CALL LO
RET


NEXT_LINE:
MOV P0,#00H
CLR P2.0 ;RS COMMAND 
MOV A,#0C0H
CALL LO
RET


LCD_CLEAR: ;This Subroutine is used to clear LCD
CALL DELAYL
ANL P0,#00H
MOV A,#01H
CLR P2.0 ; RS command 
LO: ;8-bit code is split into two 4-bit nibbles.
MOV R7,A
ANL A,#0F0H
SWAP A
ANL P0,#0F0H
ORL P0,A
CALL ENABLE
MOV A,R7
ANL A,#0FH
ANL P0,#0F0H
ORL P0,A
CALL ENABLE
RET




ENABLE: ;Give High-to-low pulse at enable pin
SETB P2.1
CALL DELAYL
CLR P2.1
CALL DELAYL
RET




;With respect to crystal frequency 3.579 MHz
DELAYL: ; 5ms DELAY
SETB PSW.4 ; SELECT BANK 2
MOV R7,#25
HDH:
MOV R6,#60
DJNZ R6,$
DJNZ R7,HDH
CLR PSW.4 ;DEFAULT BANK 
RET


LOOP: ;1 SEC DELAY
MOV R7,#100
LOOP1:
CALL DELAYL
CALL DELAYL
DJNZ R7,LOOP1
RET

;LCD INITIALIZING CODE (DO NOT DISTURB THIS)
LCDCODE:
DB 02H
DB 02H
DB 02H
DB 28H
DB 28H
DB 28H
DB 0CH
DB 06H
DB 01H




;DATA TO BE DISPLAYED
;Maximum message length = 16 characters.
;To notify end of message place '/' at the end.


MESSAGE1: DB "LCD INTERFACING /" ;Change Message1


MESSAGE2: DB " BY JAYANTH /" ;Change Message2


END


No comments:

Post a Comment