Why LCD 4-Bit Operation?
You may be Surprised why we using LCD in 4-Bit Mode and How it’s Possible? LCD in 4-Bit means we are 4 Lines of data bus instead of using 8 Line data bus. In this Method, we are Splitting Bytes of data in Nibbles. If you successfully interface Microcontroller with LCD with 4 Pins. Then we can save 4 Lines of Microcontroller, which pins we can used for other purpose. In this Article we are using 16 x 2 LCD. Same Process in Repeated For all Type of Character LCDs expect Minor Changes
Circuit
How to initialize LCD in 4 bit mode
Lcd is initialize in 4 bit mode. For that 0x30 is the be written 3 times on lcd.
LCD Pin Description
Flow Chart
Code in C Download Code (KEIL Project) (Zip Format)
#include<reg51.h>
#include<stdio.h>
#define LCDPORT P2
sbit RS=LCDPORT^0;
sbit RW=LCDPORT^1;
sbit E =LCDPORT^2;
bit status=0;
#define lcd_delay 400
/*
* Function Name : delay
* Input : value
* Output : None
* Description : This Function Gives Approximate Delay required For LCD Intilization
*/
void delay(unsigned int j)
{
unsigned int i=0;
for(i=0;i<j;i++);
}
/*
* Function Name : lcd_init_write
* Input : value
* Output : None
* Description : Used For Initilize LCD
*/
void lcd_init_write(unsigned char a)
{
RS=0;
RW=0;
LCDPORT=a;
E=1;
delay(lcd_delay);
E=0;
}
/*
* Function Name : lcd_com
* Input : value
* Output : None
* Description : For Sending Commands and Data by checking Status Bit
*/
void lcd_com(unsigned char a)
{
unsigned char temp;
if(status)
{
status=0;
goto next;
}
RS=0;
next:
RW=0;
temp=a;
temp&=0xf0; // Mask Lower 4 Bits
LCDPORT&=0x0f; // Make No Affect on 0ther Port Pins
LCDPORT|=temp; // Send Higher Nibble to LCDPORT
E=1;
delay(lcd_delay); //Send Enable Signal to LCD
E=0;
temp=a<<4; //Left Shift Byte Four Times
temp&=0xf0; // Mask Higher 4 Bits
LCDPORT&=0x0f; // Make No Affect on 0ther Port Pins
LCDPORT|=temp; // Send Lower Nibble to LCDPORT
E=1;
delay(lcd_delay); // Send Enable Signal to LCD
E=0;
}
/*
* Function Name : lcd_data
* Input : value
* Output : None
* Description : For Sending Data By Setting Status Bit=1
*/
void lcd_data(unsigned char a)
{
status=1;
RS=1;
lcd_com(a);
}
/*
* Function Name : lcd_init
* Input : None
* Output : None
* Description : For Intilization LCD in 4-Bit Mode
*/
void lcd_init(void)
{
delay(lcd_delay);
lcd_init_write(0x30); //Special Sequence:Write Function Set.
delay(lcd_delay);
lcd_init_write(0x30); //Special Sequence:Write Function Set.
delay(lcd_delay);
lcd_init_write(0x30); //Special Sequence:Write Function Set.
delay(lcd_delay);
lcd_init_write(0x20); // 0x20 for 4-bit
delay(lcd_delay);
lcd_com(0x28); //Display Off, Cursor Off, Blink Off
delay(lcd_delay);
lcd_com(4); // Clear Screen & Returns the Cursor Home
delay(lcd_delay);
lcd_com(0x85);
delay(lcd_delay);
lcd_com(6); //Inc cursor to the right when writing and don’t shift screen
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}
/*
* Function Name : lcd_puts
* Input : String
* Output : String
* Description : Display String on LCD
*/
void lcd_puts(char *str)
{
unsigned int i=0;
for(;str[i]!=0;i++)
lcd_data(str[i]);
}
/*
* Function Name : Main
* Input : None
Output : None
* Description : Display Character on LCD at Proper Location
*/
void main()
{
lcd_init(); //Intilize LCD in 4-Bit Mode
lcd_com(0X80); // Start Cursor From First Line
lcd_puts("Hello"); //Print HELLO on LCD
lcd_com(0XC0); // Start Cursor From Second Line
lcd_puts("World"); //Print HELLO on LCD
while(1); //Stay Forever Here
}