Sunday 4 October 2015

LCD interfacing with 8051-II

Hi guys. Now I am going to show “How to interface LCD to 8051” using “Embedded C” programming. And in this post we are going to some advanced than the part-1 by using functions. This coding is useful to display more number of strings as well as it is rectifying drawbacks of the part-I. First show the hardware implementation of LCD. 

 Hardware implementation:-  


                                          Hardware implementation is same as you see in part-1 if you’re not see the part-1  click on the link for Hardware implementation . Now quick starts with Software coding by using 'Keil' software tool



Software implementation:- 


By using ‘Keil’ we can write code in embedded c language and also very easy to create ‘.hex’ files which is used for the dumping and run the 8051 micro-controller. Let see the code for LCD on 8051 micro-controller.

Program:-

#include<regx52.h>
#define lcd P2            // define LCD DATA pins to Port-2
sbit RS = P3^0;          // Register select
sbit RW = P3^1;         // Read/write
sbit EN = P3^2;          // Enable pin
void delay (unsigned int ms)      // Delay function             
{
    unsigned int i, j;                                                       
    for (i=0; i<=ms; i++)
    for (j=0; j<1275; j++);                               
}
void lcd_cmd(unsigned char x)    //Lcd command function
{
   lcd = x;
   RS = 0;
   RW = 0;
   EN = 1;
   delay(20);
   EN = 0;
}
void lcd_data(unsigned char t)    // Lcd Data function
{
   lcd = t;
   RS = 1;
   RW = 0;
   EN = 1;
   delay(20);
   EN = 0;
}
void lcd_initial()                  //Lcd initialization function
{
            lcd_cmd(0x38);        //Function set: 8-bit, 2-line 5x7 dots
            lcd_cmd(0x0c);        //Display ON, cursor OFF
            lcd_cmd(0x0E);        //Display ON, cursor ON
            lcd_cmd(0x01);        //Clear Display
}
           
void disp_str(unsigned char *p)        // Display String function
                                                           //*p is pointer variable
{
     for(*p=0;*p!='\0';*p++)
         {
           lcd_data(*p);
         }
}
void main()
{
            lcd_initial();
            disp_str("*** Hi Guys ***");
            lcd_initial();
            disp_str("Welcome to My");
            lcd_cmd(0xc0);                    //Cursor move from first line to second line
            delay(50);
            disp_str("Youtube channel");
            lcd_initial();
            disp_str("Please like this");
            lcd_cmd(0xc0);                   //Cursor move from first line to second line
            delay(50);
            disp_str("and Subscribe it");
            lcd_initial();
            disp_str("Thanks for watch");
            lcd_cmd(0xc0);                   //Cursor move from first line to second line
            delay(50);
            disp_str("**** Adios ****");
            delay(50);
            lcd_cmd(0x02);                  //Return Home
            lcd_cmd(0x0c);                  //Display ON cursor OFF
            delay(1000);

}  

As you see above program it is very simple by using function label with ‘lcd_str’, and easy why because we can’t use for-loops for many times to print number of strings on LCD. This is the main advantage in the coding.


For better understanding see the below video on Youtube. Any questions regarding this put a comment below, Thanks for watching keep enjoy and do better.




                                                        
   

No comments: