8051 8 Bit mode Interfacing LCD Display using Keil C – AT89C51

Liquid Crystal Display (LCD):

16x2 LCD can be interfaced with microcontroller in 4 Bit or 8 Bit mode. These differs in how data is send to LCD. In 8 bit mode to write a character, 8 bit ASCII data is send through the data lines D0 – D7 and data strobe is given through E of the LCD. LCD commands which are also 8 bit are written to LCD in similar way.

But 4 Bit Mode uses only 4 data lines D4 – D7. In this mode 8 bit character ASCII data and command data are divided into two parts and send sequentially through data lines. The idea of 4 bit communication is used save pins of microcontroller. 4 bit communication is a bit slower than 8 bit communication but this speed difference can be neglected since LCD's are slow speed devices. Thus 4 bit mode data transfer is most commonly used.

Hardware Connection:


Program:


#include<reg51.h>

/* program by udaya prakash jayaraman */

/* Configure the data bus and Control pins as per the hardware connection
   Databus is connected to P2_0:P2_7 and control bus P0_0:P0_2*/
   
#define LcdDataBus  P2

sbit RS = P0^0;
sbit RW = P0^1;
sbit EN = P0^2;

/* local function to generate delay */
void delay(int k)
{
    unsigned int i, j;
    for(i=0;i<k;i++)
    {
        for(j=0;j<100;j++);
    }
}

/* Function to send the command to LCD */
void Lcd_CmdWrite( char cmd)
{
    LcdDataBus=cmd;    // Send the command to LCD
    RS=0;          // Select the Command Register by pulling RS LOW
    RW=0;          // Select the Write Operation  by pulling RW LOW
    EN=1;          // Send a High-to-Low Pusle at Enable Pin
    delay(15);
    EN=0;
    delay(1000);
}

/* Function to send the Data to LCD */
void Lcd_DataWrite( char dat)
{
    LcdDataBus=dat;      // Send the data to LCD
    RS=1;          // Select the Data Register by pulling RS HIGH
    RW=0;          // Select the Write Operation by pulling RW LOW
    EN=1;          // Send a High-to-Low Pusle at Enable Pin
    delay(15);
    EN=0;
    delay(1000);
}

int main()
{
    char i,a[]={"Udaya Prakash!"};
   
    Lcd_CmdWrite(0x38);        // enable 5x7 mode for chars
    Lcd_CmdWrite(0x0E);        // Display OFF, Cursor ON
    Lcd_CmdWrite(0x01);        // Clear Display
    Lcd_CmdWrite(0x80);        // Move the cursor to beginning of first line

    Lcd_DataWrite('H');
    Lcd_DataWrite('e');
    Lcd_DataWrite('l');
    Lcd_DataWrite('l');
    Lcd_DataWrite('o');
    Lcd_DataWrite(' ');
    Lcd_DataWrite('F');
    Lcd_DataWrite('R');
    Lcd_DataWrite('I');
    Lcd_DataWrite('E');
    Lcd_DataWrite('N');
    Lcd_DataWrite('D');
    Lcd_DataWrite('S');

    Lcd_CmdWrite(0xc0);        //Go to Next line and display Good Morning
    for(i=0;a[i]!=0;i++)
    {
        Lcd_DataWrite(a[i]);
    }

    while(1);
}





Have an opinion, suggestion , question or feedback about the article let it out here!