Wednesday, 26 August 2015

Compact code for Numerical values display on LCD (8051)

#include<reg51.h>          //header file

#define lcd P2        // 8-pin data lines   D0-D7
#define lcd1 P3     // 3- Command lines RS, RW, EN

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 initz()            //Initialization function
{
        cmd(0x01);
        cmd(0x80);
        cmd(0x0e);
        cmd(0x38);
}

void dat(unsigned int x)    //data function
{
        P2=x;
        delay(10000);
        P3=0x5;                // RS=1,R/W=0,E=1
        delay(100);
        P3=0x1;               // RS=1,R/W=0,E=0
}
void count(unsigned int x)     // function to calculate the numerical values 000-999
{
        unsigned int a,b=1;
        a=x;
        while(a>9)                    // loop counts the no. of digits in the number received in x
        {
                b=b*10;
                a=a/10;
        }
         while(b>0)            // loop sends the digits to dat function
         {
                 dat((x/b)+48);
                 x=x%10;
                  b=b/10;
         }      
 }      
void main()
{
        unsigned int i;
        initz();                    //LCD initialization
        while(1)
        {
        for(i=0;i<1000;i++)                 // loop to send the number to Count function
        {
        count(i);
                cmd(0x80);                // display the no. in first row
}
}
}

//Please comment below for any queries

1 comment:

Embedded engineer said...
This comment has been removed by the author.