LCD

The most commonly used LCDs found in the market today are 1 Line, 2 Line or 4 Line LCDs which have only one controller and support at most 80 characters, whereas LCDs supporting more than 80 characters make use of 2 HD44780 controllers. Apart from displaying some simple static characters you can create animated text scripts and a lot more!

 

Let’s start playing with them, but you will have to wait a little as these things mentioned below will help you to create your own magical codes… 

 

Most LCDs with 1 controller has 14 Pins and 16 Pins (two extra pins are  for back-light LED connections). Pin description is shown in the table below. (We may also have 16 pins in 2 controllers, refer to the datasheet for exact details).

 

8051 LCD

 

8051 LCD Pin Description 

 

This may not match with the exact pin configuration for your LCD, check application circuit in datasheet for exact configuration.

We will discuss here 8 bit mode first (that is data transfer through all the 8 data pins), 4 bit mode will be discussed later on. 

Read/Write (RW): 

1.) RW= 0, the information is being written on LCD.

2.) RW=1, for reading from LCD. (Only one command that is “Get LCD status” is a read command all others are write command)

It is a control line. When RW is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively querying (or reading from) the LCD. Only one instruction ("Get LCD status") is a read command. All others are write commands--so RW will be low for majority of the time.

Registers:

There are two very important registers in the LCD. The RS pin is used for their selection. 

1.) RS= 0; The Instruction command code register, allows the user to send command such as clear display, cursor at home, etc.

2.) RS=1; the data register, allow user to send data to be displayed at LCD.

 

Enable (En) pin: is used to tell the LCD that we are sending it data. A high to low pulse (of minimum length 450ns) before sending any command/data to LCD.  

 

 

How many characters can we send to LCD?  

 

Display data RAM (DDRAM) stores display data represented in 8-bit character codes. Its extended capacity is 80 X 8 bits, or 80 characters. The area in display data RAM (DDRAM) that is not used for display can be used as general data RAM. So whatever you send on the DDRAM is actually displayed on the LCD. 

{For LCDs like 1x16, only 16 characters are visible, so whatever you write after 16 chars is written in DDRAM but is not visible to the user.}

 

8051 LCD DDRAM address

 

(DDRAM address for 2 line LCD)

 

 

How does the ASCII value change to characters?

 

The answer is CGROM i.e. character generator ROM,  is used to convert ASCII values send by µC into  5 x 8 dot or 5 x 10 dot character patterns from 8-bit character. (You can also add your own characters in the list!!)

 

 

How much time should there be in two consecutive Commands/Instructions?  

The first method is to create a delay between two consecutive commands or instructions. (Check the datasheet for exact time of execution of an instruction). This may not be very appropriate as the delay is not very accurate. 

  • BUSY FLAG (BF): The MSB of the LCD data bus (D7) act as busy flag. When BF = 1 means LCD is busy and will not accept next command or data and BF = 0 means LCD is ready for the next command or data to process. This flag is internally set by LCD  & can be monitored by µC for exact amount of delay. 

 

To read Busy Flag, the conditions RS = 0 and R/W = 1 must be met.

 

STEPS TO PROGRAM: 

1) Initialize the LCD.

2) Select the command or instruction register.

3) Set RW low (to write).

4) Send a high to low pulse on Enable pin.

5) Check if the LCD is busy.

6) Move to instruction or command function.

7) Repeat steps 4-7. 

 

 

Some common commands for LCD: 

 

8051 LCD commands

 

APPLICATION in 8051

 

Circuit Diagram - 

 

 

Sample Program

This program will display www.botskool.com on the LCD.

 

 

 

 

C Code                                             www.botskool.com

#include <AT89X52.H>

#define LCD_data P0

#define LCD_D7   P0_7

#define LCD_rs   P3_0

#define LCD_rw   P3_1

#define LCD_en   P3-2

 

 

void LCD_busy()

{

     LCD_D7   = 1;           //Make D7th bit of LCD as i/p

     LCD_en   = 1;           //Make port pin as o/p

     LCD_rs   = 0;           //Command register selected

     LCD_rw   = 1;           //Reading started

     while(LCD_D7)

    {  

        //read busy flag again and again till it becomes 0

         LCD_en   = 0;     //Enable Low to high

         LCD_en   = 1;

     }

}

void LCD_initialize()

{

     LCD_data = 0x38;     //Function set: 2 Line, 8-bit, 5x7 dots

     LCD_rs   = 0;        //Command register selected

     LCD_rw   = 0;        //We are writing in data register

     LCD_en   = 1;        //Enable High->Low

     LCD_en   = 0;

LCD_busy();          //Wait for LCD to process the command

 

 

     LCD_data = 0x0F;     //Display on, Cursor blinking command

     LCD_rs   = 0;        //Command register selected

     LCD_rw   = 0;        //We are writing in data register

     LCD_en   = 1;        //Enable High->Low

     LCD_en   = 0;

LCD_busy();          //Wait for LCD to process the command

 

    

     LCD_data = 0x01;     //Clear LCD

     LCD_rs   = 0;        //Command register selected

     LCD_rw   = 0;        //We are writing in data register

     LCD_en   = 1;        //Enable High->Low

     LCD_en   = 0;

LCD_busy();          //Wait for LCD to process the command

 

    

     LCD_data = 0x06;     //Entry mode, auto increment with no shift

     LCD_rs   = 0;        //Command register selected

     LCD_rw   = 0;        //We are writing in data register

     LCD_en   = 1;        //Enable High->Low

     LCD_en   =0 ;       

LCD_busy();

}

void LCD_command(unsigned char var)

{

     LCD_data = var;      //Function set: 2 Line, 8-bit, 5x7 dots

     LCD_rs   = 0;        //Command register selected

     LCD_rw   = 0;        //We are writing in instruction register

     LCD_en   = 1;        //Enable High->Low

     LCD_en   = 0;

 LCD_busy();          //Wait for LCD to process the command

}

// Using the above function is quite simple

// var will carry the command for LCD

// for example -

// LCD_command(0x80);

void LCD_senddata(unsigned char var)

{

     LCD_data = var;      //Function set: 2 Line, 8-bit, 5x7 dots

     LCD_rs   = 1;        //Data register selected

     LCD_rw   = 0;        //We are writing

     LCD_en   = 1;        //Enable High->Low

     LCD_en   = 0;

LCD_busy();          //Wait for LCD to process the command

}

void LCD_sendstring(unsigned char *var)

{

     while(*var)              //till string ends

       LCD_senddata(*var++);  //send characters one by one

}

void main()

{

 

LCD_initialize();

LCD_command(0x80);

LCD_sendstring("www.botskool.com");

 

}

 

 

Tags:

Comments

Member since:
6 May 2009
Last activity:
7 years 48 weeks

plz give examples in c for keypad opertaions

Member since:
21 February 2009
Last activity:
7 years 35 weeks

Yes definately we can call void LCD_command() function with appropriate commands in the LCD_initialize() function. It is good that you asked this. This code will be re-formatted and updated soon.

Member since:
15 March 2009
Last activity:
8 years 14 weeks

And please answer my query... the first comment on this tutorial... 

Member since:
15 March 2009
Last activity:
8 years 14 weeks

Can't we call void LCD_command(unsigned char var) with appropriate command in the  LCD_initialize() function... Why we have to give the instructions to the control lines again and again when we have defined the function for writing command.