/* LCD 4bit MODE- D7-D4 of LCD are connected to P2.7-P2.4 ALGORITHM- first higher byte is sent and then the lower byte step1: mask the Lower nibble and send them to LCD (temp= value & 0xf0); send(temp); step2: enable pulse step3: left shift the value for 4 times, which makes lower nibble moves to higher nibble and higher nibble moves to lower nibble temp= value<<4; temp=value & 0xf0; send(temp); step4: enable pulse */ #include<reg51.h> void delay_msec(int); void lcd_init(); void lcd_data(char); void send(char); void lcd_cmd(char); void lcd_dataS(char *p); void lcd_data_int(int); sbit rs=P2^0; sbit en=P2^1; //***********************************------ void main() { P2=0x00; rs=en=0; lcd_init(); lcd_dataS("hello pc"); lcd_cmd(0xc0); lcd_data_int(555); while(1) { } } //************************************* void lcd_init() { delay_msec(20); send(0x30); delay_msec(10); send(0x30); delay_msec(1); send(0x30); delay_msec(1); send(0x20); delay_msec(1); // upto here we call it as RESETting of LCD, // and telling the LCD that we are using 4bit mode lcd_cmd(0x06); lcd_cmd(0x01); lcd_cmd(0x28); lcd_cmd(0x0e); } void send(char value) { char temp; temp= P2 & 0x0f; //store the higher nibble of P2 in temp P2= temp | value; //now bitwise OR the value with temp, //which successfully sends data(higher nibble) to P2 without //effecting its prevoius data in the lower nibble en=1; delay_msec(1); en=0; } void lcd_cmd(char cmd) { char temp; temp=cmd &0xf0; //masks (makes zero) the lower nibble rs=0; send(temp); //now send the higher nibble temp=cmd<<4; //now left shift 4 times which shifts lower //nibble to higher nibble temp=temp&0xf0; //now mask lower nibble again send(temp); //and send to the LCD } void lcd_data(char d) { char temp; temp=d &0xf0; rs=1; send(temp); temp=d<<4; temp=temp&0xf0; send(temp); } void lcd_dataS(char *p) { char temp; while(*p!='\0') { rs=1; temp=(*p) & 0xf0; send(temp); temp=(*p) << 4; temp=temp & 0xf0; send(temp); p=p+1; } } void lcd_data_int(int value) { int a[3]={0,0,0},i=0,temp; while((value/10)!=0) { a[i]=48+value%10; i++; value=value/10; } a[i]=48+value; for(i=2;i>=0;i--) { rs=1; temp= a[i] & 0xf0; send(temp); temp=(a[i] << 4) & 0xf0; send(temp); } } void delay_msec(int n) { int i,j; for(j=0;j<n;j++) for(i=0;i<921;i++); }
Wednesday, March 7, 2012
LCD in 4bit MODE -8051
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment