8051Project

lcd message display using 8051


________________________________________________________________

C Code
________________________________________________________________

#include
#define LCD P3
/* defining commands to run */
unsigned char fset = 0x38; // Function set Interface is 8-bit long
unsigned char Cursor_ON = 0x0E; // Cursor ON/OFF:: 1/0
unsigned char Clear_Lcd = 0x01; // Clear LCD
unsigned char Cursor_Right = 0x06; // cursor movement to right direction
unsigned char Cursor_HOME = 0x02; // Move cursor to home location
unsigned char lin2=0xC0; // Second line address of cursor position
unsigned char lin3=0x94; // Third line address of cursor position
unsigned char lin4=0xD4; // Fourth line address of cursor position

/* line1 to display on LCD */
code unsigned char *line1 = " RAKESH KANSARA ";
code unsigned char *line2 = " USING C CODE ";
code unsigned char *line3 = " LCD Interfacing ";
code unsigned char *line4 = " AT89C51 ";

/* defining pins for control signals */
sbit RS = P2^0; /* register select DR/IR: 1/0*/
sbit RW = P2^1; /* read/write: 1/0 */
sbit E = P2^2; /* enable/disable:: 1/0*/
sbit busy = P3^7; /* busy checking bit */

/* signatures of the function being to used*/
void init(void); /* LCD initialization function */
void writeCmd( unsigned char ); /* function to run the commands on LCD*/
void getReady(void); /* function to check either LCD free or in process i.e busy */
void writeString(unsigned char *); /* function to write data/line1 to LCD. geting a pointer parameter.*/


/* start of the main function */
void main(void)
{
for(;;) /*infiniate loop to display repeation */
{
init(); /* call to initialization function*/
writeString(line1); /*sending line1 to lcd by calling writeString() function */
writeCmd(lin2);
writeString(line2); /*sending line1 to lcd by calling writeString() function */
writeCmd(lin3);
writeString(line3);
writeCmd(lin4);
writeString(line4);
}
}

void init() /* init() function defination. this funcatin sends commnads to the LCD for */
{ /* initialization by calling the writeCmd(unsigned char) function.*/
writeCmd(fset);
writeCmd(Cursor_ON);
writeCmd(Clear_Lcd);
writeCmd(Cursor_Right);
writeCmd(Cursor_HOME);
}

/* function defination for writeCmd(unsigned char) */
void writeCmd(unsigned char cmd)
{
getReady(); /* function for checking either is LCD not busy?*/
E = 0; /* make sure LCD not selected. that not be during initializing process*/
LCD = cmd; /* sending commmand to port3 */
RS = 0; /*Select instruction register*/
RW = 0; /* Write select control */
E = 1;
E = 0;
}

/* Check busy bit7 of the port3: function defination */

void getReady()
{
E = 0; /* make sure lcd is not selected */
busy = 1; /* Make input Bit */
RS = 0; /* Command Register select */
RW = 1; /* Read from LCD */
while (busy == 1)
{
E = 0;
E = 1;
}
E = 1;
}


/* function defination to display data on LCD*/
void writeString(unsigned char *str)
{
unsigned char i;
unsigned int j;
getReady();
E = 0; /* make sure lcd is not selected */
for(i=0;str[i]!='\0';i++)
{
LCD=str[i];
RS = 1;
RW = 0;
E = 1;
E = 0;
for(j = 0; j<25000; j++);
}
}




Popular Posts