www.sakshieducation.com LCD BY

www.sakshieducation.com
w
w
w
.s
a
ks
hi
ed
uc
at
io
n.
co
m
LCD
BY
D. BALAKRISHNA,
RESEARCHER,
IIIT-H
www.sakshieducation.com
Page: 1 www.sakshieducation.com
CONTENTS
Chapter I:
Introduction
Chapter II: LCD module
JHD162A
Topic (a)
Block Diagram
Topic (b)
Pin configuration
Chapter III: Working
Modes
Topic (a)
8-bit
Topic (b)
4-bit
ed
uc
at
io
n.
Lesson 1)
Lesson 2)
Registers
Lesson 3)
Instruction set
Lesson 4)
Memory
Topic (a)
DDRAM
Topic (b)
CGROM
Topic (c)
CGRAM
Chapter IV: Programming
Interfacing LCD
Lesson 2)
Initializing LCD
ks
hi
Lesson 1)
Topic (a)
co
m
Lesson 1)
Sending Data
.s
a
Topic (b)
Sending Command
Lesson 3)
Alphanumeric
Topic (b)
Strings
w
Topic (a)
w
w
Lesson 4)
Write LCD
Scrolling
Lesson 5)
Customized Characters
Lesson 6)
Test
www.sakshieducation.com
Page: 2 www.sakshieducation.com
CHAPTER 1
INTRODUCTION
LCD (Liquid Crystal Display) is very popular for displaying device in Embedded Applications.
LCDs are very cheap and easy to interface with microcontrollers; LCDs are widely used in
devices like telephones, vending machines, washing machines, toys etc.
co
m
LCD comes in several varieties i.e. 16*2, 20*2, 20*4 etc. These different LCD varieties can
display different number of characters i.e. 16*2 can display 32 characters at a time.
This module covers the introduction of LCD, its working and how to program it.
LCD MODULE
ed
uc
at
io
n.
CHAPTER 2
The LCD module that we will be using is 16*2 JHD162A. This module has 2 rows and 16
columns of display blocks. Each block can be used to display 1 character. So there are total 32
such type of blocks.
One block has 8*5 pixels. Depending on which pixel is ON and which is OFF we can display
different type of characters like Alphabets & Numeric.
2.1
JHD162A
w
w
w
.s
a
ks
hi
This model has a green backlight that helps us to get the display even in dark. Internally this
module consists of a controller chip; a segment driver chip, LCD display and some passive
components (Don’t worry about these things, generally no need to study about these things).
Figure 2.1: LCD module
www.sakshieducation.com
Page: 3 www.sakshieducation.com
2.1.1 BLOCK DIAGRAM
The JHD162A LCD module consists 32 blocks (16*2). Each of the 32 blocks is a memory, as
soon as we write an ASCII number into one of these 32 memory locations the corresponding
character is displayed on that block.
ed
uc
at
io
n.
co
m
The function of displaying the character after decoding the data is done by an onboard controller
chip.
Figure 2.1.1: Internal block diagram of LCD
In reality the LCD module consists of several memory locations apart from the 32 dedicated for
the display.
hi
The various operations that we need to perform are:
ks
1) Configure LCD module by writing commands to memory location.
2) Write to LCD memory to display data.
.s
a
3) Perform some special operations like Clear Screen, Bring Cursor to starting position etc.
w
2.1.2 PIN CONFIGURATION
w
w
There are total 16 pins in this LCD module.
Figure 2.1.2:- LCD with its pin configuration
www.sakshieducation.com
Page: 4 www.sakshieducation.com
The following table describes the pin configuration of the LCD Module
Pin No Symbol
Description
Function
VSS
Ground
0V (GND)
2
VCC
Power supply for Logic Circuit
+5V
3
VEE
LCD Contrast Adjustment
4
RS
INSTRUCTION/DATA Register
Selection
RS = 0 : INSTR Register
READ/WRITE Selection
R/W = 0 : Register WRITE
R/W
RS = 1 : DATA Register
ed
uc
at
io
n.
5
co
m
1
R/W = 1 : Register READ
DB0
8
DB1
9
DB2
10
DB3
11
DB4
12
DB5
13
DB6
14
DB7
15
Sends data to data pins when a high
to low pulse applied
hi
7
ENABLE Signal
DATA INPUT/OUTPUT LINES
ks
E
8 BIT: DB0-DB7
w
.s
a
6
Supply Voltage For LED+
+5V
LED-
Supply Voltage For LED-
0V
w
w
LED+
16
Table1:- The Pin Configuration of the LCD Module
Pins of LCD module are categorized in to three types:
•
•
•
Power supply pins
Data pins
Control pins
www.sakshieducation.com
Page: 5 www.sakshieducation.com
Power supply pins
Pins 1 – 3, 15 & 16 comes under this category
VCC & VSS are used supply power to LCD module
VEE is used to control the contrast of LCD display.
Data pins
co
m
LED+ & LED- are for backlight of LCD
ed
uc
at
io
n.
Pins 7 – 14 (DB0 – DB7) are used for the data transfer between LCD & microcontroller.
Control pins
Pins 4, 5 & 6 are control pins in LCD module.
These are:
RS (Register Select)
R/W (Read/Write)
E (Enable)
RS: (Register Select)
hi
•
•
•
RS = 0: Selects Instruction register
RS = 1: Selects Data register
.s
a
•
•
ks
This pin is used to select the Register (Instruction/Data) of LCD.
We will discuss clearly about these Registers in further.
w
R/W: (Read/Write)
w
w
This pin is used to select the mode of operation.
•
•
R/W = 0: Read mode (µC read the data from LCD)
R/W = 1: Write mode (µC writes the data to LCD)
E: (Enable)
Whenever the this pin goes High to Low, then only the data will transfer to data pins.
www.sakshieducation.com
Page: 6 www.sakshieducation.com
CHAPTER 3
WORKING
The main function of LCD is displaying information. We have already discussed that LCD
consists Blocks, each block has 8*5 pixels.
ed
uc
at
io
n.
co
m
Depending on which pixel is ON and which is OFF we can display different type of characters
like Alphabets & Numeric.
hi
Figure 3.1 (a): Blocks in 16*2 LCD
MODES
.s
a
3.1
ks
Figure 3.1 (b): Figure demonstrating how the character displayed on a pixel
There are basically 2 modes of operation in LCD.
w
Write
Read
w
w
•
•
In write mode of operation the data will write into LCD. In case of Read mode we will read the
data from LCD.
In this document we will discuss about Write mode of operation only.
The LCD has 2 modes of operation in data transfer.
•
•
8-Bit mode
4-Bit mode
www.sakshieducation.com
Page: 7 www.sakshieducation.com
3.1.1 8-BIT MODE
In this 8-bit mode we will use all the 8-data pins of the LCD module for data transfer
between microcontroller and LCD. Data transfer is very fast in this mode of operation.
•
•
•
Connect data pins of LCD to Port pins of µC
Send to the LCD port
Send enable signal
ed
uc
at
io
n.
Main drawback of this mode is:
co
m
Common steps in 8-bit mode:
Whenever we use this mode of operation we need 8-pins for data, 3-pins for control
signals and 5 pins for power supply (we can use Vcc & GND of microcontroller). So
totally we need 11-pins from microcontroller.
In our 8051 MCU 11-pins required out of 40 pins. This is wastage of system ports.
To overcome this problem we will shift to 4-Bit mode.
3.1.2 4-BIT MODE
hi
In this 4-bit mode we will use on 4-data pins of LCD for data transfer.
.s
a
ks
Whenever we use this mode of operation we need 4-pins for data 3-pins for control
signals and 5 pins for power supply (we can use Vcc & GND of microcontroller). So
totally we need 7-pins from microcontroller.
While comparing with 8-bit mode we saved 4-port pins of microcontroller.
w
Common steps in 4-bit mode:
Mask lower 4-bits
Send to the LCD port
Send enable signal
Mask higher 4-bits
Send to LCD port
Send enable signal
w
w
•
•
•
•
•
•
3.2
REGISTERS
There are mainly 2 registers in our LCD module.
•
Instruction register
www.sakshieducation.com
Page: 8 www.sakshieducation.com
•
Data register
By using RS pin we can toggle these two registers (Refer pin configuration).
Instruction/Command Register
co
m
It stores the data at the data pins of LCD. Here data is called as command. A command is an
instruction given to LCD to do a predefined task like clearing the screen, setting the cursor
position etc.
Data Register
ed
uc
at
io
n.
It stores the data to be displayed on the LCD. The LCD considered the given data as ASCII
values only.
If we want display any character we should send the ASCII value of that character.
INSTRUCTION SET
Instruction
Hex
Dec
1
Function Set: 8-bit, 1 Line, 5x7 Dots
0x30
48
2
Function Set: 8-bit, 2 Line, 5x7 Dots
0x38
56
3
Function Set: 4-bit, 1 Line, 5x7 Dots
0x20
32
4
Function Set: 4-bit, 2 Line, 5x7 Dots
0x28
40
Entry Mode
0x06
6
Display off Cursor off
(clearing display without clearing DDRAM content)
0x08
8
Display on Cursor on
0x0E
14
Display on Cursor off
0x0C
12
9
Display on Cursor blinking
0x0F
15
10
Shift entire display left
0x18
24
11
Shift entire display right
0x1C
30
12
Move cursor left by one character
0x10
16
13
Move cursor right by one character
0x14
20
ks
No.
hi
3.3
7
w
w
8
w
6
.s
a
5
www.sakshieducation.com
Page: 9 www.sakshieducation.com
14
Clear Display (also clear DDRAM content)
0x01
1
15
Set DDRAM address or cursor position on display
0x80+add
128+add
16
Set CGRAM address or set pointer to CGRAM location
0x40+add
64+add
Table 2:- Instruction Set
MEMORY
There are three types of memories in LCD module.
DDRAM
CGROM
CGRAM
ed
uc
at
io
n.
•
•
•
co
m
3.4
3.4.1 DDRAM: Display Data RAM
hi
Display data RAM (DDRAM) stores display data in 8-bit character codes. Its capacity is 80x8
bits, or 80 characters. Whatever you send to the DDRAM will display on the LCD. For LCDs
like 1x16, only 16 characters are visible, so whatever you write after 16 characters in DDRAM is
not visible to the user.
ks
Figure 3.4.1: DDRAM Address for 2 Lines LCD
.s
a
3.4.2 CGROM: Character Generator ROM
Whenever we send an ASCII value to DDRAM, how the character is displayed on LCD? The
answer is CGROM.
w
w
w
The character generator ROM generates 5 x 8 dot or 5 x 10 dot character patterns from 8-bit
character codes (see below figure for more details). It can generate 208 5 x 8 dot character
patterns and 32 5 x 10 dot character patterns.
www.sakshieducation.com
Page: 10 www.sakshieducation.com
ed
uc
at
io
n.
co
m
Figure 3.4.2: LCD characters code map for 5x8 dots
3.4.3 CGRAM: Character Generator RAM
ks
hi
CGRAM is used to create custom characters in LCD. In the character generator RAM, the user
can program character patterns. For 5 x 8 dots, eight character patterns can be written.
.s
a
Later in this tutorial we will discuss how to use CGRAM area to make custom character and also
making animations.
w
As we can see in above figure, the character code from 0x00 to 0x07 is occupied by the user
defined characters. So we can create 8 characters only, if we want to create more than 8 then we
have to delete previous one.
w
w
If user wants to display the fourth custom character then the code to display 0x03 from CGROM
i.e. when user sends 0x03 to the DDRAM then the fourth user created pattern will be displayed
on the LCD.
www.sakshieducation.com
Page: 11 www.sakshieducation.com
CHAPTER 4
PROGRAMMING
4.1
co
m
In this section we will discuss about how to interface LCD to 8051 microcontroller and how to
program the LCD.
LCD INTERFACING
ed
uc
at
io
n.
In this project LCD is working in 4-bit mode i.e., the data transferred to the LCD must be in 4-bit
data form. The PORT 0 of 8051 is connected to data pins and control pins (Rs, R/W and En) of
LCD.
w
w
w
.s
a
ks
hi
Here in 4-bit mode we are using upper nibble of data pins of LCD is connected to lower nibble
of port pins of 8051 shown in figure below.
Figure 4.1: Circuit diagram if LCD interfacing
In this project the hardware connections are given below.
LCD Pin
RS
R/W
E
D7
D6
D5
D4
8051 Pin
P0.7
P0.6
P0.5
P0.3
P0.2
P0.1
P0.0
www.sakshieducation.com
Page: 12 www.sakshieducation.com
D3
-D2
-D1
-D0
-Table 4.1: Hardware connections of LCD interfacing to 8051
4.2
INITIALIZING LCD
Initialize the port as output.
Register selection (select instruction register).
Read/Write mode selection (select write mode).
Select 4-bit mode of operation.
Turn display ON & cursor ON.
Clear screen.
ed
uc
at
io
n.
•
•
•
•
•
•
co
m
The following instructions are used to initialize LCD.
Initialize port
In this project LCD connected to PORT 0, here we are using LCD in write mode only. So
we have to assign PORT 0 as output. In 8051 by default all the ports are acts as output
ports, so no need to code again.
Register selection
ks
hi
In initialization of LCD we have to send commands to LCD, so we can select instruction
register by making RS = 0,
.s
a
P0^7 = 0;
Observe above figure RS pin of the LCD connected to P0.7.
w
Read/Write mode
w
w
Here we are using Write mode only. So select write mode by clearing R/W pin.
P0^6 = 0;
Observe above figure R/W pin of the LCD connected to P0.6.
Before going to data mode selection we have to discuss about how to send Data/Command to
LCD module.
Sending Command
By using Data pins (DB0 – DB7) only we can send data/command to the LCD module.
www.sakshieducation.com
Page: 13 www.sakshieducation.com
Steps1:
Divide 8-bit data (here command) into 2 parts.
int l, u;
u = c & 0xF0; //Separates upper nibble
co
m
u = u >> 4; //format data
l = c & 0x0F; //Lower nibble
Step 2:
ed
uc
at
io
n.
Send upper nibble and toggle E pin.
Note: RS pin should be 0 because we are sending command, so we have to select
command register.
P0 = (P0 & 0xF0)| u;
//send upper nibble
clear_RS ();
//Clear RS pin
clear_RW ();
//Clear RW pin
Toggle_pin_e ();
hi
Step 3:
//Toggle E pin
ks
Send lower nibble and toggle E pin.
.s
a
P0 = (P0 & 0xF0)| l;
//send lower nibble
//Clear RS pin
clear_RW ();
//Clear RW pin
w
clear_RS ();
//Toggle E pin
w
w
Toggle_pin_e ();
Sending Data
The main difference between Sending Command & Data is RS pin selection. In case of
sending Command RS should be 0.
In case of sending Data RS should be 1.
Except this difference everything is common in sending Data & Command
www.sakshieducation.com
Page: 14 www.sakshieducation.com
Complete code of LCD initialization
void LCD_init(void)
{
clear_RS ();
clear_RW ();
delay_ms (2);
// Clear RS pin
// Clear RW pin
LCD_Send_Cmd (0x28);
// Clear Screen
// turn display on + turn cursor on +
// turn cursor blinking
// 4 bit mode + 2 line display + 5*7 font
}
WRITE LCD
ed
uc
at
io
n.
4.3
co
m
LCD_Send_Cmd (0x01);
LCD_Send_Cmd (0x0F);
Alphanumeric
Writing LCD is nothing but a sending data to LCD. By using the following code we can print
single alphanumeric character on LCD screen.
hi
void LCD_Send_Data (char c)
{
int l,u;
u = c & 0xF0;
u = u >> 4;
l = c & 0x0F;
.s
a
ks
P0 = (P0 & 0xF0)| u;
set_RS ();
clear_RW ();
Toggle_pin_e ();
w
w
w
P0 = (P0 & 0xF0)| l;
set_RS ();
clear_RW ();
Toggle_pin_e ();
}
Strings
Printing strings is nothing but a printing of characters one-by-one. So by using printing character
code in some loop we can print string.
www.sakshieducation.com
Page: 15 www.sakshieducation.com
void LCD_Send_String1(char *s)
{
while(*s!='\0')
{
LCD_Send_Data(*s);
s++;
}
}
SCROLLING
ed
uc
at
io
n.
4.4
co
m
Ex:
We knew that our JHD162A module can print 16 characters in one line. If our length of string is
more than 16 characters we will go to the second line. Totally we can print up to 32 characters.
Now the real question comes into the picture, if our string contains more than 32 characters, then
what we have to do? The answer is SCROLLING.
We might observe in railway stations, the train names & timings scrolls on screen. Same concept
we have to implement here.
Functionality
ks
First we will print first character
Clear screen
Print next character along with first character and so on
.s
a
•
•
•
hi
In reality there is no scrolling. It’s our human eye illusion. The logic behind the scrolling is:
Let us take one example for clarity:
w
Scroll “Welcome”
w
w
First of all we have to print “W”, then clear screen then print “We”….(Before printing every
time we have to clear the LCD screen)
www.sakshieducation.com
Page: 16 www.sakshieducation.com
co
m
ed
uc
at
io
n.
Figure 4.4: Process of how letters scroll in LCD
Complete code
hi
void LCD_Scroll (unsigned char input[])
{
unsigned char print[16]="
";
unsigned char i, x;
unsigned char len = strlen (input);
LCD_init ();
LCD_Send_Cmd (0x0C);
LCD_Send_Cmd (0x01);
.s
a
ks
for (x=0;x<len;x++)
{
for(i=0;i<15;i++)
print[i]=print[i+1];
print[15]=input[x];
w
w
w
LCD_GoTo_XY(1,1);
LCD_Send_String (print);
delay_ms (300);
LCD_Send_Cmd (0x01);
}
delay_ms (300);
}
www.sakshieducation.com
Page: 17 www.sakshieducation.com
4.5
CUSTOMIZED CHARACTERS
In LCD module we can print our own designed characters/symbols called “Customized
Characters”. By using CGRAM we can create our own customized characters. CGRAM can
store up to 8 characters (0x00 – 0x07).
ed
uc
at
io
n.
The following figure is best example to create customized characters.
co
m
How to create customized characters?
hi
Figure 4.5: The example of customized character
ks
Depending on which pixel is ON and which is OFF we can display different type of characters.
Here we have to consider rows only. Totally we had 8 rows, so we will get 8 values.
.s
a
Consider 1st block in above figure. The row values of 1st block will be:
unsigned char spl1[]={0x04, 0x03, 0x0F, 0x07, 0x0F, 0x12, 0x12, 0x0E };
w
w
w
We can take these values as an array, and send to CGRAM. Then the CGRAM will store the
values as characters.
Code:
void LCD_Cust_Char_Create(unsigned char cgram, unsigned char data[])
{
unsigned char i;
LCD_Send_Cmd (cgram); // Address where customized character is to be stored
for (i=0;i<8;i++)
LCD_Send_Data (data[i]);
_delay_ms (10);
}
www.sakshieducation.com
Page: 18 www.sakshieducation.com
In above code LCD_Cust_Char_Create function is there. In that function the arguments are
cgram & data.
Here the argument cgram indicates the CGRAM location where the character will store. The
argument data indicates the value of Block.
After creation of customized characters we will get one doubt.
co
m
How to print customized character?
After creation of customized character (stored in the CGRAM) we will print those characters
simply by sending the data of CGRAM (where the character stored).
ed
uc
at
io
n.
Code:
void LCD_Cust_Char_Print(unsigned char character, unsigned char Pos1,unsigned
char Pos2)
{
LCD_GoTo_XY (Pos1, Pos2); // Location of LCD where the character is to be
displayed
LCD_Send_Data (character);
}
In above code LCD_Cust_Char_Print function is there. In that function the arguments are
character, Pos1 & Pos2.
ks
hi
Here the argument character (0 - 7) indicates the character to be print. Argument Pos1indicates
the position of row and argument Pos2 indicates the position of column.
.s
a
In above code another function LCD_GoTo_XY is there. By using this function we will move
the cursor in to particular position by using arguments. The code of LCD_GoTo_XY function is
given below.
w
w
w
void LCD_GoTo_XY(unsigned int x, unsigned int y)
{
if(y==1)
LCD_Send_Cmd(x+127);
else if(y==2)
LCD_Send_Cmd(x+191);
}
The complete code to display the above example: (Figure 4.5)
#include<reg51.h>
#include “delay.h”
#include “lcd.h”
unsigned char spl1[]={0x04, 0x03, 0x0F, 0x07, 0x0F, 0x12, 0x12, 0x0E };
unsigned char spl2[]={0x1F, 0x1F, 0x1F, 0x1F, 0x03, 0x00, 0x0C, 0x12 };
www.sakshieducation.com
Page: 19 www.sakshieducation.com
unsigned char spl3[]={0x00, 0x18, 0x1C, 0x1E, 0x12, 0x02, 0x0C, 0x14 };
unsigned char spl4[]={0x1E, 0x0F, 0x1E, 0x0C, 0x06, 0x02, 0x01, 0x00 };
unsigned char spl5[]={0x16, 0x0C, 0x00, 0x11, 0x18, 0x1E, 0x01, 0x1F };
unsigned char spl6[]={0x16, 0x0C, 0x04, 0x12, 0x02, 0x04, 0x18, 0x00 };
LCD_init ();
delay_ms (50); // delay of 50 mille seconds
LCD_Send_Cmd (0x0C);
ed
uc
at
io
n.
while (1)
{
LCD_Cust_Char_Create (64, spl1);
LCD_Cust_Char_Print (0, 5, 1);
co
m
int main(void)
{
LCD_Cust_Char_Create (72, spl2);
LCD_Cust_Char_Print (1, 6, 1);
LCD_Cust_Char_Create (80, spl3);
LCD_Cust_Char_Print (2, 7, 1);
hi
LCD_Cust_Char_Create (88, spl4);
LCD_Send_Cmd (0x10);
LCD_Cust_Char_Print (3, 5, 2);
ks
LCD_Cust_Char_Create (96, spl5);
LCD_Send_Cmd (0x10);
LCD_Cust_Char_Print (4, 6, 2);
w
w
w
}
}
.s
a
LCD_Cust_Char_Create (104, spl6);
LCD_Send_Cmd (0x10);
LCD_Cust_Char_Print (5, 7, 2);
In above code one header files “lcd.h” & “delay.h” were included. These two header files are
user defined.
www.sakshieducation.com
Page: 20 www.sakshieducation.com
CHAPTER 6
TEST
1. How many pins the LCD JHD162 consists?
Ans: 16
2. If r/w =1 it represents what?
3. How many data pins are there to send the information to LCD?
Ans: 8
4. What is the condition to set the LCD in command mode?
ed
uc
at
io
n.
Ans: RS=0
co
m
Ans: Register READ
5. What is the LCD command code to make the LCD screen clear?
Ans: 0x01
6. What is the command code instruction to set LCD in 5x8 block size,8 bit operation,
Ans: 0x38
7. What is the command code instruction to set LCD in data mode?
Ans: RS=1
8. E pin is an ------------------- pin of LCD.
hi
Ans: Enable
Ans: 0x80
ks
9. Which of the following is the address of first row first column in 16x2 LCD?
.s
a
10. Which of the following is the second row 5 column of LCD?
w
w
w
Ans: 0xC5
www.sakshieducation.com
Page: 21