Wednesday, 26 August 2015

LCD code to display the characters (8051)

#include<reg51.h>

#define Lcd P2                                    // for 8-data pins D0-D7
#define lcd1 P3                                        //for RS, RW, EN Pins

void delay(unsigned int x)                           //delay function
{
        while(x--);
}

void cmd(unsigned int x)                            // command function
{
        P2=x;
        delay(1000);
        P3=0x04;                                      // RS=0,R/W=0,E=1
        delay(100);
        P3=0x00;                                              // RS=0,R/W=0,E=0
}
void intiz()                                  // initialization function
{
     
        cmd(0x01);                            // to clear screen
        cmd(0x0c);                            // display ON cursor  OFF
        cmd(0x80);                              // first row first column
        cmd(0x38);                            //2-lines and 5x7 matrix
}
void dat(unsigned int x)    // data function
{
        P2=x;
        delay(100);
        P3=0x05;               // RS=1,R/W=0,E=1
        delay(100);
        P3=0x01;                 // RS=1,R/W=0,E=0

}
void main()
{
int j,x,a[]={'A','L','I',' ','S','A','D','I','Q'};      // characters to be displayed

        intiz();                                                  // LCD initialization
         
        while(1)
        {
                x=0x8f;                                      // first row last column
                cmd(x--);
                //cmd(0x1c);

                for(j=0;j<3;j++)                            //loop to display the characters
                {      
                     dat(a[j]);
                        cmd(0x1c);
                }

                     x=0xcf;                              // last row last column
                    cmd(x--);

                for(j=3;j<9;j++)                           //loop to display the characters
                {
                        dat(a[j]);
                        cmd(0x1c);
                }
}

}

//Please comment below for any queries

No comments: