LCD in 4-Bit means we are using 4 Lines of data bus instead of using 8 Line data bus. In this Method, we are Splitting Bytes of data in Nibbles. Thus saving 4 Lines of Microcontroller. Here we have used 16 x 2 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 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
CODE | |
I/D=1: Increment | DL=0:4-bit |
I/D=0: Decrement | N=1:2 Rows |
S=1: With display shift | N=0: 1 Row |
S/C=1: Display Shift | F=1:5x10 dot |
S/C=0: Cursor movement |
F=0: 5x7 dots |
R/L=1: Shift to the right | BF=1 : Internal operation is being performed |
R/L=0: Shift to the left | BF=0: Instruction acceptable |
DL=1: 8-bit |
DESCRIPTION:
DDRAM: Display Data RAM
CGRAM: Character Generator RAM
ACG: GCRAM Address
ADD: DDRAM Address Corresponds to cursor address.
AC: Address Counter, used for both DDRAM and CGRAM
EXECUTION TIME:
fcp or fosc=250kHz
However, when frequency changes, execution time also changes
Example
If fcp or fosc is 250kHz, 37µS x (270/250)=40µS
FLOW DIAGRAM FOR WRITING PROGRAM:
PROTEUS DESIGN
CODE(8051)
#include<reg51.h>
sbit rs = P3^0;
sbit en = P3^1;
void lcdint();
void lcdcmd(char);
void lcddata(char);
void lcdstring(char *p);
void delay(int);
void togle_e_pin();
void main()
{
P2=0x00;
rs=0;
en=0;
lcdint();
lcdstring("WELCOME!!!");
lcdcmd(0xc0);
lcdstring("projectsguru");
while(1)
{
}
}
void delay(int k)
{
int i;
for(i=0;i<k;i++);
}
void togle_e_pin()
{
en= 1;
delay(100);
en= 0;
delay(100);
}
void lcddata(char value)
{
rs= 1;
P2 &= 0x0F;
P2 |= (value&0xF0);
togle_e_pin();
P2 &= 0x0F;
P2 |= ((value<<4)&0xF0);
togle_e_pin();
}
void lcdcmd(char value)
{
rs= 0;
P2 &= 0x0F;
P2 |= (value&0xF0);
togle_e_pin();
P2 &= 0x0F;
P2 |= ((value<<4)&0xF0);
togle_e_pin();
}
void lcdint()
{
P2 &= 0x0F;
P2 |= (0x30&0xF0);
togle_e_pin();
delay(500);
P2 &= 0x0F;
P2 |= (0x20&0xF0);
togle_e_pin();
delay(500);
lcdcmd(0x28);
lcdcmd(0x01);
lcdcmd(0x0c);
}
void lcdstring(char *p)
{
while(*p!='\0')
{
lcddata(*p);
delay(2000);
p++;
}
}
PROTEUS DESIGN
CODE(AVR)
/*
* _4bit_nw.c
* Created: 7/9/2014 10:29
*:52 PM
* Author: gargi
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
void lcdint(void);
Toggle_e_pin();
void lcdcmd(unsigned char);
void lcddata(char);
void lcdstring(char *);
#define en PB2
#define rw PB1
#define rs PB0
int main(void)
{
DDRA=0xff;//output port
DDRB=0x07;
lcdint(); // Initialize LCD in 4bit mode
lcdcmd(0x01);
_delay_ms(2);
lcdstring("WELCOME!!");
lcdcmd(0xc0);
lcdstring("projectsguru");
while(1)
{
}
}
void Toggle_e_pin();
{
PORTB|=(1<<en);
_delay_ms(1);
PORTB&=~((1<<en));
_delay_ms(1);
}
void lcdcmd(unsigned char Command)
{
PORTB&=~((1<<rs));
PORTA &= 0x0F;
PORTA |= (Command&0xF0);
Toggle_e_pin();
PORTA &= 0x0F;
PORTA |= ((Command<<4)&0xF0);
Toggle_e_pin();
}
void lcddata(char LCDChar)
{
PORTB|=(1<<rs);
PORTA &= 0x0F;
PORTA |= (LCDChar&0xF0);
Toggle_e_pin();
PORTA &= 0x0F;
PORTA |= ((LCDChar<<4)&0xF0);
Toggle_e_pin();
}
void lcdint(void)
{
PORTA &= 0x0F;
PORTA |= 0x30;
Toggle_e_pin();
_delay_ms(6);
PORTA &= 0x0F;
PORTA |= 0x30;
Toggle_e_pin();
_delay_ms(200);
PORTA &= 0x0F;
PORTA |= 0x30;
Toggle_e_pin();
PORTA &= 0x0F;
PORTA |= 0x20;
Toggle_e_pin();
_delay_ms(2);
lcdcmd(0x28);
lcdcmd(0x0c);
lcdcmd(0x01);
lcdcmd(0x06);
}
void lcdstring(char *s)
{
while(*s!='\0')
{
lcddata(*s);
s++;
}
}
Category: