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.




                                                        
   

Friday, 2 October 2015

LCD interfacing with 8051-I

Hi guys. Now I am going to show “How to interface LCD to 8051” using “Embedded C” programming. In this post I am going to show how to interface LCD to 8051 micro-controller and how to display strings on that LCD using simple code in embedded c. First show the hardware implementation of LCD.



Hardware implementation:- 

                                   
Now going to hardware implementation first, and you know about the tool called “Proteus”, by using this you can implement the hardware design. Now go to how to connect LCD 16x2 display in Proteus is shown below.



As you see in above picture, shows how to interfacing LCD with 8051. Place the 8051 micro-controller and connect the LCD  data pins(D0-D7) to port-2 of the 8051 micro-controller similarly register select(RS), Read/write(RW) and Enable(EN) pins are connected to P-3.0,P-3.1,P-3.2 respectively. And Variable resistor(VR1) is used to either increase or decrease the contrast of the 16x2 Lcd display. Despite of these connections we also know about the commands for LCD 16x2 display those commands are useful for our project. Some commands are show in below image..



After completed everything go to the software simulation and understand the program carefully.

Software implementation:-
                                   
Now quickly starts with software implementation, for this purpose you know about the software called “Keil” this tool used to write the code in “Embedded C” language and it is very easy to use. 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 main()                                             //Main function
{
    int i,j;
    char str1[]="welcome to My";
    char str2[]="  Blog";
    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
       while(1)
          {
               for(i=0;i<13;i++)     
                 {
                       lcd_data(str1[i]);
                  }
         lcd_cmd(0xc0);                             //Cursor move from first line to second line
         delay(50);                                      // delay 50ms
                for(j=0;j<7;j++)
                  {
                        lcd_data(str2[j]);
                   }            
           lcd_cmd(0x02);                           //Return Home
           lcd_cmd(0x0c);                           //Display ON cursor OFF
   
   }      
}

As you see above program it is very simple and easy, why because we can use strings and programs consists of two strings str1 and str2, similarly we use two for-loops for both the strings and print data on LCD. If you have clear understand about this topic then you must watch the below video.



Pros:-
  • This program is very simple and easy.
  • Using strings and loops we can easy to develop and understand.
Cons:-
  • In this program is, if you display another string on Lcd we can use another string and for-loop it is very difficult to display more number of strings and code memory is also increases this is the main draw back of this program instead of this it is very easy.