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. 

No comments: