LCD Interfacing with 8051 Microcontroller

Published  June 17, 2015   17
J Jayant
Author
LCD Interfacing with 8051 Microcontroller (89S52)
LCD Interfacing with 8051 Microcontroller (89S52)

Display units are the most important output devices in embedded projects and electronics products. 16x2 LCD is one of the most used display unit. 16x2 LCD means that there are two rows in which 16 characters can be displayed per line, and each character takes 5X7 matrix space on LCD. In this tutorial we are going to connect 16X2 LCD module to the 8051 microcontroller (AT89S52). Interfacing LCD with 8051 microcontroller might look quite complex to newbies, but after understanding the concept it would look very simple and easy. Although it may be time taking because you need to understand and connect 16 pins of LCD to the microcontroller. So first let's understand the 16 pins of LCD module.

We can divide it in five categories, Power Pins, contrast pin, Control Pins, Data pins and Backlight pins.

Category

Pin NO.

Pin Name

Function

Power Pins

1

VSS

Ground Pin, connected to Ground

2

VDD or Vcc

Voltage Pin +5V

Contrast Pin

3

V0 or VEE

Contrast Setting, connected to Vcc thorough a variable resistor.

Control Pins

4

RS

Register Select Pin, RS=0 Command mode,

RS=1 Data mode

5

RW

Read/ Write pin, RW=0 Write mode,

RW=1 Read mode

6

E

Enable, a high to low pulse need to enable the LCD

Data Pins

7-14

D0-D7

Data Pins, Stores the Data to be displayed on LCD or the command instructions

Backlight Pins

15

LED+ or A

To power the Backlight +5V

16

LED- or K

Backlight Ground

All the pins are clearly understandable by their name and functions, except the control pins, so they are explained below:

RS: RS is the register select pin. We need to set it to 1, if we are sending some data to be displayed on LCD. And we will set it to 0 if we are sending some command instruction like clear the screen (hex code 01).

RW: This is Read/write pin, we will set it to 0, if we are going to write some data on LCD. And set it to 1, if we are reading from LCD module. Generally this is set to 0, because we do not have need to read data from LCD. Only one instruction “Get LCD status”, need to be read some times.

E: This pin is used to enable the module when a high to low pulse is given to it. A pulse of 450 ns should be given. That transition from HIGH to LOW makes the module ENABLE.

There are some preset command instructions in LCD, we have used them in our program below to prepare the LCD (in lcd_init() function). Some important command instructions are given below:

Hex Code

Command to LCD Instruction Register

0F

LCD ON, cursor ON

01

Clear display screen

02

Return home

04

Decrement cursor (shift cursor to left)

06

Increment cursor (shift cursor to right)

05

Shift display right

07

Shift display left

0E

Display ON, cursor blinking

80

Force cursor to beginning of first line

C0

Force cursor to beginning of second line

38

2 lines and 5×7 matrix

83

Cursor line 1 position 3

3C

Activate second line

08

Display OFF, cursor OFF

C1

Jump to second line, position 1

OC

Display ON, cursor OFF

C1

Jump to second line, position 1

C2

Jump to second line, position 2

Circuit Diagram and Explanation

Circuit Diagram for Interfacing LCD with 8051 Microcontroller (89S52)

Circuit diagram for LCD interfacing with 8051 microcontroller is shown in the above figure. If you have basic understanding of 8051 then you must know about EA(PIN 31), XTAL1 & XTAL2, RST pin(PIN 9), Vcc and Ground Pin of 8051 microcontroller. I have used these Pins in above circuit. If you don’t have any idea about that then I recommend you to read this Article LED Interfacing with 8051 Microcontroller before going through LCD interfacing.

So besides these above pins we have connected the data pins (D0-D7) of LCD to the Port 2 (P2_0 – P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3) of microcontroller respectively.

PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1 (VSS) and PIN 16(Backlight ground) are connected to ground.

Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected to voltage supply and Ground.

Code Explanation

I have tried to explain the code through comments (in code itself).

As I have explained earlier about command mode and data mode, you can see that while sending command (function lcd_cmd) we have set RS=0, RW=0 and a HIGH to LOW pulse is given to E by making it 1, then 0. Also when sending data (function lcd_data) to LCD we have set RS=1, RW=0 and a HIGH to LOW pulse is given to E by making it 1 to 0. Function msdelay() has been created to create delay in milliseconds and called frequently in the program, it is called so that LCD module can have sufficient time to execute the internal operation and commands.

A while loop has been created to print the string, which is calling the lcd_data function each time to print a character until the last character (null terminator- ‘\0’).

We have used the lcd_init() function to get the LCD ready by using the preset command instructions (explained above). 

Complete Project Code

// Program for LCD Interfacing with 8051 Microcontroller (AT89S52) 
#include
#define display_port P2      //Data pins connected to port 2 on microcontroller
sbit rs = P3^2;  //RS pin connected to pin 2 of port 3
sbit rw = P3^3;  // RW pin connected to pin 3 of port 3
sbit e =  P3^4;  //E pin connected to pin 4 of port 3
void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned i,j ;
    for(i=0;i
(function() { document.addEventListener('DOMContentLoaded', function() { // Clean up any remaining HTML entities or tags document.querySelectorAll('.code-block code').forEach(function(element) { let content = element.innerHTML; // Replace common HTML entities content = content.replace(/&lt;/g, '<'); content = content.replace(/&gt;/g, '>'); content = content.replace(/&nbsp;/g, ' '); content = content.replace(/&amp;/g, '&'); // Remove paragraph tags and convert <br> to newlines content = content.replace(/<\/?p>/g, ''); content = content.replace(/<br\s*\/?>/g, '\n'); // Clean up multiple newlines content = content.replace(/\n\s*\n/g, '\n'); element.innerHTML = content; }); // Copy button functionality document.querySelectorAll('.copy-button').forEach(function(button) { button.addEventListener('click', function() { const pre = this.nextElementSibling; const code = pre.querySelector('code'); const textContent = code.textContent || code.innerText; navigator.clipboard.writeText(textContent.trim()).then(function() { button.textContent = 'Copied!'; button.classList.add('copied'); setTimeout(function() { button.textContent = 'Copy Code'; button.classList.remove('copied'); }, 2000); }).catch(function(err) { console.error('Failed to copy:', err); button.textContent = 'Failed to copy'; }); }); }); }); })(); Video Tags 8051 89S52 16x2 LCD microcontroller embedded Have any question realated to this Article?Ask Our Community MembersWhatsAppTelegramDiscordForum Comments Submitted by Austin on Mon, 04/11/2016 - 02:05 Permalink Programmer what kind of programmer did you use and which software Log in or register to post comments M Submitted by Maddy on Mon, 04/18/2016 - 18:04 In reply to Programmer by Austin Permalink Please refer this: Getting Please refer this: Getting Started with 8051 Microcontroller Log in or register to post comments Submitted by Muhammad haroon on Thu, 05/26/2016 - 20:09 Permalink Start frist time at 8051 What is the difference b\w 8051 and 89s52 Log in or register to post comments Submitted by harish kumar on Thu, 06/02/2016 - 19:30 Permalink liquid crystal display is liquid crystal display is working but it is not display anything like circuit digest. how can i fix that sir programme uploading done and programme is correct but it is not work pls tell any idea sir Log in or register to post comments M Submitted by Maddy on Sat, 06/11/2016 - 17:03 In reply to liquid crystal display is by harish kumar Permalink Check your circuit Check your circuit connections again. Log in or register to post comments Submitted by Talha Shah on Sun, 06/12/2016 - 14:59 In reply to liquid crystal display is by harish kumar Permalink Agree with you harish kumar Agree with you harish kumar If you got the solution kindly share with us Log in or register to post comments Submitted by Rajat Ahuja on Wed, 07/20/2016 - 12:40 Permalink lcd interfacing hey..!!! your lcd interfacing code wwas really helpful. can you please provide me a code for interfacing LCD in 4 Bit Mode. Log in or register to post comments Submitted by sruthi on Tue, 01/03/2017 - 14:33 Permalink sir i m not getting the sir i m not getting the display on lcd it is running but there is display of CIRCUIT DIGEST Log in or register to post comments Submitted by Rahul on Tue, 02/14/2017 - 22:21 Permalink Thanku for ur explanation and Thanku for ur explanation and code.Can u tell me how to store multiple strings into LCD? Log in or register to post comments Submitted by parmar darshil on Wed, 03/08/2017 - 19:03 Permalink project sir, i want program for the ardino uno adc program Log in or register to post comments Submitted by bharat upadhye on Mon, 05/22/2017 - 11:36 Permalink display not showing words.. sir, i used this code it is working while simulation(Proteus s/w) in laptop....but in hardware not displaying words in LCD.....it's showing blocks...plz tell what might be the problem.. Log in or register to post comments Submitted by PAULSON KALU on Tue, 12/05/2017 - 16:53 Permalink sim900a interface with at89s52 i will love to be part of you guys Log in or register to post comments Submitted by abdul on Sun, 02/04/2018 - 13:48 Permalink the program can be erased on the program can be erased on at89s52 Log in or register to post comments Submitted by Hassan on Tue, 03/06/2018 - 17:46 Permalink lcd option Hello circuit digest! Can I use blue character display instead of green. Log in or register to post comments Submitted by nirali on Mon, 03/26/2018 - 20:39 Permalink assembly progamm hey circuit digest... how can display fix value on LCD interfacing with 8051..and give me assembly language ........plz Log in or register to post comments Submitted by Suchita Mankame on Fri, 04/27/2018 - 10:47 Permalink 8051 The above video shows lcd interface on bread board Can this project be shown on general purpose board??? Also on pcb??? Log in or register to post comments Submitted by Fakir Chandra Mandi on Thu, 06/07/2018 - 19:11 Permalink LCD Very useful article Log in or register to post comments Log in or register to post comments .community-link{ color: #828282; font-size: 13px; text-transform: uppercase; } .community_icon:hover{ opacity: 0.7; } .community-link:hover{ color:#0e3d79; } .community-icon-p{ padding: 5px 5px !important; background: none !important; } .head-article{ font-size: 20px;color: #4d4d4d;margin-bottom: 20px; } @media only screen and (max-width: 361px) { .community-link { font-size: 10px; } .head-article{ font-size: 18px; margin-bottom: 12px; } .community_icon{ width:30px; } } var large = '<div class="field field--name-field-tags field--type-entity-reference field--label-above " style="margin-bottom: 30px;margin-top: 30px;"><div class="field--label">Have any question realated to this Article?</div><div class=""><div class="" style="display: inline-block;padding-right: 15px;"><h4 class="head-article">Ask Our Community Members</h4></div><div class="field--item community-icon-p" style=""><a href="https://chat.whatsapp.com/JR4e0Fc0V20H6jTMDd1daT" hreflang="en" target="_blank"><img class="community_icon" src="https://i.postimg.cc/R0zg5mSb/whatspp-logo.png" width="40px;"><div class="community-link">WhatsApp</div></a></div><div class="field--item community-icon-p"><a href="https://t.me/circuitdigest" hreflang="en" target="_blank"><img class="community_icon" src="https://i.postimg.cc/ZKFV6RrY/teleg-logo.png" width="40px;"><div class="community-link">Telegram</div></a></div><div class="field--item community-icon-p"><a href="https://discord.com/invite/UXJrFJSWpz" hreflang="en" target="_blank"><img class="community_icon" src="https://i.postimg.cc/vH6VfSfQ/discord-logo.png" width="40px;"><div class="community-link">Discord</div></a></div><div class="field--item community-icon-p"><a href="https://circuitdigest.com/forums" hreflang="en" target="_blank"><img class="community_icon" src="https://i.postimg.cc/4dTgGz1c/form-logo.png" width="40px;"><div class="community-link">Forum</div></a></div></div></div>'; var elements = document.getElementsByClassName("field--name-body"); insertAfter(large, elements); //elements.insertAdjacentHTML('afterend', large); //$('.field--name-body').append(large);​ //$('.field--name-body').after(large);
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1593583401069-0'); });