LCD 4 bit
Discussion in "General help Guidance and Discussion" started by Hoang Sep 23, 2007.
Sun Sep 23 2007, 02:00 pm
I've just started with 8051 uC, this code's writed for LCD 4-bit , it worked but I can't understand why in the initial procedure (LCDinit() function), there's three sequential statements : "LCD control_init(0x30)"- or it's compulsory. Somebody help me. Thanks alot.
#include <string.h>
#include <at89x52.h>
#define LCD_PORT P2
// LCD 4 bit display
sbit BF = 0xA7;
sbit RS = P3^5;
sbit RW = P3^6;
sbit EN = P3^7;
void delay_5ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<4;j++){}
}
void delay(long time){long i;for(i=1;i<time;i++){;}}
void delay_15ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<100;j++){}
}
void LCDbusy(void)
{
BF = 1;
EN = 1;
RS = 0;
RW = 1;
while (BF==1) {EN=0;EN=1;}
RW = 0;
}
void LCDcontrol_init(unsigned char x)
{
LCD_PORT = x;
RS = 0;
RW = 0;
EN = 1;
delay(400);
EN = 0;
}
void LCDcontrol(unsigned char x)
{
LCD_PORT = (x & 0xF0);
RS = 0;
RW = 0;
EN = 1;
delay(400);
EN = 0;
delay(100);
x <<= 4;
LCD_PORT = (x & 0xF0);
EN = 1;
delay(400);
EN = 0;
//LCDbusy();
}
void LCDinit(void)
{
delay_15ms();
LCDcontrol_init(0x30);
delay_5ms();
LCDcontrol_init(0x30);
delay_5ms();
LCDcontrol_init(0x30);
delay_5ms();
LCDcontrol_init(0x20);
LCDbusy();
LCDcontrol(0x28);
LCDbusy();
LCDcontrol(0x0C);
LCDbusy();
LCDcontrol(0x06);
LCDbusy();
LCDcontrol(85);
LCDbusy();
LCDcontrol(0x01);
LCDbusy();
}
void LCDwrite(unsigned char c)
{
LCD_PORT = (c & 0xF0);
RS = 1;
RW = 0;
EN = 1;
delay(400);
EN = 0;
LCDbusy();
c <<= 4;
LCD_PORT = (c & 0xF0);
EN = 1;
delay(400);
EN = 0;
LCDbusy();
}
void LCDputs(unsigned char *s,unsigned char row)
{
unsigned char len;
if(row==1) LCDcontrol(0x80);
else LCDcontrol(0xC0);
len=strlen(s);
while(len!=0)
{
LCDwrite(*s);
s++;
len--;
}
}
void main()
{
LCDinit();
LCDputs("coming home",1);
while(1) {}
}
#include <string.h>
#include <at89x52.h>
#define LCD_PORT P2
// LCD 4 bit display
sbit BF = 0xA7;
sbit RS = P3^5;
sbit RW = P3^6;
sbit EN = P3^7;
void delay_5ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<4;j++){}
}
void delay(long time){long i;for(i=1;i<time;i++){;}}
void delay_15ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<100;j++){}
}
void LCDbusy(void)
{
BF = 1;
EN = 1;
RS = 0;
RW = 1;
while (BF==1) {EN=0;EN=1;}
RW = 0;
}
void LCDcontrol_init(unsigned char x)
{
LCD_PORT = x;
RS = 0;
RW = 0;
EN = 1;
delay(400);
EN = 0;
}
void LCDcontrol(unsigned char x)
{
LCD_PORT = (x & 0xF0);
RS = 0;
RW = 0;
EN = 1;
delay(400);
EN = 0;
delay(100);
x <<= 4;
LCD_PORT = (x & 0xF0);
EN = 1;
delay(400);
EN = 0;
//LCDbusy();
}
void LCDinit(void)
{
delay_15ms();
LCDcontrol_init(0x30);
delay_5ms();
LCDcontrol_init(0x30);
delay_5ms();
LCDcontrol_init(0x30);
delay_5ms();
LCDcontrol_init(0x20);
LCDbusy();
LCDcontrol(0x28);
LCDbusy();
LCDcontrol(0x0C);
LCDbusy();
LCDcontrol(0x06);
LCDbusy();
LCDcontrol(85);
LCDbusy();
LCDcontrol(0x01);
LCDbusy();
}
void LCDwrite(unsigned char c)
{
LCD_PORT = (c & 0xF0);
RS = 1;
RW = 0;
EN = 1;
delay(400);
EN = 0;
LCDbusy();
c <<= 4;
LCD_PORT = (c & 0xF0);
EN = 1;
delay(400);
EN = 0;
LCDbusy();
}
void LCDputs(unsigned char *s,unsigned char row)
{
unsigned char len;
if(row==1) LCDcontrol(0x80);
else LCDcontrol(0xC0);
len=strlen(s);
while(len!=0)
{
LCDwrite(*s);
s++;
len--;
}
}
void main()
{
LCDinit();
LCDputs("coming home",1);
while(1) {}
}
Sun Sep 23 2007, 03:10 pm
Ok i explain you the sequence
To initialize LCD there is a special init sequence. Which is usually not required when dealing with 8-bit mode, but is kind of necessary when working with 4-bit mode.
To initialize LCD, we first need to reset it.. so below is the reset sequence.
After this the LCD is reset properly, now we need to set the data-width of the LCD which is 4-bit so..
I hope you understood the above reset sequence..
after this reset sequence, busy flag is ready to read. and you LCD is ready work.
To initialize LCD there is a special init sequence. Which is usually not required when dealing with 8-bit mode, but is kind of necessary when working with 4-bit mode.
To initialize LCD, we first need to reset it.. so below is the reset sequence.
//First Init LCDcontrol_init(0x30); delay_5ms(); //second init LCDcontrol_init(0x30); delay_5ms(); //third init LCDcontrol_init(0x30); delay_5ms();
After this the LCD is reset properly, now we need to set the data-width of the LCD which is 4-bit so..
//set data width LCDcontrol_init(0x20);
I hope you understood the above reset sequence..
after this reset sequence, busy flag is ready to read. and you LCD is ready work.
Powered by e107 Forum System