Skip to main content

16*2 LCD interfacing with 8051 microcontroller

LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of applications. A 16x2 LCD display is 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 being:

1.LCDs are economical;
2.easily programmable;
3. have no limitation of displaying special & animation.

A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 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 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.

Liquid Crystal Display (LCD) is very helpful in providing user interface as well as for debugging purpose. The most common type of LCD controller is HITACHI 44780 which provides a simple interface between the controller & an LCD.

The most commonly used ALPHANUMERIC displays are:
  • 1x16 (Single Line & 16 characters),
  • 2x16 (Double Line & 16 character per line),
  • 4x20 (four lines & Twenty characters per line).

Pin Configuration:

A 16x2 LCD consist of 16 pin that make connections to display a text on it. It can be interfaced with any controller to control the displayed images on it.
 
pin configuration

LCD commands should know before programming

Interfacing with LCD:

These LCD's are very simple to interface with the controller as well as are cost effective.

The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines depends on the mode of operation. If operated in 8-bit mode then 8 data lines + 3 control lines i.e. total 11 lines are required. And if operated in 4-bit mode then 4 data lines + 3 control lines i.e. 7 lines are required. How do we decide which mode to use? It’s simple if you have sufficient data lines you can go for 8 bit mode & if there is a time constrain i.e. display should be faster then we have to use 8-bit mode because basically 4-bit mode takes twice as more time as compared to 8-bit mode.

When RS is low (0), the data is to be treated as a command. When RS is high (1), the data being sent is considered as text data which should be displayed on the screen.

When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively reading from the LCD. Most of the times there is no need to read from the LCD so this line can directly be connected to Gnd thus saving one controller line.

The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required to latch the data. The LCD interprets and executes our command at the instant the EN line is brought low. If you never bring EN low, your instruction will never be executed.

For Contrast setting a 10K pot should be used as shown in the figure.

Display Data Ram (DDRAM) stores the display data. So when we have to display a character on LCD we basically write it into DDRAM. For a 2x16 LCD the DDRAM address for first line is from 80h to 8fh & for second line is 0c0h to 0cfh. So if we want to display 'H' on the 7th postion of the first line then we will write it at location 87h.

Circuit Diagram:

16x2 LCD interfacing with 8051

Code:

#include "REG52.h"

#define LCD P3
sbit RS=P2^5;
sbit RW=P2^6;
sbit EN=P2^7;
long int i;

void CMD(char);
void DATA1(char);

void main()
{
CMD(0x38);
for(i=0;i<=100;i++);

CMD(0x0E);
for(i=0;i<=100;i++);

CMD(0x01);
for(i=0;i<=100;i++);

CMD(0x14);
for(i=0;i<=100;i++);

CMD(0x81);
for(i=0;i<=100;i++);

DATA1('K') ;
for(i=0;i<=100;i++);

DATA1('N') ;
for(i=0;i<=100;i++);

DATA1('I') ;
for(i=0;i<=100;i++);

DATA1('X') ;
for(i=0;i<=100;i++);

CMD(0xC6);
for(i=0;i<=100;i++);

DATA1('S') ;
for(i=0;i<=100;i++);

DATA1('O') ;
for(i=0;i<=100;i++);

DATA1('L') ;
for(i=0;i<=100;i++);

DATA1('U') ;
for(i=0;i<=100;i++);

DATA1('T') ;
for(i=0;i<=100;i++);

DATA1('I') ;
for(i=0;i<=100;i++);

DATA1('O') ;
for(i=0;i<=100;i++);

DATA1('N');
for(i=0;i<=100;i++);

DATA1('S') ;
for(i=0;i<=100;i++);
}

void CMD(unsigned char c)
{
LCD=c;
RS=0;
RW=0;
EN=1;
for(i=0;i<20;i++);
EN=0;
}

void DATA1(unsigned char a)
{
LCD=a;
RS=1;
RW=0;
EN=1;
for(i=0;i<20;i++);
EN=0;
}
Program to implement string function:
#include "REG52.h"
#define LCD P3
sbit RS=P2^5;
sbit RW=P2^6;
sbit EN=P2^7;
int i, j;
unsigned char a[]="WELCOME";
unsigned char b[]="HOME";

void CMD(char);
void DATA1(char);

void main()
{
CMD(0x38);
for(i=0;i<=500;i++);

CMD(0x0E);
for(i=0;i<=500;i++);

CMD(0x01);
for(i=0;i<=500;i++);

CMD(0x14);
for(i=0;i<=500;i++);

CMD(0x85);
for(i=0;i<=500;i++);

for(j=0;a[j]!='';j++)
{
DATA1(a[j]);
CMD(0xC6);
for(i=0;i<=500;i++);
for(j=0;b[j]!='';j++)
DATA1(b[j]);
}

void CMD(unsigned char c)
{
LCD=c;
RS=0;
RW=0;
EN=1;
for(i=0;i<100;i++);
EN=0;
}

void DATA1(unsigned char d)
{
LCD=d;
RS=1;
RW=0;
EN=1;
for(i=0;i<100;i++);
EN=0;
}

Explanation of code:

- Above code specify the implementation of 16x2 LCD with 8051 microcontroller. In this, you have to use Data pins of LCD (D0-D7) and RS, R/W and EN. The command and data function are necessary to define before the main function they define the pattern of alphabets on LCD. Command values are given in the hexadecimal chart above. And Data is what you want to display on LCD.

Now, RS=0 for command values and RS=1 for data values. EN is enabled to see the character on LCD and disabled after sometimes so that new values can be passed.

Comments

Popular posts from this blog

Buzzer interface with 8051 microcontroller

Buzzer  is a electronic device that converts the electronic signal into buzzing noise, that is applied to it. It can be used as electronic bell or as quiz buzzer in many applications around us. Here, i world like to discuss the interfacing of a small buzzer with 8051 microcontroller and how different projects can be constructed. Buzzer Interfacing: This project shows the interface with AT89S52 microcontroller to a buzzer. When a push button is pressed, the buzzer will get ON and OFF ( number of times set in the code ) and then stops. Circuit Diagram: - The port P1 of the microcontroller is connected to buzzer. This type of connection is possible, if the current requirements of the buzzer is not more than 20mA. The output is in current source mode so that buzzer will turn ON when the output of the port is logic LOW. Switch is connected to port P3 which remains at logic HIGH by pull up resistor.  Code: #include "REG52.h" #define buz P1 sbit SW=P3^0; long i...

Different ways to generate delays in 8051

The delay length in 8051 microcontroller depends on three factors: The crystal frequency the number of clock per machine the C compiler. The original 8051 used 1/12 of the crystal oscillator frequency as one machine cycle. In other words, each machine cycle is equal to 12 clocks period of the crystal frequency connected to X1-X2 pins of 8051. To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one. The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051, regardless of the 8051 version. In other words, AT89C51, DS5000, and DS89C4x0 the duration of the time to execute an instruction varies, but they all use 1/12th of the crystal's oscillator frequency for the clock source. 8051 has two different ways to generate time delay using C programming, regardless of 8051 version. The first method is simply using Loop   program function in which Delay() funct...

How to Switch OFF Citizen Calculator

You might be confused on seeing no OFF button on your calculator. Actually, these are general calculator available at low cost in the market. They process simple calculations like division, subtraction, Addition, Multiplication. But, how will you OFF that calculator having no option on them. There is always a trick which OFF them. It depends on the company calculator and their series to which they belong. I can tell you about Citizen Calculator CT-500 (as you can see in the image), how can you switch OFF it and can save battery too. If you leave calculator idle for 6-7 minutes, it will goes OFF automatically. My calculator goes OFF in 6.50 minutes from the point of leaving it idle. But, this is not the power saving trick for these types of calculator having not much backup battery. To save the power of your normal calculator, here is the trick to switch OFF it. Press ' /(divide sign)', 'x(multiply sign)', '%(percentage sign)', 'autoreplay option(...