Liquid Crystal Display LCD 4-Bit Operation
Interface LCD in 4 bit mode, LCD in 4 bit mode, Interfacing LCD with microcontroller
WhyLCD4-BitOperation?
You may be Surprised why we using LCD in 4-Bit Mode and How its 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
Pin No
Symbol
I/O
Description
Vss
Vcc
Vee
Ground
+5V
RS
Input
Contrast Control
Command/Data Register
R/W
Input
Read/Write Register
E
DB0
Input/Output
Input/Output
Enable
Not Used in 4-Bit Mode
DB1
DB2
DB3
Input/Output
Input/Output
Input/Output
Not Used in 4-Bit Mode
Not Used in 4-Bit Mode
Not Used in 4-Bit Mode
14
DB4
DB5
DB6
DB7
Input/Output
Input/Output
Input/Output
Input/Output
15
Vcc
16
Vss
Data Bus in 4-Bit Mode
Data Bus in 4-Bit Mode
Data Bus in 4-Bit Mode
Data Bus in 4-Bit Mode
For LCD Back Light
For LCD Back Light
4
5
6
7
8
9
10
11
12
13
Flow Chart
Code in C
#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
*
Input
*
Output
*
Description
Intilization
*/
void delay(unsigned int j)
{
unsigned int i=0;
for(i=0;i<j;i++);
}
/*
*
Function Name
*
Input
*
Output
*
Description
*/
void lcd_init_write(unsigned char a)
{
RS=0;
RW=0;
LCDPORT=a;
E=1;
delay(lcd_delay);
E=0;
}
/*
*
Function Name
:
*
Input
*
Output
*
Description
*/
void lcd_com(unsigned char a)
{
unsigned char temp;
if(status)
{
status=0;
goto next;
delay
:
:
:
value
None
This Function Gives Approximate Delay required For LCD
lcd_init_write
:
value
:
None
:
Used For Initilize LCD
lcd_com
:
value
:
None
:
For Sending Commands and Data by checking Status Bit
}
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;
}
/*
*
*
*
*
*/
Function Name
Input
Output
Description
// Mask Lower 4 Bits
// Make No Affect on 0ther Port Pins
// Send Higher Nibble to LCDPORT
//Send Enable Signal to LCD
//Left Shift Byte Four Times
// Mask Higher 4 Bits
// Make No Affect on 0ther Port Pins
// Send Lower Nibble to LCDPORT
// Send Enable Signal to LCD
lcd_data
:
value
:
None
:
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 dont shift screen
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}
/*
*
Function Name
:
lcd_puts
*
Input
*
Output
*
Description
*/
void lcd_puts(char *str)
{
unsigned int i=0;
for(;str[i]!=0;i++)
lcd_data(str[i]);
}
/*
*
Function Name
*
Input
Output
*
Description
*/
:
:
:
String
String
Display String on LCD
:
:
:
:
Main
None
None
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
}