Professional Documents
Culture Documents
Introduction:LCD
Liquid Crystal Display(LCDs) provide a cost effective way to put a text output unit for a microcontroller. As
we have seen in the previous tutorial, LEDs or 7 Segments do no have the flexibility to display informative
messages. This display has 2 lines and can display 16 characters on each line. Nonetheless, when it is
interfaced with the microcontroller, we can scroll the messages with software to display information which is
more than 16 characters in length.
The LCD is a simple device to use but the internal details are complex. Most of the 16x2 LCDs use a Hitachi
HD44780 or a compatible controller. Yes, a micrcontroller is present inside a Liquid crystal display as shown
in figure 1.
The Display Controller takes commands and data from a external microcontroller and drivers the LCD
panel(LCDP). It takes a ASCII value as input and generate a patter for the dot matrix. E.g., to display letter 'A',
it takes its value 0X42(hex) or 66(dec) decodes it into a dot matrix of 5x7 as shown in figure 2.
Basic Commands
The LCD controller uses RS and RW lines along with E to operate the LCD.
The commonly used instructions are shown in the instruction set below. Observe the Bit names: I/D, S, D,
C etc at the bottom of instruction set to decode the instructions completely.
Clear Display
Cursor Home
Cursor/display shift
Function Set
Data Read
Data Write
Instruction Set
HD44780U based instruction set
Code
Instruction
R R/ B B B B B
S W 7 6 5 4 3
Clear display 0
Cursor home 0
Entry mode
set
Display
on/off
control
Description
0 0 0 0
0 0 0 0
0 0 0 0
B B B
2 1 0
0 1
0 0 0 0
Cursor/displa 0
y shift
0 0 0 1 S/ R/ *
C L
remains unchanged.
Function set
D
0 0 1
N
L
Read busy
flag &
address
counter
Write
CGRAM or
DDRAM
Write Data
Write
CGRAM or
DDRAM
Write Data
B
F
CGRAM/DDRAM
address
LCD UNIT
4
Let us look at a pin diagram of a commercially available LCD like JHD162 which uses
a HD44780 controller and then describe its operation.
All the pins are identically to the lcd internal controller discussed above
PIN NUMBER
FUNCTION
Ground
VCC
10
11
Bit 4
12
Bit 5
13
Bit 6
14
Bit 7
15
Back-light Anode(+)
16
Back-Light Cathode(-)
8 bit Mode
Objective
Schematic Discription
Data Lines: In this mode, all of the 8 datalines DB0 to DB7 are connected from
the microcontroller to a LCD module as shown the schematic.
Control Lines:' The RS, RW and E are control lines, as discussed earlier.
Power & contrast:Apart from that the LCD should be powered with 5V
between PIN 2(VCC) and PIN 1(gnd). PIN 3 is the contrast pin and is output of
center terminal of potentiometer(voltage divider) which varies voltage between 0
to 5v to vary the contrast.
Back-light: The PIN 15 and 16 are used as backlight. The led backlight can be
powered through a simple current limiting resistor as we do with normal leds.
Schematic
Code
As with all the interfaces to simplify thing we have separated code into two files, main.c and lcd_8_bit.c. You
may go through the tools setup tutorial on configuring the code.
The main.c is very simple it includes the standard library files. Then it uses several functions from the
lcd_8_bit.c file to set up and display messages. As you can see, it makes things very simple when the libraries
are well written. We will discuss the implementation while discussing the lcd_8_bit.c file.
P2
// LCD databus connected to PORT2
// Register select pin connected to P0.0
// Read Write pin connected to P0.1
// Enable pin connected to P0.2
10
2. void LCD_DataWrite( char dat) This function sends a character to be displayed on LCD in the following
steps.
The timings are similar as above only change is that RS is made high.
void LCD_DataWrite( char dat)
{
databus=dat;
// Send the data to LCD
rs=1;
// Select the Data Register by pulling RS HIGH
rw=0;
// Select the Write Operation by pulling RW LOW
en=1;
// Send a High-to-Low Pusle at Enable Pin
delay_us(10);
en=0;
delay_ms(1);
}
3. void LCD_Init()
Looking at the instruction set of the LCD controller, we can initialize the LCD with following steps
// LCD
//
//
//
11
The remaining code is listed below. You could observe that the basic functions to read
data and write write command are used extensively and entire library is built upon
them. There are also other functions that are used to display number etc in the lcd
library file.
#include<reg51.h> #include "delay.h" #include "lcd.h" #define databus P2 // LCD
databus connected to PORT2 sbit rs= P0^0; // Register select pin connected to P0.0
sbit rw= P0^1; // Read Write pin connected to P0.1 sbit en= P0^2; // Enable pin
connected to P0.2
/* 16x2 LCD Specification */ #define LCDMaxLines 2 #define
LCDMaxChars 16 #define LineOne 0x80 #define LineTwo 0xc0 #define BlankSpace ' '
void LCD_Init() { delay_us(5000); LCD_CmdWrite(0x38); // LCD 2lines, 5*7 matrix
LCD_CmdWrite(0x0E); // Display ON cursor ON Blinking off LCD_CmdWrite(0x01); // Clear
the LCD LCD_CmdWrite(0x80); // Cursor to First line First Position }
void
LCD_CmdWrite( char cmd) { databus=cmd; // Send the command to LCD rs=0; // Select
Command Register by pulling RS LOW rw=0; // Select Write Operation by pulling RW
LOW en=1; // Send a High-to-Low Pusle at Enable Pin delay_us(10); en=0;
delay_ms(1); }
void LCD_DataWrite( char dat) { databus=dat; // Send the data to
LCD rs=1; // Select the Data Register by pulling RS HIGH rw=0; // Select the Write
Operation by pulling RW LOW en=1; // Send a High-to-Low Pusle at Enable Pin
delay_us(10); en=0; delay_ms(1); } void LCD_GoToXY(char row, char col) { char pos;
if(row<LCDMaxLines) { pos= LineOne | (row << 6); // take the line number //row0>pos=0x80 row1->pos=0xc0 if(col<LCDMaxChars) pos= pos+col; //take the char
number //now pos points to XY pos LCD_CmdWrite(pos) } }
void
LCD_DisplayString(char *string_ptr) { while(*string_ptr) LCD_DataWrite(*string_ptr+
+); }
4 bit Mode
Schematic
There are following differences in 4 bit mode.
In code, we need to send the command to select 4 bit mode as shown in the
instruction set above.
12
The main program remains exactly as in 8 bit mode, we simply include the lcd_4_bit.c to work in 4 bit mode.
The important and required code is listed below,
Code
#include<reg51.h> #include "delay.h" #include "lcd.h" #define databus P0 //LCD
databus connected to PORT0 sbit rs= databus^0; // Register select pin connected to
P0.0 sbit rw= databus^1; // Read Write pin connected to P0.1 sbit en= databus^2; //
Enable pin connected to P0.2 /* 16x2 LCD Specification */ #define LCDMaxLines 2
#define LCDMaxChars 16 #define LineOne 0x80 #define LineTwo 0xc0 #define
BlankSpace ' ' void LCD_Init() { delay_us(5000); LCD_CmdWrite(0x02); //Initilize the LCD
13
Downloads
"The code is given for 4 and 8 bit modes with and without busy Flag"
8051_LCD_Tutorial_Code.zip
14