LCD Interfacing with 8051 Microcontroller

Hi All… In our last tutorial, we learned How to Interface LED and Switch. So today we are going to see LCD interfacing with an 8051 Microcontroller. Before Interfacing, we should know something about the 16×2 LCD module. Let’s start.

LCD DISPLAY

We always use devices made up of Liquid Crystal Displays (LCDs) like computers, digital watches, and also DVD and CD players. They have become very common and have taken a giant leap in the screen industry by clearly replacing the use of Cathode Ray Tubes (CRT). CRT draws more power than LCD and is also bigger and heavier. All of us have seen an LCD, but no one knows the exact working of it. Let us take a look at the working of an LCD.

Here we are using alphanumeric LCD 16×2. A 16×2 LCD display is a very basic module and is very commonly used in various devices and circuits. These modules are preferred over seven segments and other multi-segment LEDs.

The reasons are: LCDs are economical; easily programmable; have no limitation of displaying special & even custom characters (unlike in seven segments), animations, and so on.

A 16×2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in a 5×7 pixel matrix. This LCD has two registers, namely, Command and Data. The command register stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing it, clearing its screen, setting the cursor position, controlling the display, etc. The data register stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.

16×2 LCD Pin Diagram

16*2 LCD Pinout

Pin Description

 Pin No

Function

Name

1

Ground (0V)

Ground

2

Supply voltage; 5V (4.7V – 5.3V)

Vcc

3

Contrast adjustment; through a variable resistor

VEE

4

Selects command register when low; and data register when high

Register Select

5

Low to write to the register; High to read from the register

Read/write

6

Sends data to data pins when a high-to-low pulse is given

Enable

7

8-bit data pins

DB0

8

DB1

9

DB2

10

DB3

11

DB4

12

DB5

13

DB6

14

DB7

15

Backlight VCC (5V)

Led+

16

Backlight Ground (0V)

Led-

The LCD display module requires 3 control lines as well as either 4 or 8 I/O lines for the data bus. The user may select whether the LCD is to operate with a 4-bit data bus or an 8-bit data bus. If a 4-bit data bus is used the LCD will require a total of 7 data lines (3 control lines plus the 4 lines for the data bus). If an 8-bit data bus is used the LCD will require a total of 11 data lines (3 control lines plus the 8 lines for the data bus).

The three control lines are referred to as EN, RS, and RW.

The EN line is called “Enable.” This control line is used to tell the LCD that you are sending it data. To send data to the LCD, your program should make sure this line is low (0) and then set the other two control lines and/or put data on the data bus. When the other lines are completely ready, bring EN high (1) and wait for the minimum amount of time required by the LCD datasheet (this varies from LCD to LCD), and end by bringing it low (0) again.

The RS line is the “Register Select” line. When RS is low (0), the data is to be treated as a command or special instruction (such as a clear screen, position cursor, etc.). When RS is high (1), the data being sent is text data which should be displayed on the screen. For example, to display the letter “T” on the screen you would set RS high.

The RW line is the “Read/Write” control line. When RW is low (0), the information on the data bus is written to the LCD. When RW is high (1), the program is effectively querying (or reading) the LCD. Only one instruction (“Get LCD status”) is a read command. All others are write commands–so RW will almost always be LOW.

Finally, the data bus consists of 4 or 8 lines (depending on the mode of operation selected by the user). In the case of an 8-bit data bus, the lines are referred to as DB0, DB1, DB2, DB3, DB4, DB5, DB6, and DB7.

LCD COMMANDS

16x2 LCD Commands

Now let’s move to the program.

Interfacing LCD with Microcontroller – CIRCUIT DIAGRAM

RS is connected to Port 0.0 (P0.0)

RW is connected to Port 0.1 (P0.1)

EN is connected to Port 0.2 (P0.2)

Data lines are connected to Port 2 (P2)

LCD Interfacing with 8051 Microcontroller – Programming

Send Data

To send data on the LCD, data is first written to the data pins with R/W = 0 (to specify the write operation) and RS = 1 (to select the data register). A high to low pulse is given at EN pin when data is sent. Each write operation is performed on the positive edge of the Enable signal.

void dat(unsigned char b)
{
    lcd_data=b;
    rs=1;
    rw=0;
    en=1;
    lcd_delay();
    en=0;
}

Send String

We cannot send more than 8 bits at the same time. Because data lines are only having 8 bits. So how we can send string? Any guess? Yeah, you are correct. We have to send the string by character. See this code.

void show(unsigned char *s)
{
    while(*s) {
        dat(*s++);
    }
}

Send Command

To send a command on the LCD, a particular command is first specified to the data pins with R/W = 0 (to specify the write operation) and RS = 0 (to select the command register). A high to low pulse is given at the EN pin when data is sent.

void cmd(unsigned char a)
{
    lcd_data=a;
    rs=0;
    rw=0;
    en=1;
    lcd_delay();
    en=0;
}

LCD Initializing

To initialize the LCD we have to use certain commands.

void lcd_init()
{
    cmd(0x38);
    cmd(0x0e);
    cmd(0x01);
    cmd(0x06);
    cmd(0x0c);
    cmd(0x80);
}

Now see the full code below.

LCD Interfacing with 8051 – FULL CODE

#include <reg51.h>

#define lcd_data P2

sbit rs=P0^0;
sbit rw=P0^1;
sbit en=P0^2;

void lcd_init();
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void lcd_delay();

void lcd_init()
{
    cmd(0x38);
    cmd(0x0e);
    cmd(0x01);
    cmd(0x06);
    cmd(0x0c);
    cmd(0x80);
}

void cmd(unsigned char a)
{
    lcd_data=a;
    rs=0;
    rw=0;
    en=1;
    lcd_delay();
    en=0;
}

void dat(unsigned char b)
{
    lcd_data=b;
    rs=1;
    rw=0;
    en=1;
    lcd_delay();
    en=0;
}

void show(unsigned char *s)
{
    while(*s) {
        dat(*s++);
    }
}

void lcd_delay()
{
    unsigned int lcd_delay;
    for(lcd_delay=0;lcd_delay<=6000;lcd_delay++);
}

int main()
{
    unsigned int j;
    lcd_init();
    while(1) {
        cmd(0x80);
        show("   Welcome To   ");
        cmd(0xc0);
        show(" EMBETRONICX.COM");

        for(j=0; j<30000; j++);
        cmd(0x01);
        for(j=0; j<30000; j++);
    }
}

LCD Interfacing with 8051 – Working

LCD interfacing with 8051 - GIF

[ Image: Output ]

That’s all guys. But this is just basic. Here I haven’t used any hardware. I’m simply using Proteus. But if you use hardware it should work. But you should give proper connections. Here if you want to try something please try these exercises given below.

Tasks

  1. Scroll “EMBETRONICX” in the first row of the LCD.
  2. When I press the switch it should display the switch status whether ON or OFF. You can connect the switch to any pin in 8051.

In our next tutorial, we will see how to write the custom character on the LCD display.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents