[otw_is sidebar=otw-sidebar-1]lcd interfacing is very important topic for embedded developers. So it is very important to know how to interface the LCD 16×2 with 8051. In today’s post we are going to demonstrate interfacing LCD with 8051 microcontroller using c code. We are going to use free SDCC Compiler which is open source and could be downloaded sdcc freely.

What is jhd 16×2 LCD?

This is a common LCD available for embedded system developers to display characters display. This LCD comes with 14 or 16 pin variants. If the total pins are 16, last two pins are used for background led light. This LCD also could be available in two or more colors. The most common colors are blue and green.

8bit vs 4bit interfacing

There are possible two ways to use this LCD. One is in 8bit mode, which we are talking in today’s tutorial, and second is 4bit interfacing mode. 8 bit mode is default mode for this lcd. Which means if this lcd cause to reset for any reason this lcd will automatically be shifted to 8 bit mode. On the other hand 4bit mode is used, if we are short of I/O pins and need to use reduced pin mode. This mode is mostly common if you have some previous experience in lcd interfacing with arduino or pic microcontroller.

LCD interfacing with 8051 C code

Now we are going to write the actual code for the lcd interfacing. We choosed C language for this tutorial and SDCC compiler. Create a new project in Code Blocks IDE. You can also use notepad or notepad++ or any editor of your choice to write the C code. For SDCC or most of the Embedded C programming, You need to focus on ANSI C standards.

Required Functions for LCD Interfacing

We need these 4 functions which will be used for interfacing lcd with 8051 microcontroller. Let’s look to these functions and we will explain this later.

void lcd_init(void);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void DELAY(unsigned);
  1. As you can see first function is to initialize the LCD. Which will send most of the configuration commands to the LCD.
  2. Second function lcd_cmd is used to send the commands to the LCD. These are those commands which are used to tell the LCD either to change LCD cursor behavior or to move around the available 16 characters per line. Or even to clear the LCD.
  3. lcd_data function is the actual function which will tell the LCD to display the given character onto the LCD. So which ever character that we want to put on LCD will be given to this function and this function is responsible for displaying that character to the LCD.
  4. DELAY function is similar as previous, which is just to waist some time. This function will be used in all LCD related functions to sync the speed of microcontroller with the comparatively slow speed LCD.

Complete code

Here is complete code in c language for interfacing 16×2 lcd with 8051 microcontroller using sdcc compiler.

/*
*/

#include <mcs51/8051.h>

#define LCD_PORT P0
#define LCD_RS P2_1
#define LCD_EN P2_0

void lcd_init(void);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void DELAY(unsigned);

void main(void)
{

lcd_init();
lcd_data('H');lcd_data('E');lcd_data('L');lcd_data('L');lcd_data('0');lcd_data('!');


while(1)
;

}


void lcd_cmd(unsigned char cmd_val) //Function to send command instruction to LCD
{
/*
To make sure that provided data will be treated as command rather than 
to display on LCD, WE need to make sure that the RS = 0;
*/
LCD_PORT = cmd_val;
LCD_RS= 0;
LCD_EN=1;
DELAY(1);
LCD_EN=0;
}

void lcd_data(unsigned char value) 
{
/*This is main function which will display our data
To put character on LCD all we have to change is to make RS=1*/
LCD_PORT = value;
LCD_RS= 1;
LCD_EN=1;
DELAY(1);
LCD_EN=0;
}

void lcd_init() /*LCD Initialization function*/
{
lcd_cmd(0x38); //This will set the font and lines of lcd
DELAY(10);
lcd_cmd(0x0F); //This command is specific for LCD cursor blinking
DELAY(10);
lcd_cmd(0x01); //This command is used to clear the lcd
DELAY(10);
lcd_cmd(0x81); //This command is just to force at first line and first character
DELAY(10);
}

void DELAY(unsigned x){
unsigned int i=1275;
for(;x>0;x--)
for(i=1275;i>0;i--);
}

Assembly Code

If you are concerned with more detail and need to know about the assembly language code to interface with the LCD Here is detail LCD tutorial on YouTube.

https://gist.github.com/abdul-rehman-2050/0b425d5518bdf53c425312dcc4da685f

Reference:

  • The 8051 microcontroller and embedded systems. [Muhammad Ali Mazidi; Janice Gillispie Mazidi; Rolin D McKinlay
  • “LCD Interfacing with 8051 Microcontroller (89S52): Tutorial with Circuit Diagram and Code,” Circuitdigest.com, 2015. [Online]. Available: https://circuitdigest.com/microcontroller-projects/lcd-interfacing-with-8051-microcontroller-89s52. [Accessed: 03-Mar-2020].