Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

4 Pin LCD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

  (/wiki)   (/shop)

Interfacing LCD in 4-bit mode with Contents

8051
As per the name the 2x16 has 2 lines with 16 chars on each line. It supports all the ASCII chars
and is basically used for displaying the alphanumeric characters. Here each character is
displayed in a matrix of 5x7 pixels. Apart from alphanumeric chars it also provides the
provision to display the custom characters by creating the pattern.

LCD UNIT
Let us look at a pin diagram of a commercially available LCD like JHD162 which uses a
HD44780 controller and then describe its operation.

(/wiki/File:Pic16f877aLcdInterface.png)

Pin Number Symbol Pin Function

1 VSS Ground

2 VCC +5v

3 VEE Contrast adjustment (VO)

4 RS Register Select. 0:Command, 1: Data

5 R/W Read/Write, R/W=0: Write & R/W=1: Read

6 EN Enable. Falling edge triggered 


7 D0 Data Bit 0 (Not used in 4-bit operation)

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 1/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

8 D1 Data Bit 1 (Not used in 4-bit operation)

9 D2 Data Bit 2 (Not used in 4-bit operation)

10 D3 Data Bit 3 (Not used in 4-bit operation)

11 D4 Data Bit 4

12 D5 Data Bit 5

13 D6 Data Bit 6

14 D7 Data Bit 7/Busy Flag

15 A/LED+ Back-light Anode(+)

16 K/LED- Back-Light Cathode(-)

Apart from the voltage supply connections the important pins from the programming
perspective are the data lines(8-bit Data bus), Register select, Read/Write and Enable pin.

Data Bus: As shown in the above gure and table, an alphanumeric LCD has an 8-bit data bus
referenced as D0-D7. As it is an 8-bit data bus, we can send the data/cmd to LCD in bytes. It
also provides the provision to send the data/cmd in chunks of 4-bit, which is used when there
are limited number of GPIO lines on the microcontroller.

Register Select(RS): The LCD has two register namely a Data register and Command register.
Any data that needs to be displayed on the LCD has to be written to the data register of LCD.
Command can be issued to LCD by writing it to Command register of LCD. This signal is used
to differentiate the data/cmd received by the LCD.
If the RS signal is LOW then the LCD interprets the 8-bit info as Command and writes it
Command register and performs the action as per the command.
If the RS signal is HIGH then the LCD interprets the 8-bit info as data and copies it to data
register. After that the LCD decodes the data for generating the 5x7 pattern and nally
displays on the LCD.

Read/Write(RW): This signal is used to write the data/cmd to LCD and reads the busy ag of
LCD. For write operation the RW should be LOW and for read operation the R/W should be
HIGH.

Enable(EN): This pin is used to send the enable trigger to LCD. After sending the data/cmd,
Selecting the data/cmd register, Selecting the Write operation. An HIGH-to-LOW pulse has to
be sent on this enable pin which will latch the info into the LCD register and triggers the LCD
to act accordingly.

Schematic
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 2/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

Below schematic shows the minimum connection required for interfacing the LCD with the
microcontroller.
As we are interfacing the LCD in 4-bit mode, only the higher 4 data lines are used as the data
bus.

Port Connection
This section shows how to con gure the GPIO for interfacing the LCD.
The below con guration is as per the above schematic. You can connect the LCD to any of the
PORT pins available on your boards and update this section accordingly

1 /* Configure the data bus and Control bus as per the hardware connection
2 Databus is connected to P2.4:P2.7 and control bus P2.0:P2.2*/
3 #define LcdDataBus P2
4 sbit LCD_RS = P2^0;
5 sbit LCD_RW = P2^1;
6 sbit LCD_EN = P2^2;

wan/2c051cee1f15cfd4329a945e2e3fa63e/raw/4704e1ac27a0312aa72478bc8886f8470b3740b5/8051_Lcd4bitConnection.c)
8051_Lcd4bitConnection.c (https://gist.github.com/SaheblalBagwan/2c051cee1f15cfd4329a945e2e3fa63e#file-
8051_lcd4bitconnection-c) hosted with ❤ by GitHub (https://github.com)

LCD Operation
In this section, we are going to see how to send the data/cmd to the LCD along with the timing
diagrams. First let's see the timing diagram for sending the data and the command
signals(RS,RW,EN), accordingly, we write the algorithm and nally the code.

Timing Diagram
The below image shows the timing diagram for sending the data to the LCD.
As shown in the timing diagram the data is written after sending the RS and RW signals. It is
still ok to send the data before these signals.
The only important thing is the data should be available on the databus before generating the
High-to-Low pulse on EN pin.

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 3/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

(/wiki/File:LCD_CmdWrite.jpg)

Steps for Sending Command:

step1: Send the I/P command to LCD.


step2: Select the Control Register by making RS low.
step3: Select Write operation making RW low.
step4: Send a High-to-Low pulse on Enable PIN with some delay_us.

1 /* Function to send the command to LCD.


2 As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */
3 void Lcd_CmdWrite(char cmd)
4 {
5 LcdDataBus = (cmd & 0xF0); //Send higher nibble
6 LCD_RS = 0; // Send LOW pulse on RS pin for selecting Command register
7 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
8 LCD_EN = 1; // Generate a High-to-low pulse on EN pin
9 delay(1000);
10 LCD_EN = 0;
11
12 delay(10000);
13

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 4/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

14 LcdDataBus = ((cmd<<4) & 0xF0); //Send Lower nibble


15 LCD_RS = 0; // Send LOW pulse on RS pin for selecting Command register
16 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
17 LCD_EN = 1; // Generate a High-to-low pulse on EN pin
18 delay(1000);
19 LCD_EN = 0;
20
21 delay(10000);
22 }

wan/4a521e33274ae73b7b492611ed20d762/raw/c12d802b957fc86e46366c96096c4cb713b6c351/8051_Lcd4BitCmdWrite.c)
8051_Lcd4BitCmdWrite.c (https://gist.github.com/SaheblalBagwan/4a521e33274ae73b7b492611ed20d762#file-
8051_lcd4bitcmdwrite-c) hosted with ❤ by GitHub (https://github.com)

Steps for Sending Data:

step1: Send the character to LCD.


step2: Select the Data Register by making RS high.
step3: Select Write operation making RW low.
step4: Send a High-to-Low pulse on Enable PIN with some delay_us.
The timings are similar as above only change is that RS is made high for selecting Data
register.

1 /* Function to send the Data to LCD.


2 As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */
3 void Lcd_DataWrite(char dat)
4 {
5 LcdDataBus = (dat & 0xF0); //Send higher nibble
6 LCD_RS = 1; // Send HIGH pulse on RS pin for selecting data register
7 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
8 LCD_EN = 1; // Generate a High-to-low pulse on EN pin
9 delay(1000);
10 LCD_EN = 0;
11
12 delay(10000);
13
14 LcdDataBus = ((dat<<4) & 0xF0); //Send Lower nibble
15 LCD_RS = 1; // Send HIGH pulse on RS pin for selecting data register
16 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
17 LCD_EN = 1; // Generate a High-to-low pulse on EN pin

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 5/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

18 delay(1000);
19 LCD_EN = 0;
20
21 delay(10000);
22 }

wan/19146e69651dc5465bdac99f75c31a29/raw/777921f2b8c0804936a3d8d5d0c73bb7020d7f9b/8051_Lcd4BitDataWrite.c)
8051_Lcd4BitDataWrite.c (https://gist.github.com/SaheblalBagwan/19146e69651dc5465bdac99f75c31a29#file-
8051_lcd4bitdatawrite-c) hosted with ❤ by GitHub (https://github.com)

Hardware Connections

(/wiki/File:Lcd_4-Bit_Tutorial.png)

Code Examples
Here is the complete code for displaying the data on 2x16 LCD in 4-bit mode.

1 #include<reg51.h>
2 /* Configure the data bus and Control bus as per the hardware connection

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 6/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

3 Databus is connected to P2.4:P2.7 and control bus P2.0:P2.2*/


4 #define LcdDataBus P2
5 sbit LCD_RS = P2^0;
6 sbit LCD_RW = P2^1;
7 sbit LCD_EN = P2^2;
8
9
10 /* local function to generate delay */
11 void delay(int cnt)
12 {
13 int i;
14 for(i=0;i<cnt;i++);
15 }
16
17
18
19 /* Function to send the command to LCD.
20 As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */
21 void Lcd_CmdWrite(char cmd)
22 {
23 LcdDataBus = (cmd & 0xF0); //Send higher nibble
24 LCD_RS = 0; // Send LOW pulse on RS pin for selecting Command register
25 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
26 LCD_EN = 1; // Generate a High-to-low pulse on EN pin
27 delay(1000);
28 LCD_EN = 0;
29
30 delay(10000);
31
32 LcdDataBus = ((cmd<<4) & 0xF0); //Send Lower nibble
33 LCD_RS = 0; // Send LOW pulse on RS pin for selecting Command register
34 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
35 LCD_EN = 1; // Generate a High-to-low pulse on EN pin
36 delay(1000);
37 LCD_EN = 0;
38
39 delay(10000);
40 }
41
42
43 /* Function to send the Data to LCD.
44 As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 7/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

45 void Lcd_DataWrite(char dat)


46 {
47 LcdDataBus = (dat & 0xF0); //Send higher nibble
48 LCD_RS = 1; // Send HIGH pulse on RS pin for selecting data register
49 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
50 LCD_EN = 1; // Generate a High-to-low pulse on EN pin
51 delay(1000);
52 LCD_EN = 0;
53
54 delay(10000);
55
56 LcdDataBus = ((dat<<4) & 0xF0); //Send Lower nibble
57 LCD_RS = 1; // Send HIGH pulse on RS pin for selecting data register
58 LCD_RW = 0; // Send LOW pulse on RW pin for Write operation
59 LCD_EN = 1; // Generate a High-to-low pulse on EN pin
60 delay(1000);
61 LCD_EN = 0;
62
63 delay(10000);
64 }
65
66
67
68 int main()
69 {
70 char i,a[]={"Good morning!"};
71
72 LcdDataBusDirnReg = 0x00; // Configure all the LCD pins as output
73
74
75 Lcd_CmdWrite(0x02); // Initialize Lcd in 4-bit mode
76 Lcd_CmdWrite(0x28); // enable 5x7 mode for chars
77 Lcd_CmdWrite(0x0E); // Display OFF, Cursor ON
78 Lcd_CmdWrite(0x01); // Clear Display
79 Lcd_CmdWrite(0x80); // Move the cursor to beginning of first line
80
81
82 Lcd_DataWrite('H');
83 Lcd_DataWrite('e');
84 Lcd_DataWrite('l');
85 Lcd_DataWrite('l');
86 Lcd_DataWrite('o');

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 8/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

87 Lcd_DataWrite(' ');
88 Lcd_DataWrite('w');
89 Lcd_DataWrite('o');
90 Lcd_DataWrite('r');
91 Lcd_DataWrite('l');
92 Lcd_DataWrite('d');
93
94 Lcd_CmdWrite(0xc0); //Go to Next line and display Good Morning
95 for(i=0;a[i]!=0;i++)
96 {
97 Lcd_DataWrite(a[i]);
98 }
99
100 while(1);
101 }

gwan/3827aa5155b70f2745ea53ef2dd44574/raw/79cc2b57123fc8a91908f8d4630539c1806677e3/8051_Lcd4BitExample1.c)
8051_Lcd4BitExample1.c (https://gist.github.com/SaheblalBagwan/3827aa5155b70f2745ea53ef2dd44574#file-
8051_lcd4bitexample1-c) hosted with ❤ by GitHub (https://github.com)

Using Explore Embedded Libraries :


In the above tutorial, we just discussed how to interface 2x16Lcd in 4-bit mode.
Once you know the working of LCD, you can directly use the ExploreEmbedded libraries to
play around with your LCD.
For that you need to include the lcd.c/lcd.h and the associated les(delay/stdutils).

The below sample code shows how to use the already available LCD functions.

LCD 1x16

1 #include "lcd.h"
2
3 int main()
4 {
5 /*Connect RS->P2.0, RW->P2.1, EN->P2.2 and data bus to P2.4 to P2.7*/
6 LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P2_4,P2_5,P2_6,P2_7);
7 LCD_Init(1,16);
8
9 LCD_DisplayString("Explore Embedded\n");
10 while(1);

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 9/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

11
12 return (0);
13 }

SaheblalBagwan/3329928508b185f1fad661a07f28d6c6/raw/59607f067c4c1f0297081043292ed0abff4862ff/8051_lcd_1x16.c)
8051_lcd_1x16.c (https://gist.github.com/SaheblalBagwan/3329928508b185f1fad661a07f28d6c6#file-8051_lcd_1x16-c)
hosted with ❤ by GitHub (https://github.com)

LCD 2x16

1 #include "lcd.h"
2
3 int main()
4 {
5 /*Connect RS->P2.0, RW->P2.1, EN->P2.2 and data bus to P2.4 to P2.7*/
6 LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P2_4,P2_5,P2_6,P2_7);
7 LCD_Init(1,16);
8
9 LCD_DisplayString("Explore Embedded");
10 LCD_GoToLine(1);
11 LCD_DisplayString("Lcd 4-Bit mode");
12 while(1);
13
14 return (0);
15 }

heblalBagwan/05cb28c2b0ee15b69bd630c498e1bcdb/raw/b1c87916efcb0d7ff8565d682d331eeec3e2f0ef/8051_lcd_2x16.c)
8051_lcd_2x16.c (https://gist.github.com/SaheblalBagwan/05cb28c2b0ee15b69bd630c498e1bcdb#file-8051_lcd_2x16-c)
hosted with ❤ by GitHub (https://github.com)

LCD 4x20

1 #include "lcd.h"
2
3 int main()
4 {
5 /*Connect RS->P2.0, RW->P2.1, EN->P2.2 and data bus to P2.4 to P2.7*/
6 LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P2_4,P2_5,P2_6,P2_7);
7 LCD_Init(4,20);
8
9 LCD_DisplayString("Explore Embedded\n");
10 LCD_DisplayString("LCD 4-bit Mode\n");

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 10/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

11 LCD_DisplayString("20 x 4 \n");
12 LCD_DisplayString(":) :O");
13 while(1);
14
15 return (0);
16 }

heblalBagwan/7c4da4bc35c7ffc5e964e1c31c673497/raw/582f1db625d0b5633e9550afbd2ef1bebe1635f6/8051_lcd_4x20.c)
8051_lcd_4x20.c (https://gist.github.com/SaheblalBagwan/7c4da4bc35c7ffc5e964e1c31c673497#file-8051_lcd_4x20-c)
hosted with ❤ by GitHub (https://github.com)

(/wiki/File:01LCD_4bit.png)

Downloads
Download the sample code and design les from this link
(https://github.com/ExploreEmbedded/8051_DevelopmentBoard).

Have an opinion, suggestion , question or feedback about the article let it out here!

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 11/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

4 Comments exploreembedded.com/wiki 
1 Login

 Recommend 1 t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Kunal Baviskar • a year ago


Can You suggest how to Interface an LCD in 4-bit mode using Assembly Language?
△ ▽ • Reply • Share ›

Explorer Mod > Kunal Baviskar • a year ago


Refer "The 8051 Microcontroller and Embedded Systems: Using Assembly and C"
book by Mazadi.
You will find the 8-bit Lcd interface example. After going through this tutorial you
should be able to adapt it for 4-bit mode.
△ ▽ • Reply • Share ›

Kunal Baviskar > Explorer • a year ago


I had referred that book,I had even written the code for 4-bit. But the
problem is during simulation data is available at the pin but its not displaying
on LCD.
△ ▽ • Reply • Share ›

Explorer Mod > Kunal Baviskar • a year ago

Could be delay problem. Don't check the busy flag. Give some extra
delay after each nibble write and char write.
△ ▽ • Reply • Share ›

ALSO ON EXPLOREEMBEDDED.COM/WIKI

Interfacing Seven Segment Displays with Robo with Hornbill ESP32


AVR 3 comments • 2 years ago
1 comment • 2 years ago Aiswarya M — Is this procedure and code
MD MASUM BILLAH — Your Tutorial is very applicable for NodeMCU (ESP8266) ???
helpful. Thanks a lot.

AWS IOT with Arduino ESP32 FunTime, a DIY wall clock that doubles up
22 comments • a year ago as automatic night lamp
prerana gawale — I'm getting following 1 comment • 2 years ago
Error:Connected to wifiE (14135) aws_iot: Sandeep Patil — Test comment
f il d! b d l d
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 12/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials

Category (/wiki/Special:Categories): 8051 tutorials (/wiki/Category:8051_tutorials)

Subscribe to hear about our latest Explorations!

name@example.com SUBSCRIBE

Contact (/contact) About (/about) Warranty (/refund) Terms & Conditions (/terms) Reward points  (/rewards)

 (https://twitter.com/exploreembedded)  (https://www.facebook.com/ExploreEmbedded/)

 (https://www.youtube.com/channel/UCvXGpvPuosEI-ALxvCrSbaA)  (https://github.com/ExploreEmbedded)

Now shipping worldwide from India with 

https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 13/13

You might also like