LCD 4-BIT MODE with 8051
TO know the Basic of LCD go to the previous post click here .
There
are many reasons why sometime we prefer to use LCD in 4-bit mode instead of
8-bit. One basic reason is lesser number of pins are needed to interface LCD.
In 4-bit mode the data is sent in
nibbles, first we send the higher nibble and then the lower nibble. To enable
the 4-bit mode of LCD, we need to follow special sequence of initialization
that tells the LCD controller that user has selected 4-bit mode of operation.
We call this special sequence as resetting the LCD. Following is the reset
sequence of LCD.
- Wait for abour 20mS
- Send the first init value (0x30)
- Wait for about 10mS
- Send second init value (0x30)
- Wait for about 1mS
- Send third init value (0x30)
- Wait for 1mS
- Select bus width(0x30 - for 8-bit and 0x20 for 4-bit)
- Wait for 1mS
The busy flag will only be valid after the above reset sequence. Usually we do not use busy flag in 4-bit mode as we have to write code for reading two nibbles from the LCD. Instead we simply put a certain ammount of delay usually 300 to 600uS. This delay might vary depending on the LCD you are using, as you might have a different crystal frequency on which LCD controller is running. So it actually depends on the LCD MODULE you are using. So if you feel any problem running the LCD, simply try to increase the delay. This usually works. For me about 400uS works perfect.
LCD
connections in 4-bit Mode
Above is the connection diagram of LCD in 4-bit mode, where we only need 6 pins to interface an LCD. D4-D7 are the data pins connection and Enable and Register select are for LCD control pins. We are not using Read/Write (RW) Pin of the LCD, as we are only writing on the LCD so we have made it grounded permanently. If you want to use it.. then you may connect it on your controller but that will only increase another pin and does not make any big difference. Potentiometer RV1 is used to control the LCD contrast. The unwanted data pins of LCD i.e. D0-D3 are connected to ground.
LCD
connections in 4-bit Mode
We will now look into the common
steps to send data/command to LCD when working in 4-bit mode. As
i already explained in 4-bit mode data is sent nibble by nibble, first we send
higher nibble and then lower nibble. This means in both command and data
sending function we need to saperate the higher 4-bits and lower 4-bits.
The common steps are:
The common steps are:
- Mask lower 4-bits
- Send to the LCD port
- Send enable signal
- Mask higher 4-bits
- Send to LCD port
- Send enable signal
lcd.c file
#include<reg51.h>
#include<stdio.h>
#define
LCDPORT P2
sbit RS =
LCDPORT ^ 0;
sbit RW =
LCDPORT ^ 1;
sbit E =
LCDPORT ^ 2;
#include
"lcd.h"
void main()
{
lcd_init();
lcd_com(15);
lcd_puts("test");
lcd_com(0XC0);
lcd_puts("World");
while (1);
}
lcd.h file .
bit status
= 0;
#define
lcd_delay 400
void delay(unsigned int j)
{
unsigned int i = 0;
for (; i < j; i++);
}
void
_lcd_init_write(unsigned char a)
{
RS = 0;
RW = 0;
LCDPORT = a;
E = 1;
delay(lcd_delay);
E = 0;
}
void lcd_com(unsigned char a)
{
unsigned char temp;
if (status) {
status = 0;
goto
next;
}
RS = 0;
next:
RW = 0;
temp = a;
temp &= 0xf0;
LCDPORT &= 0x0f;
LCDPORT |= temp;
E = 1;
delay(lcd_delay);
E = 0;
temp = a << 4;
temp &= 0xf0;
LCDPORT &= 0x0f;
LCDPORT |= temp;
E = 1;
delay(lcd_delay);
E = 0;
}
void lcd_data(unsigned char a)
{
status = 1;
RS = 1;
lcd_com(a);
}
void lcd_init(void)
{
delay(lcd_delay);
lcd_init_write(0x30);
delay(lcd_delay);
lcd_init_write(0x30);
delay(lcd_delay);
lcd_init_write(0x30);
delay(lcd_delay);
lcd_init_write(0x20);
delay(lcd_delay);
lcd_com(0x28);
delay(lcd_delay);
lcd_com(4);
delay(lcd_delay);
lcd_com(0x85);
delay(lcd_delay);
lcd_com(6);
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}
void lcd_puts(char *aaa)
{
unsigned int i = 0;
for (; aaa[i] != 0; i++)
lcd_data(aaa[i]);
}
Download here the Proteus and Keil File Click Here
Till now whatever we discussed in the previous part of ths LCD tutorial, we were dealing with 8-bit mode. Now we are going to learn how to use LCD
in 4-bit mode. There are many reasons why sometime we prefer to use LCD
in 4-bit mode instead of 8-bit. One basic reason is lesser number of
pins are needed to interface LCD.
In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
- Wait for abour 20mS
- Send the first init value (0x30)
- Wait for about 10mS
- Send second init value (0x30)
- Wait for about 1mS
- Send third init value (0x30)
- Wait for 1mS
- Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
- Wait for 1mS
Till now whatever we discussed in the previous part of ths LCD tutorial, we were dealing with 8-bit mode. Now we are going to learn how to use LCD
in 4-bit mode. There are many reasons why sometime we prefer to use LCD
in 4-bit mode instead of 8-bit. One basic reason is lesser number of
pins are needed to interface LCD.
In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
- Wait for abour 20mS
- Send the first init value (0x30)
- Wait for about 10mS
- Send second init value (0x30)
- Wait for about 1mS
- Send third init value (0x30)
- Wait for 1mS
- Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
- Wait for 1mS