Programming LCD in 4 bit and 8 bit mode using 8051

Interfacing of LCD plays a significant role in wide range of digital display applications such as Voltmeter, Ammeter, Locker etc. But many times we find it difficult to interface and program the LCD properly to make it display our desired characters.Through this post i am going to explains about programming lcd and interfacing it in 4 bit and 8 bit mode with a 8051 Microcontroller and programming it using Embedded C.

16×2 LCD:

This type of LCD module is very common and used widely in many types of display applications.It consists of 16 rows and 2 columns of 5×7 dot marices.The LCD display was a 16 pin package with back light,Contrast adjustment and 5×7 dot resolution.It consists of two built in registers known as data and Command register each has a specific function to perform with the display.The Data register is for writing the data to be displayed and Command register is to place the commands.The pin description of 16×2 LCD was given below.

The datas and the Commands both are given to the LCD through the data pins D0 to D7 but the logic state in the RS pin decides the data or command was given to the LCD.As stated in the above table for sending commands the RS pins should be in Logic 0 and for the datasthe RS Pin should hold the Logic 1 or high state.

COMMANDS FOR THE LCD:

The commands to the LCD was given in order to initialize the LCD and to give the starting address or position of the characters to be written in the LCD.The various commands to the LCD was given below.

The above commands perform the function specified to them and should be given to the LCD with the Register select (RS) pin in logic 0.

MODE OF OPERATION:

8 Bit mode.

4 Bit mode.

8 BIT MODE:

In 8 bit mode LCD requires uses 8 pins for data/command transmission from the microcontroller. The data bytes will be sent through the pins (D0-D7) along with EN,RS and RW(optional) of the LCD. So we need to dedicate a total of 11 pins from the Microcontroller to the LCD.

DESIGN:

INITIALIZING THE LCD:

The first step in coding the LCD is initializing the LCD connected by giving the commands as input through the data line D0-D7. For initializing the LCD following commands should be given to the LCD module

RS pin should be low to tell the LCD that the incoming byte is a command, so make it low.

Place the byte in D0-D7 pins of LCD and set the EN pin to high and then make it low with time delay of 10ms between them.

Send 38H command to use 2 lines 5×7 matrix in 8 bit mode.

Send 0FH for making the LCD,cursor and Cursor blinking ON.

Send 06H for incrementing cursor position.

Finally 01 and 02 for clearing screen and returning home.

DATA/COMMAND TRANSMISSION TO THE LCD:

The data transmission to a LCD must be performed by means of assigning logic states to three pins RS and E. R/W pin is not needed so we can ground it as shown in the schematic diagram. The data is send to the module by following these steps.

RS pin should be high for a data transmission and low for a command transmission.

Place the data/command in the data lines D0-D7 of the LCD.

Pulse En pin from high to low with certain time delay for transmission to complete.

Now lets move into the Programming of the LCD using this AT89S52 microcontroller and this programming was done by using Keil Uvision Software.

4 BIT MODE:

In 8 bit mode LCD requires uses 4 pins for data/command transmission from the microcontroller. The data bytes will be sent through the pins (D4-D7) higher bits along with EN,RS and RW(optional) of the LCD. So we need to dedicate a total of 7 pins from the Microcontroller to the LCD.

The main difference between the 4 and 8 bit mode is usage of pins from the microcontroller. .Also 8 bit mode is faster than the four bit mode which sends the data in the form of nibbles (4 bits).

DESIGN:

INITIALIZING THE LCD:

The first step in coding the LCD is initializing the LCD connected by giving the commands as input through the data line D4-D7 in the fom of Nibbles. For initializing the LCD following a specific reset sequence should be given and then initialized to follow the 4 bit mode.

Place the byte in D4-D7 pins of LCD and set the EN pin to high and then make it low with time delay of 10ms between them.

Send 28H command to use 2 lines 5×7 matrix in 4 bit mode.

Send 0FH for making the LCD,cursor and Cursor blinking ON.

Send 06H for incrementing cursor position.

Finally 01 and 02 for clearing screen and returning home.

DATA/COMMAND TRANSMISSION TO THE LCD:

The data transmission to a LCD must be performed by means of assigning logic states to three pins RS and E. R/W pin is not needed so we can ground it as shown in the schematic diagram. The data is send to the module by following these steps.

RS pin should be high to convey LCD a data transmission is going to take place.

Place the Upper nibble in the lower four bits of Port 2 by means of bit shifting and mask the upper four bits.

Pulse En pin from high to low with certain time delay for transmission to complete.

Now place the lower nibble and mask the rest of the bits, then repeat step 3.

NOTE:

Sometimes you might also want to use reset sequence for 8 bit mode to make the LCD work properly, in that case transmit the command “0x30” with enable pulse from high to low three times with certain delay in between each command.

Time delay for processing command/data may vary depending on the LCD , so in case you face any problem with command/data transmission try increasing the delay between high to low enable transition.