LCD interfacing with 8051-4 bit mode


LCD is one for the most useful device. However it needs upto 11 GPIO pins for proper working. If we configure LCD to operate in 4 bit mode, we can  interface LCD using only 6 GPIO pins of micro-controller. In this article we will explain how to interface LCD in 4 bit but before that you must understand the basics of LCD from this tutorial.

Conversion from 8 bit to 4 bit

Let's assume that dataport of LCD has some initial values which are unknown and we have a 8 bit command cmd to be given to LCD.

Thus initially dataport= xxxx xxxx  and cmd= yyyy zzzz ( here yyyy is upper nibble of command and zzzz is lower nibble of the command)

As we need to send data in 4 bit mode we need to send data in two parts on D4-D7 of LCD Databus and we must keep initial values of D3-D0 as it is. So our final output must be xxxx yyyy and xxxx zzzz

In short we have two inputs

  1. dataport= xxxx xxxx &
  2. cmd=yyyy zzzz

We need two outputs

  1. output-1= yyyy xxxx &
  2. output-2= zzzz xxxx

We should keep in mind below listed rules for AND & OR operations.

For AND operation,

x.1=x & x.0=0 here . indicates AND operation

For OR operation,

x+0=x & x+1=1 here + indicates OR operation

Now to obtain output-1, let's follow simple steps

dataport= xxxx xxxx

mask-1=   0000 1111

val-1= dataport & mask-1 = 0000 xxxx

cmd = yyyy zzzz

mask-2=1111 0000

val-2=cmd & mask-2=yyyy 0000

Now perform OR operation between val-1 and val-2

val-1 | val-2 = (0000 xxxx) | (yyyy 0000) = yyyy xxxx =output-1

This value of output-1 has to be given to dataport. Thus output-1 can be obtained from the equation

dataport=(dataport & 0x0f)|(cmd & 0xf0)

Now to obtain output-2, let's follow simple steps

dataport= xxxx xxxx

mask-1=   0000 1111

val-1= dataport & mask-1 = 0000 xxxx

cmd = yyyy zzzz

Rotate this value right side for 4 times to obtain zzzz 0000 means val-2=cmd<<4=zzzz 0000

Now perform OR operation between val-1 and val-2

val-1 | val-2 = (0000 xxxx) | (zzzz 0000) = zzzz xxxx =output-2

This value of output-2 has to be given to dataport. Thus output-2 can be obtained from the equation

dataport=(dataport & 0x0f)|(cmd <<4)

In this way, we can obtain data in 4 bit mode.

Apart from this, we need minor changes in the code which we have used for 8 bit.

LCD has some default configurations which are 8 bit mode and increment after displaying data. If we want to modify any of this configuration (which we are changing here from 8 bit mode to 4 bit mode), we need to reset LCD. To reset the LCD, we have to give series of command as 0x33 (2 times) followed by 0x32.

Here is the list of basic commands for LCD.

From this list, it is clear that Function Set command for 4 bit mode, 2 line 5x7 dots LCD is 0x28 instead of 0x38 for 8 bit mode.

With these changes, we are good to go for LCD interfacing in 4 bit mode. Code and schematic for 4 bit are given in the respective sections of the site.

If you have any queries, feel free to Ask us here.

Please log in to view code.

You may also like...

Leave a Reply

Specify Facebook App ID and Secret in Super Socializer > Social Login section in admin panel for Facebook Login to work

Specify LinkedIn Client ID and Secret in Super Socializer > Social Login section in admin panel for LinkedIn Login to work

Your email address will not be published. Required fields are marked *