Wednesday, August 25, 2010

Interfacing LCD to 8051

Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for debugging purpose. The most common type of LCD controller is HITACHI 44780 which provides a simple interface between the controller & an LCD. These LCD's are very simple to interface with the controller as well as are cost effective.
The most commonly used ALPHANUMERICdisplays are 1x16 (Single Line & 16 characters), 2x16 (Double Line & 16 character per line) & 4x20 (four lines & Twenty characters per line). 
The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines depends on the mode of operation. If operated in 8-bit mode then 8 data lines + 3 control lines i.e. total 11 lines are required. And if operated in 4-bit mode then 4 data lines + 3 control lines i.e. 7 lines are required. How do we decide which mode to use? It’s simple if you have sufficient data lines you can go for 8 bit mode & if there is a time constrain i.e. display should be faster then we have to use 8-bit mode because basically 4-bit mode takes twice as more time as compared to 8-bit mode.
 Pin SymbolFunction
 1 Vss Ground
 2 Vdd Supply Voltage
 3 Vo Contrast Setting
 4 RS Register Select
 5 R/W Read/Write Select
 6 En Chip Enable Signal
 7-14 DB0-DB7 Data Lines
 15 A/Vee Gnd for the backlight
 16 K Vcc for backlight

When RS is low (0), the data is to be treated as a command. When RS is high (1), the data being sent is considered as text data which should be displayed on the screen.
When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively reading from the LCD. Most of the times there is no need to read from the LCD so this line can directly be connected to Gnd thus saving one controller line.
The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required to latch the data. The LCD interprets and executes our command at the instant the EN line is brought low. If you never bring EN low, your instruction will never be executed.
For Contrast setting a 10K pot should be used as shown in the figure.
Display Data Ram (DDRAM) stores the display data. So when we have to display a character on LCD we basically write it into DDRAM. For a 2x16 LCD the DDRAM address for first line is from 80h to 8fh & for second line is 0c0h to 0cfh. So if we want to display 'H' on the 7th postion of the first line then we will write it at location 87h.
Now as you have noticed two types of data is given to the LCD data to be displayed, command or special instruction.So now let us write a subroutine for both the type of data

Data to be Displayed

·         lcd_datadisplay:
·         SETB RS                 //Telling the LCD that the data which is being send is to be displayed
·         MOV P1,A              //Character to be displayed is in Acc
·         SETB EN
·         CLR EN                 //High to Low pulse on EN to latch the data
·         CALL DELAY         //Delay so that LCD finishes its internal operations
·         ret 

Command or Special Instruction.

·         lcd_command:
·         CLR RS                   //Telling the LCD that the data which is being send is a command
·         MOV P1,A                           //Character to be displayed is in Acc
·         SETB EN
·         CLR EN                                  //High to Low pulse on EN to latch the data
·         CALL DELAY         //Delay so that LCD finishes its internal operations
·         ret 
Here I have used delay at the end of both the subroutines this is done to wait until the instruction is completely executed by the LCD. This will assure that our program gives the LCD the time it needs to execute instructions and also makes our program compatible with any LCD, regardless of how fast or slow it is.  
"*" - Not Used/Ignored.  This bit can be either "1" or "0" 

Set Cursor Move Direction:

ID - Increment the Cursor After Each Byte Written to Display if Set
S - Shift Display when Byte Written to Display  Enable Display/Cursor
D - Turn Display On(1)/Off(0) C - Turn Cursor On(1)/Off(0)
B - Cursor Blink On(1)/Off(0)  

Move Cursor/Shift Display

SC - Display Shift On(1)/Off(0) RL - Direction of Shift Right(1)/Left(0)  

Set Interface Length

DL - Set Data Interface Length 8(1)/4(0)
N - Number of Display Lines 1(0)/2(1)
F - Character Font 5x10(1)/5x7(0)  
Poll the "Busy Flag" BF - This bit is set while the LCD is processing  
Move Cursor to CGRAM/Display
A - Address 
Read/Write ASCII to the Display
D - Data  
The above Table shows the different commands for the LCD. I wont go into its details.
Before starting to display on LCD we need to Initialize it. Firstly we must tell the LCD whether we'll be using 8-bit or 4-bit mode. Also we will be telling the LCD that we need 5x8 character font. Both these options are selected using a single command i.e. 38h. So to activate both these options we must execute following instructions:
·         MOV A,#38h
·         CALL lcd_command 
Now that we have to Turn On the display & set the cursor option i.e. cursor ON/OFF & Cursor blinking ON/OFF for that we will use the command 0Eh i.e. Display On , Cursor ON but Cursor blinking OFF.
·         MOV A,#0Eh
·         CALL lcd_command 
And the last command we require is to configure the LCD in such a way that everytime we send a character to it the cursor position automatically increments by one & moves to right i.e. 06h.
·         MOV A,#06h
·         CALL lcd_command 
So the lcd_initialize contains the following instructions
·         lcd_initialize:
·         MOV A,#38h
·         CALL lcd_command
·         MOV A,#38h
·         CALL lcd_command
·         MOV A,#38h
·         CALL lcd_command
·         ret 
Similarly another important instruction that we require is Clearing the LCD i.e. 
·         lcd_clr:
·         MOV A,#01h
·         CALL lcd_command
·         ret
Now we know all the commands to display on LCD. Let us write a program that will display 'DNA TECHNOLOGY' on LCD.
·         ORG 0000h
·         CALL lcd_initialize
·         CALL lcd_clr
·         MOV A,#80h //Location from where Data is to be displayed
·         CALL lcd_command
·         MOV A,#'D'
·         CALL lcd_datadisplay
·         MOV A,#'N'
·         CALL lcd_datadisplay
·         MOV A,#'A'
·         CALL lcd_datadisplay
·         MOV A,#20h         //Hex value for blank space to be displayed
·         CALL lcd_datadisplay
·         MOV A,#'T'
·         CALL lcd_datadisplay
·         MOV A,#'E'
·         CALL lcd_datadisplay
·         MOV A,#'C'
·         CALL lcd_datadisplay
·         MOV A,#'H'
·         CALL lcd_datadisplay
·         MOV A,#'N'
·         CALL lcd_datadisplay
·         MOV A,#'O'
·         CALL lcd_datadisplay
·         MOV A,#'L'
·         CALL lcd_datadisplay
·         MOV A,#'O'
·         CALL lcd_datadisplay
·         MOV A,#'G'
·         CALL lcd_datadisplay
·         MOV A,#'Y'
·         CALL lcd_datadisplay
·         stop:
·         ajmp stop 

Debugging Procedure 

If the above program does not work then follow these steps:-
1) Check if the supply is correct.
2) If all connections are correct.
3) Increase the delay time.








No comments:

Post a Comment