You are on page 1of 5

Microprocessor & Microcontroller Lab

Lab 07: LCD Interfacing

Lab 07: LCD Interfacing


Objective:
1. To interface the LCD with 8051 in 8 bit mode 2. Code 8051 for display of alphabets on LCD

Introduction:
The LCD available on the 8051 development board is 2x16 LCD which means that this LCD can display 16 characters in a line and there are 2 lines. So maximum 32 characters can be displayed on the 16x2 LCD at a time. Each character is displayed in a 5x7 pixel matrix. There are two registers in an LCD: 1) Data Register: Data register contains the character to be displayed on the LCD. Data is the ASCII value of the character to be displayed 2) Command / Instruction Register: Stores the command / instructions given to the LCD. A command or instruction performs the predefined task for the LCD. Some of the predefined tasks are clearing the screen, Moving / setting cursor, scrolling the display etc. Some of the commands / instructions are given in the Table 2.
Table 2: Pin Description of 16x2 LCD

PIN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

SYMBOL
Vss Vcc Vdd RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 LED+ LED-

I/O
I I I I/O I/O I/O I/O I/O I/O I/O I/O -

DESCRIPTION
Power supply (GND) Power supply (+5V) Contrast Adjustment 0 = Select command register 1 = Select data register 0 = Write to LCD 1 = Read from LCD A high to low pulse (minimum 450ns wide) is given when data is sent to data pins Data bit line 0 (LSB) Data bit line 1 Data bit line 2 Data bit line 3 Data bit line 4 Data bit line 5 Data bit line 6 Data bit line 7 (MSB) Backlight Vcc Backlight GND 0V

+5V

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Page 22

Microprocessor & Microcontroller Lab Pin No. 4, RS:

Lab 07: LCD Interfacing

Register Select (RS) Pin of LCD is used to select between Command Register and Data Register. If RS is set, Data Register is selected and if RS is zero, Command Register is selected. Pin No. 5, R/W: Read / Write (R/W) Pin of LCD is used to select whether to read from LCD or write something on the LCD. IF R/W pin is set, Read operation is performed. IF R/W is zero, a command / data can be written on the LCD. Pin No. 6, E: Enable (E) Pin of LCD is used to enable the LCD. When this bit goes from low to high, write operation is performed.

To send data on the LCD, data is first written to the data pins with R/W = 0 (to specify the write operation) and RS = 1 (to select the data register). A high to low pulse is given at E pin when data is sent. Each write operation is performed on the positive edge of the Enable signal. To send a command on the LCD, a particular command is first specified to the data pins with R/W = 0 (to specify the write operation) and RS = 0 (to select the command register). A high to low pulse is given at E pin when data is sent.
Pin No. 7-14, D0-D7: There are the data bits. An ASCII equivalent of the character to be displayed or a command required to be executed by the LCD is sent on these 8-data bits. Data pin8 (D7) of the LCD is busy flag and is read when R/W = 1 & RS = 0. When busy flag=1, it means that LCD is not ready to accept data since it is busy with the internal operations. Therefore before passing any data to LCD, its command register should be read and busy flag should be checked.
Table 2: Command list of 16x2 LCD

S No.
1 2 3 4 5 6 7 8 9 10

Command Description
Function Set: 8-bit, 1 Line, 5x7 Dots Function Set: 8-bit, 2 Line, 5x7 Dots Function Set: 4-bit, 1 Line, 5x7 Dots Function Set: 4-bit, 2 Line, 5x7 Dots Display off Cursor off (clearing display without clearing DDRAM content) Display on Cursor on Display on Cursor off Display on Cursor blinking Shift entire display left Shift entire display right

Command
0x30 0x38 0x20 0x28 0x08 0x0E 0x0C 0x0F 0x18 0x1C

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Page 23

Microprocessor & Microcontroller Lab 11 12 13 14 15 16 17 18 Move cursor left by one character Move cursor right by one character Clear Display (also clear DDRAM content) Return Home

Lab 07: LCD Interfacing 0x10 0x14 0x01 0x02 0x04 0x06 0x80 0xC0

During write operation only shift cursor left (decrement cursor) During write operation only shift cursor right (increment cursor) Force the cursor at first line, position zero Force the cursor at second line, position zero

Fig 1: 8051 Development Board - Layout of LCD Interface and 8051 Input Output Ports

C Code for LCD Programming:


//ThisprogramdisplaysthealphabetfromAtoZontwolinesofLCD #include<AT89x51.h> sbitRS=P1^0; //RegisterSelectPinonP1^0 sbitRW=P1^1; //ReadorWriteSelectPinonP1^1 sbitE=P1^2; //EnableSelectPinonP1^2 sbitBF=P2^7; //BusyFlagonMSBofDataPort chardata1; DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad Page 24

Microprocessor & Microcontroller Lab Lab 07: LCD Interfacing intready() //ForcheckingthattheLCDisreadyornot? { charch; for(ch=0;ch<100;ch++); return1; } //ReadyFunctionEnds voiddelay(unsignedintz)//Fordelay(Starts) { intx,y; for(x=0;x<z;x++) for(y=0;y<=1000;y++); } //DelayFunctionEnds intLCD_Run_Command(unsignedcharCOMMAND) { if(ready()) { P2=COMMAND; RS=0; delay(1); RW=0; delay(1); E=0; delay(1); E=1; delay(1); } return1; } voidLCD_Show(unsignedcharCHARACTER) { if(ready()) //ifLCDinternalcontrollerisnotbusy { P2=CHARACTER; RS=1; delay(1); RW =0; delay(1); E=0; delay(1); E=1; delay(1); } } voidLCD_Initialize() { LCD_Run_Command(0x38);//8datalines,twolines,Font5x7. LCD_Run_Command(0x0E);//Display=ON,Curson=ON,CursorBlonking=ON LCD_Run_Command(0x01);//Cleardisplayandreturncursortothehomeposition LCD_Run_Command(0x06);//Duringread/writeoperationonlycursor(nottext) //shouldmoveright } voidmain(void) { while(1) { DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad Page 25

Microprocessor & Microcontroller Lab Lab 07: LCD Interfacing delay(50); LCD_Initialize(); for(data1='A';data1<='N';data1++) LCD_Show(data1); //DisplaycharactersAtoNonLCDLine1 LCD_Run_Command(0xC0);//CursoratLine2,Position0 for(data1='O';data1<='Z';data1++) LCD_Show(data1); //DisplaycharacterOtoZonLCDLine2 delay(50); } }

Simulation:

Lab Task:

At the center of first line of LCD, show GROUP No. XX where XX is your group number. At the center of second line, display the first name of your group leader.

DEE, Faculty of Engineering & Technology, International Islamic University, Islamabad

Page 26

You might also like