how to interface 16x2 LCD with 8051 microcontroller (AT89C51)


      It is very important to keep a track of the working of almost all the automated and semi-automated devices, be it a washing machine, an autonomous robot or anything else. This is achieved by displaying their status on a small display module. LCD (Liquid Crystal Display) screen is such a display module and a 16x2 LCD module is very commonly used. These modules are replacing seven segments and other multi segment LEDs for these purposes. The reasons being: LCDs are economical, easily programmable, have no limitation of displaying special & evencustom characters (unlike in seven segments), animations and so on. LCD can be easily interfaced with a microcontroller to display a message or status of a device. This topic explains the basics of a 16x2 LCD and how it can be interfaced with AT89C51 to display a character.




#include<at89x52.h> 
#define dataport P1       //Data port for LCD
#define sec 1000
sbit sw=P2^0;
sbit rs=P3^5;
sbit e=P3^4;

void delay(unsigned int msec)    // Time delay function
{
    int i,j ;
    for(i=0;i<msec;i++)
    for(j=0;j<1275;j++);
}

void lcd_cmd(unsigned char item)    //Program to send command to LCD
{
    dataport = item;
    rs= 0;
//    rw=0;
    e=1;
    delay(1);
    e=0;
    return;
}

void lcd_data(unsigned char item)    // Program to send data to LCD
{
    dataport = item;
    rs= 1;
//    rw=0;
    e=1;
    delay(1);
    e=0;
    return;
}

void lcd_data_string(unsigned char *str) // Program to send string to LCD
{
    int i=0;
    while(str[i]!='\0')
    {
        lcd_data(str[i]);
        i++;
        delay(10);
    }
    return;
}

void main()
{
    unsigned char str1[] ="HELLO";
    //unsigned char str2[] ="WORLD";
    unsigned char str3[] ="WELCOME";
    //unsigned char str4[] ="TO EARTH";
    lcd_cmd(0x38);  // for using 8-bit 2 row mode of LCD
    lcd_cmd(0x0E);  // turn display ON for cursor blinking
    lcd_cmd(0x01);  //clear screen
    while(1)
        {
             if(sw==1)
                {
                  lcd_cmd(0x01);  //clear screen
                        delay(50);
                    lcd_cmd(0x80);//chose first row
                        delay(50);
                    lcd_data_string(str1);
                        delay(50);
                    lcd_cmd(0x86);
                        delay(50);
                    lcd_data_string("WORLD");
                        delay(50);
               
  }
                   else
                 {
                 lcd_cmd(0x01);  //clear screen
                        delay(50);
                    lcd_cmd(0xC0); // chose second row
                        delay(50);
                    lcd_data_string(str3);
                        delay(50);
                   lcd_cmd(0xC8);
                        delay(50);
                    lcd_data_string("TO EARTH");
                        delay(50);
//              lcd_cmd(0x14);
//                    delay(50);


                 }        }            }     
Share on Google Plus

About CONSPEAR TECH

0 comments:

Post a Comment