LCD interfacing with 8051

What is an LCD?

LCD stands for Liquid Crystal Display. It is used as an output device. It is used to display messages. The LCD uses a Back-light through which information or messages passed are seen. In Liquid Crystal Display the screen size is 16*2 which means 16 columns and 2 rows.

Explanation of 16x2 LCD :-



Pin 1.  Vcc:- It is connected to the 5v pin.


Pin 2. GND:- It is connected to the ground. 

Pin 3. Contrast:- It controls the contrast of LCD.

Pin 4. Register select (RS):- It is used as a control signal to tell the LCD what kind of signal we are sending command or display. For command we set RS to low, and for message we set the RS pin to high

Pin 5. Read and Write (R/W):- It is used to select the operation we want to perform usually it is used for writing. For write the RW pin is set to low, and to read the data from LCD it is set to high.

Pin 6. Enable:- The EN (Enable) pin on lcd is a control signal used to initiate the data transfer. When the EN pin is set high, the lcd process the data or command after processing it it again set to low.

Pin 7. to Pin 10 Data pins( used in 8-bit only):- The data pins are used to transfer the data from the microcontroller to the LCD.

Pin 11. to Pin 14 Data pins:- The data pins are used to transfer the data from the microcontroller to the LCD.

Pin 15 Anode:- It is a positive terminal LED and blacklight.

Pin 16 Cathode:- It is a negative terminal to LED and blacklight.

Chart of LCD Commands:-

No                              HEX Value                       Command to LCD
1 0x01                Clear Display Screen
2 0x38                Set: 8-bit, 2 Line, 5x7 Dots
3 0x28                 Set: 4-bit, 2 Line, 5x7 Dots
4 0x06                 Entry Mode
5 0x08                 Display off, Cursor off
6 0x0E                 Display on, Cursor on
7 0x0C                 Display on, Cursor off
8 0x0F                 Display on, Cursor blinking
9 0x18                 Shift the entire display left
10 0x1C                 Shift the entire display right
11 0x10                 Move the cursor left by one character
12 0x14                 Move the cursor right by one character
13 0x80                 Force the cursor to the beginning of the row
14 0xC0                 Force the cursor to the beginning of 2nd row.

Connection of Liquid Crystal Display With 8051.


Difference between 8-bit LCD and 4-bit LCD:-






In an 8-bit LCD, we have 8 data lines, and the 8-bit data is sent directly to the LCD. In a 4-bit LCD, we have 4 data lines (D4-D7), and the 8-bit data cannot be sent directly to the LCD. For example, if we send y, the ASCII value of y is 89, and in binary (10001001), an 8-bit LCD will directly send the data. However, in a 4-bit LCD, it right-shifts the 4-bit and sends 4 bits to the LCD, followed by the remaining 4 bits. As compared to an 8-bit LCD, a 4-bit LCD is Slower. 

Task 1: To display a message and a string on the LCD. (For 8-bit).

 Code:-

#include<reg51.h>

#define lcd P2

sbit RS=P3^0;

sbit RW=P3^1;

sbit E=P3^2;

void delay(unsigned int t);

void lcd_begin();

void lcd_command(unsigned int cmd);

void lcd_message(unsigned char ch);

void lcd_string(unsigned char a[]);

void main()

{

lcd_begin();

lcd_message('s');

lcd_command(0xC0);

lcd_string("HELLO");

}

void lcd_begin()

{

lcd_command(0x38);

lcd_command(0x01);

lcd_command(0x0F);

lcd_command(0x80);

}

void lcd_command(unsigned int cmd)

{

lcd=cmd;

RS=0;

RW=0;

E=1;

delay(1);

E=0;

}

void lcd_message(unsigned char ch)

{

lcd=ch;                             

RS=1;

RW=0;

E=1;

delay(1);

E=0;

}

void lcd_string(unsigned char a[])

{

unsigned int i;

for(i=0;a[i]!='\0';i++)

{

lcd_message(a[i]);

}

}

void delay(unsigned int t)

{

int i,j;

for(i=0;i<=100*t;i++)

{

for(j=0;j<=1257;j++)

{

}

}

}

 Output:-




Task 2: To display a message and a string on the LCD. (For 4-bit).

 Code:-

#include<reg51.h>

#define datapins P2

sbit RS=P2^4;

sbit E=P2^5;

void delay(unsigned int t);

void lcd_begin();

void lcd_command(unsigned int cmd);

void lcd_message(unsigned char ch);

void lcd_string(unsigned char a[]);

void main()

{

lcd_begin();

lcd_command(0x01);

lcd_message('s');

lcd_command(0xC0);

lcd_string("HELLO");

while(1);

}

void lcd_begin()

{

lcd_command(0x28);

lcd_command(0x01);

lcd_command(0x0F);

lcd_command(0x80);

}

void lcd_command(unsigned int cmd)

{

datapins=(cmd>>4);

RS=0;

E=1;

delay(1);

E=0;

datapins=cmd;

RS=0;

E=1;

delay(1);

E=0;

}

void lcd_message(unsigned char ch)

{

datapins=(ch>>4);

RS=1;

E=1;

delay(1);

E=0;

datapins=ch;                             

RS=1;

E=1;

delay(1);

E=0;

}

void lcd_string(unsigned char a[])

{

unsigned int i;

for(i=0;a[i]!='\0';i++)

{

lcd_message(a[i]);

}

}

void delay(unsigned int t)

{

int i,j;

for(i=0;i<=10*t;i++)

{

for(j=0;j<=125;j++);

{

}

}

}

 Output:-