How to interface 16X2 LCD with 8051 controller

Keep Blogging & Exchange Knowledge

Visits=450

How to interface 16X2 LCD with 8051 controller

To interface 8051 controller with 16X2 LCD screen. We need to understand the 16x2 LCD first

Pin structure of 16x2 LCD screen is given below:-


Pin no

Name of pin

Description

1

Gnd

Ground Connection for LCD

2

Vcc

5V to give power to LCD

3

Vo

Contrast pin to set the intensity of LCD screen

4

Rs

Register select to set the behavior of DB0-DB7 pins

IF Rs=0 (Behave like command)

IF Rs=1 (Behave like data) 

5

R/W

It defines reading or writing on LCD

IF R/W =0 (Controller is writing on LCD)

IF R/W =1 (Controller is reading from LCD)

6

En

It latches your data/command on DB0-DB7 pins into data/command register

When En goes from High to low(data will be latched into LCD)

7

DB0

Data bit 0

8

DB1

Data bit 1

9

DB2

Data bit 2

10

DB3

Data bit 3

11

DB4

Data bit 4

12

DB5

Data bit 5

13

DB6

Data bit 6

14

DB7

Data bit 7

15

LED+

It makes on the back light of LCD when you give (5V)

16

LED-

It makes on the back light of LCD when you give (0V or gnd)

After Knowing the functionality of 16X2 LCD pins Now we can connect our LCD with 8051 controller

                                                   

    In the above circuit(copyright ) we only show the pins connection between 8051 controller and 16x2 LCD but you have to connect other pins for proper working of 8051 controller.

Now Important pin connection between 16x2 LCD and 8051 controller
LCD PINS8051 controller PINS
DB0-DB7 (Data pins of LCD)connected to port0 of 8051 controller
EN(enable pin of LCD)connected to P2.7 of 8051 controller
RS(register select of LCD)connected to P2.6 of 8051 controller
Now we should know the basic commands of 16x2 LCD just required for initialize it

Command od 16x2 LCDFunctionality of command
0x38LCD will work in two line display and 5x7 Pixel for characters
0x06Shift cursor right when new character comes
0x0eDisplay on cursor blinking
0x01Clear screen
0x80Address of first line first character of LCD (for first line address of characters goes from 0x80 to 0x8f and for second line 0xc0 to 0xcf)

Now we can start preparing to write program for 16x2 LCD on 8051 controller

  • we need basic four functions to be program on 8051 controller
  • Delay function (As data transfer rate for 16x2 LCD is slow compared to 8051 controller)
  • Command function (To transfer 1 byte of data to 16x2 LCD as command)
  • Data function (To transfer 1 byte of data to 16x2 LCD as data)
  • Init function (To initialize LCD)
  • Program to print "megasasta" on 16x2 LCD using 8051 controller


#include                                //header file contains declarations of SFR of 8051 controller

sbit en=P2^7;
sbit rs=P2^6;
Void delay (unsigned char a);               //To get the desired delay passed into the function
Void comm (unsigned char a);              // To pass one byte of data as command
Void datta (unsigned char a);                // To pass one byte of data as data (use datta as data is reserved keyword)
void init(void);                                        // To call all the initialization commands in it
Void main()
{
    init();
    comm(0x80);                              // give command to set address of first character of first line
    datta('m');                                   // send the character m in ASCII format(letter inside '' will be                                                        //converted into ASCII)
    datta('e');
    datta('g');
    datta('a');
    datta('s');
    datta('a');
    datta('s');
    datta('t');
    datta('a');
    while(1);                                      //busy the program in infinite loop
}
//After above program we have to write functions called in above program
Void init()
{
    comm(0x38);                                // passes the value of command (all the used commands                                                          //are already listed above)
    comm(0x06);
    comm(0x0E);
    comm(0x01);
    comm(0x80);
}
Void comm(unsigned char a)
{
    P0=a;                                            // data passes to PORT0 of 8051 controller as connected                                                          //to 16x2 LCD DATA PINS
    rs=0;                                             // to select command register  using pin 2.6 of 8051                                                          //controller (data on port0 will be                                                          //treated as command)           
    en=1;                                            // give high to low pulse on enable to latch byte on                                                          //DB0-DB7 on pin p2.7 of 8051                                                          //controller
    delay(10);
    en=0;                            
    delay(10);
}
Void datta(unsigned char a)
{
    P0=a;                                            // data passes to PORT0 of 8051 controller as connected                                                          //to LCD DATA PINS
    rs=1;                                             // to select data register using pin 2.6 of 8051                                                          //controller(data on port0 will be treated as data)           
    en=1;                                            // give high to low pulse on enable to latch byte on                                                          //DB0-DB7 on pin p2.7 of 8051 controller
    delay(10);
    en=0;                            
    delay(10);
}
void delay(unsigned char a)
{
    unsigned int i;
    for(a;a>0;a--)                                // loop will run equal to the value in a
    for(i=0;i<112;i++);                         // this loop will run for 112 times to get 1ms delay(only for                                                          //11.0592Mhz crystal)
}

So above is the program to interface 16x2 LCD with 8051 controller

                                                                        Copyrighted.com Registered & Protected <br></h2><h2>B3FW-S2RY-T1ZN-4APY





Mega Sasta is a online platform provides facilities to sell products by post free advertisement in advertisement section, Conduct online test in study section Or you can write your own blog with megasasta in blog section. Mega sasta is only providing you these facilities free of cost. But any thing posted on advertisment section, written in blog or online exam section. Mega sasta is not responsible for any wrong commitment done through any of its section. IF you found any thing against law on this site please inform us through contact us link on below of this page. Its an humble request to help us make megasasta.com more clean and genuine.
Thank you for using megasasta.com