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

16X2 LCD Basics:: LCD Pin Description: Pin No Symbol I/O Description

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

16X2 LCD Basics:

The Chinese LCD used here is JHD162A. It has a KS0066U/ HD44780U controller. It has a 16 pin interface device
which consists of 2 rows with 16 characters each. The operating voltage is 5V. Also, it has an LED backlight. There
are 2 modes of operation:

1) Instruction Mode: To initialize and configure the LCD before its use

2) Data Mode: Displays the characters for the respective codes that are given to the LCD via Data Pins.

LCD Pin Description:


Pin
Symbol I/O Description
No

1 VSS - Ground

2 VCC   +5V

3 VEE   Contrast Control

4 Input Command/Data Register


RS

5 R/W Input Read/Write Register

6 E Input/Output Enable

7 DB0 Input/Output Not Used in 4-Bit Mode

8 DB1 Input/Output Not Used in 4-Bit Mode

9 DB2 Input/Output Not Used in 4-Bit Mode

10 DB3 Input/Output Not Used in 4-Bit Mode

11 DB4 Input/Output Data Bus in 4-Bit Mode

12 DB5 Input/Output Data Bus in 4-Bit Mode

13 DB6 Input/Output Data Bus in 4-Bit Mode

14 DB7 Input/Output Data Bus in 4-Bit Mode

15 VCC - For LCD Back Light


16 VSS - For LCD Back Light

Initializing LCD Module:

Once you have verified all the connections from MCU to HFC4050 to LCD Module, we can go ahead to display text on
the LCD. But first, the LCD needs to be initialized properly. (According to the Datasheet) Before initializing the LCD,
you will need to wait for a minimum time of about 15 milliseconds after the input voltage supply is stable and greater
than 4.5 Volts.

The first step is to make sure that the RS and Enable are held LOW. Next is to input some commands to the LCD
using the Data pins. These commands will not be executed until a pulse is supplied to the Enable pin. After supplying
the command with a pulse, Enable has to be made High and then Low after a short delay. Then, the command is
executed.

The LCD can be configured in 4-bit mode by sending the appropriate command which is called “Function set” to it.
The Function set is a hexadecimal command of the LCD MPU unit, which selects working modes of the LCD. The
“Function Set” is mentioned in following table: 

Description: 

DL - Data Length (DL = 1 8bit, DL = 0 4bit)

N - No. of Lines (N = 1 2Lines, N = 0 1Lines)

F - Fonts (F = 1 5x10 dots, F = 0 5x7 dots)

According to the table, the value of Function Set for 4 –bit mode will be 0010 0000(0x20) because DL=0. The value
“Function Set” for the LCD configuration 2 line (N=1), 5X7 dots (F=0) and 4-bit (DL=0) mode will be 0010
1000(0x28).

When the power supply is given to the LCD, it remains in 8-bit mode. Now, if 0x20 is sent, lower nibble will not be
received by LCD because four data lines (D4-D7) are connected, so 0x02 is sent instead of 0x20. For more details
about the LCD interfacing refer these Links.

How to interface LCD in 4 bit mode with AVR microcontroller.


How to interface LCD with LPC2148.
Create a project using Keil uvision4 for LPC1768 Microcontroller:

In this section, we will start creating a project in Keil MDK we have already installed Keil µVision and Co-MDK Plug-in
+ CoLinkEx Drivers required for the CoLinkEx programming adapter. You can start by downloading the project files
and kick start your practical experiment.

Code.rar
Code Description:

These functions explained are included by making a LCD library and included in the coding files.

The below function is to send command bytes to the LCD:


Lcd_CmdWrite ()

The below function is to send Data bytes to the LCD:

Lcd_DataWrite ()

Initialization of 4-bit mode:

    Lcd_CmdWrite(0x02);                // Initialize LCD in 4-bit mode


    Lcd_CmdWrite(0x28);                // enable 5x7 mode for chars
    Lcd_CmdWrite(0x0E);                // Display OFF, Cursor ON
    Lcd_CmdWrite(0x01);                // Clear Display
    Lcd_CmdWrite(0x80);                // Move the cursor to beginning of the first line

16X2 LCD Basics :


This particular chinese LCD i.e JHD162A has KS0066U controller or similar to the famous
HD44780U. It Consists of 2 Rows with 16 Characters on each. It has a 16 pin Interface. Operates on
5V and has LED backlight. Works in 2 Modes :

1) Instruction Mode : Used for initializing and configuring LCD before we can use it & during
operation.
 2) Data Mode : Displays the respective characters for codes supplied to it via Data Pins.
Standard Pinout is as follows :
To keep things simple lets group the pins into 3 :
1) Power Pins : Pin 1,2,3,15,16
2) Control Pins : Pin 4,5,6
3) Data Pins : Pin 7 to 14
Now lets see some of the important pins before we actually start writing programs for LCD:

 Contrast Voltage (VEE) : Instead of using a trim pot just connect a 1K resistor to VEE in
series and ground it. That gives the best contrast of what I’ve seen.
 RS – short for Register select (Control Pin) : Used to switch been Instruction and Data
Mode. RS = High for Instruction Mode and RS = Low for Data mode.
 R/W – Read or Write (Control Pin): R/W = High for Read Mode and R/W = Low for Write.
Since we are going to use Write Mode only we will permanently ground it using a pull-down
resistor of 4.7K Ohms. Caution : If you are planning to use Read Mode with 3.3V MCUs
you’ll need a Bi-directional level shifter which can shift 5V to 3.3V and Vice-Versa.
 Enable (Control Pin) : This is similar to a trigger pin. Each Data/Instruction is executed by
the LCD module only when a pulse is applied to Enable pin. More specifically the it is
executed at the falling edge of the pulse.

Initializing LCD Module :


After you have cross-checked all your connections from MCU to HFC4050 to LCD Module its time
now to Display text on LCD. But before that we need to initialize the LCD properly. Also NOTE that :
as per the Datasheet – before initializing LCD we need to wait for a minimum time of about 15ms
after the input voltage supply is stable and >4.5Volts.
The 1st thing required for this is that RS must be held LOW and Enable also LOW in the beginning.
Now we must supply some commands using the Data Pins to LCD. But this command wont be
executed until we supply a pulse to Enable Pin. After supplying the command we make enable High
and then Low after a short delay(i.e a Pulse) and the command is executed.
Codes for various instructions can be obtained from below table : (Refer to page 12 of the
Datasheet)

For beginners we are only interested in following commands (which are also executed in given order
during initialization):

1. Function Set
2. Display Switch
3. Input Set
4. Screen Clear Command
5. DDRAM AD Set – Resposition Cursor Command

 In our case we are going to use 8Bit Mode with 2Rows and 5×10 Font style which gives
Function set as 0x3C.
 Next we issue Display On , Cursor On , Blink On which gives Display Switch as 0x0F.
 Next we select Increment Mode which gives Input Set as 0x06.
 Now we clear the screen and set cursor at Home which gives Screen Clear as 0x01.
 Also the command to reposition the cursor at Home at anytime is 0x80 and to reposition it at
the 1st column of the second row is 0xC0.

To cut-short or in simple words: We need to supply 5 commands in given order to the Data Pins
with a small amount of delay in between to initialize the LCD properly. These commands are : 0x3C ,
0x0F , 0x06 , 0x01 , 0x80(actually not required). Now since we have used the the 1st eight pins of
port 0 on lpc214x we can directly assign these values to the IO0PIN register. Note that Bit 0 i.e LSB
in IO0PIN is towards extreme right and the bit number increases as we move towards the left
approaching the MSB. Also since we have connected P0.0 to Data Bit 0 pin , P0.1 to Data bit 1 and
so on … we have a one to one consecutive mapping between them MCU pins and LCD Data pins.
NOTE: During Initialization , issuing 0x80 command to reposition the cursor at home will be of no
use since it would already be at Home after issuing Screen Clear Command. Also we need to
Supply a Pulse to Enable after issuing each command and wait for a short while for the LCD
controller to process it. Datasheet says a delay of 40us is required at minimum. But even a delay of
1ms wont hurt us to be on the safer side.

Correct Sequence for Initializing LCD module (8 Bit Interface) is as given :

1. After LCD Module is powered on and MCU boots wait for 20ms (Specifically >15ms)
2. Now make RS and Enable LOW making sure that R/W is permanently grounded.
3. Issue Function set command – 0x3C and Pulse Enable (wait for >40us after pulsing).
4. Issue Display Switch – 0x0F and Pulse Enable (wait for >40us after pulsing).
5. Issue Input Set – 0x06 and Pulse Enable (wait for >40us after pulsing).
6. Issue Screen Clear – 0x01 and Pulse Enable (wait for >1.64ms after pulsing).
7. Issue DDRAM AD Set – 0x80 and Pulse Enable (wait for >40us after pulsing).
Now LCD is ready for Printing!
After Initializing the LCD you would see a Cursor Blinking at in 1st column at row 1 :

Printing Characters on LCD Module :


Now that we have initialized LCD correctly its time to send some characters. For this we enter into
Data Mode by making RS High. After that any number / code applied to Data pins will be converted
into corresponding Character and displayed. The codes for the characters are ASCII Codes given by
the font table as show below :
Hence to print a character on LCD we just need to apply the 8bit ASCII code to Data pins. This can
be done easily by type casting the character into an integer (which is done automatically by the
compiler – but we are doing it here explicitly). Refer to Page 14 of datasheet for font table.
Now , after printing 16 characters you might wanna print more characters in 2nd Row. This is done
by entering into Instruction Mode by holding RS=LOW and giving command 0xC0 which repositions
the cursor at 2nd Row & 1st column. Needless to say that we need to again give a Pulse on Enable
pin to process it. After that we put RS to high and get back in Data mode.
Note on Repositioning the Cursor : As we have seen , the command for repositioning the cursor
at home is 0x80. Think of 0x80 as the base address of Display Data Ram(DDRAM). Hence 0x80 will
set cursor at 1st Row, 1st Column. If you add 0x01 to 0x80 the cursor will go to 2nd column in 1st
row. Likewise if you add 0x0F to 0x80 you’ll end up at 1st row , last column. Similar to this the base
address for 2nd Row is 0x40 and not 0x10! 0x10 is the base address of 3rd row and 0x50 is base
address for 4th row in the case of 16×4 LCD Modules. So to position the cursor at 2nd row, 1st
column we must add 0x40 to 0x80 which gives 0x80+0x40=0xC0. Hence after giving command
‘0xC0’ cursor comes down to 2nd row & 1st col.
More on DDRAM and CGROM @ http://www.8051projects.net/lcd-interfacing/basics.php

Programs for Lpc2148 to print Characters on LCD


module :
1st Let me explain a few functions that I’ve made in the programs below:

 initLCD() : Initializes the LCD.


 enable() : To generate a pulse on Enable pin.
 LCD_Cmd(int) : To issue LCD commands.
 LCD_WriteChar(char) : Print a Single Character.
 LCD_WriteString(string) : Print a String.
 delay() :Generate required delay.(For program1 Only!)
 delayMS(int) : Generate specificed delay.(For program2 Only!)
Program #1 : Basic Program without precise Timing & no PLL setup.

/*
(C) OCFreaks! | Umang Gajera 2012-13.
More Embedded tutorials @ www.ocfreaks.com/cat/embedded
>>Program for Interfacing 16X2 LCD Module with LPC2148
*/

#include <lpc214x.h>

/*
Connections from LPC2148 to LCD Module:
P0.0 to P0.7 used as Data bits.
P1.16 connected to pin4 i.e. RS - Command / Data
P1.17 connected to pin6 i.e. E - Enable
Pin5 of LCD Module i.e. 'R/W' connected to ground
*/

void initLCD(void);
void enable(void);
void LCD_WriteChar(char c);
void LCD_WriteString(char * string);
void LCD_Cmd(unsigned int cmd);
void delay(void);

int main(void)
{
initLCD(); //LCD Now intialized and ready to Print!
LCD_WriteString(".: Welcome to :.");
LCD_Cmd(0x80 + 0x40); //Come to 2nd Row
LCD_WriteString("www.OCFreaks.com");
while(1); // Loop forever
return 0; //This won't execute :P
}

void initLCD(void)
{
IO0DIR = 0xFF; //P0.0 to P0.7 configured as Output - Using 8 Bit mode
IO1DIR |= (1<<16) | (1<<17); //P1.16 and P1.17 configured as Output -
Control Pins
IO0PIN = 0x0; //Reset Port0 to 0.
IO1PIN = 0x0; //Reset Port1 to 0 - Which also makes RS and Enable LOW.

//LCD Initialization Sequence Now starts


delay(); //Initial Delay
LCD_Cmd(0x3C); //Function Set Command : 8 Bit Mode , 2 Rows , 5x10 Font
Style
LCD_Cmd(0x0F); //Display Switch Command : Display on , Cursor on , Blink
on
LCD_Cmd(0x06); //Input Set : Increment Mode
LCD_Cmd(0x01); //Screen Clear Command , Cursor at Home
LCD_Cmd(0x80); //Not required the 1st time but needed to reposition the
cursor at home after Clearing Screen
//Done!
}

void enable(void)
{
delay();
IO1PIN |= (1<<17);//Enable=High
delay();
IO1PIN &= ~(1<<17);//Enable=Low
delay();
}

void LCD_WriteChar(char c)
{
IO1PIN |= (1<<16); //Switch to Data Mode
IO0PIN = (int) c; //Supply Character Code
enable(); //Pulse Enable to process it
}

void LCD_WriteString(char * string)


{
int c=0;
while (string[c]!='\0')
{
LCD_WriteChar(string[c]);
c++;
}
}
void LCD_Cmd(unsigned int cmd)
{
IO1PIN = 0x0; //Enter Instruction Mode
IO0PIN = cmd; //Supply Instruction/Command Code
enable(); //Pulse Enable to process it
}

void delay(void)
{
int i=0,x=0;
for(i=0; i<19999; i++){ x++; }
}

Program #2 : Program with PLL* setup and precise Timing**. (Xtal=12Mhz ,


CCLK=60Mhz)
*PLL tutorial for LPC2148 @ http://www.ocfreaks.com/lpc214x-pll-tutorial-for-cpu-and-peripheral-
clock/
**Tutorial on Timer for LPC2148 @ http://www.ocfreaks.com/lpc2148-timer-tutorial/

/*
(C) OCFreaks! | Umang Gajera 2012-13.
More Embedded tutorials @ www.ocfreaks.com/cat/embedded
>>Program2 for Interfacing 16X2 LCD Module with LPC2148
>>using Timer0 and CPU @ 60Mhz
*/

#include <lpc214x.h>

/*
XTAL Freq=12 Mhz , CCLK=60Mhz , PCLK=60Mhz
Using common delay of 2ms for all operations

Connections from LPC2148 to LCD Module:


P0.0 to P0.7 used as Data bits.
P1.16 connected to pin4 i.e. RS - Command / Data
P1.17 connected to pin6 i.e. E - Enable
Pin5 of LCD Module i.e. 'R/W' connected to ground
*/

#define PLOCK 0x00000400

void initLCD(void);
void enable(void);
void LCD_WriteChar(char c);
void LCD_WriteString(char * string);
void LCD_Cmd(unsigned int cmd);
void delayMS(unsigned int milliseconds);
void setupPLL0(void);
void feedSeq(void);
void connectPLL0(void);

int main(void)
{
setupPLL0();
feedSeq(); //sequence for locking PLL to desired freq.
connectPLL0();
feedSeq(); //sequence for connecting the PLL as system clock

//SysClock is now ticking @ 60Mhz!

VPBDIV = 0x01; // PCLK is same as CCLK i.e 60Mhz

//PLL0 Now configured!

initLCD(); //LCD Now intialized and ready to Print!


LCD_WriteString(".: Welcome to :.");
LCD_Cmd(0x80 + 0x40); //Come to 2nd Row
LCD_WriteString("www.OCFreaks.com");
while(1); // Loop forever
return 0; //This won't execute :P
}

void initLCD(void)
{
IO0DIR = 0xFF; //P0.0 to P0.7 configured as Output - Using 8 Bit mode
IO1DIR |= (1<<16) | (1<<17); //P1.16 and P1.17 configured as Output - Control
Pins
IO0PIN = 0x0; //Reset Port0 to 0.
IO1PIN = 0x0; //Reset Port1 to 0 - Which also makes RS and Enable LOW.

//LCD Initialization Sequence Now starts


delayMS(20); //Initial Delay
LCD_Cmd(0x3C); //Function Set Command : 8 Bit Mode , 2 Rows , 5x10 Font Style
LCD_Cmd(0x0F); //Display Switch Command : Display on , Cursor on , Blink on
LCD_Cmd(0x06); //Input Set : Increment Mode
LCD_Cmd(0x01); //Screen Clear Command , Cursor at Home
LCD_Cmd(0x80); //Not required the 1st time but needed to reposition the
cursor at home after Clearing Screen
//Done!
}

void enable(void)
{
//Using common delay of 2ms
delayMS(2);
IO1PIN |= (1<<17);//Enable=High
delayMS(2);
IO1PIN &= ~(1<<17);//Enable=Low
delayMS(2);
}

void LCD_WriteChar(char c)
{
IO1PIN |= (1<<16); //Switch to Data Mode
IO0PIN = (int) c; //Supply Character Code
enable(); //Pulse Enable to process it
}

void LCD_WriteString(char * string)


{
int c=0;
while (string[c]!='\0')
{
LCD_WriteChar(string[c]);
c++;
}
}

void LCD_Cmd(unsigned int cmd)


{
IO1PIN = 0x0; //Enter Instruction Mode
IO0PIN = cmd; //Supply Instruction/Command Code
enable(); //Pulse Enable to process it
}

void delayMS(unsigned int milliseconds)


{
//Timers will be explained in detail in the very next tutorial @
www.ocfreaks.com/cat/embedded/

T0IR = 0;
T0CTCR = 0;
T0PR = 60000; //60000 clock cycles @60Mhz = 1 mS
T0PC = 0;
T0TC = 0;
T0TCR = 0x01; //enable timer

//wait until timer counter reaches the desired dela1y


while(T0TC < milliseconds);

T0TCR = 0x00; //disable timer


}

//---------PLL Related Functions :---------------

void setupPLL0(void)
{
PLL0CON = 0x01; // PPLE=1 & PPLC=0 so it will be enabled
// but not connected after FEED sequence
PLL0CFG = 0x24; // set the multipler to 5 (i.e actually 4)
// i.e 12x5 = 60 Mhz (M - 1 = 4)!!!
// Set P=2 since we want FCCO in range!!!
// So , Assign PSEL =01 in PLL0CFG as per the table.
}

void feedSeq(void)
{
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
}

void connectPLL0(void)
{
// check whether PLL has locked on to the desired freq by reading the lock
bit
// in the PPL0STAT register

while( !( PLL0STAT & PLOCK ));

// now enable(again) and connect


PLL0CON = 0x03;
}

Output after flashing any of the above code to MCU (Successfully Tested on Lpc2148 and
Lpc1768) :
Download Links to Source Code using KEIL UV4 Project IDE :
1) Program #1 : OCFreaks.com_LPC2148_LCD_Interfacing.rar
2) Program #2 : OCFreaks.com_LPC2148_LCD_Interfacing_precise.rar

Silence please : 2 LCDs were Harmed in the


making
While working on this article 2 of my Green LCDs were damaged. One got completely useless and
other got partially damaged. In any case both of them are useless now.
LCD #1 : Display completely gone kaput..
LCD #2 : Display partially gone kaput..
ntroduction
In this tutorial, we will be learning how to interface a 16 x 2 character LCD
display (HD44780U )with the LPC 1768 micro-controller. The 16 x 2 character
LCD display is one of the most basic display module used by beginners while
learning embedded systems.
16 characters can be displayed on the screen at a time across two lines. Each
of these 16 characters are displayed using pixels that are arranged in a 5 x 7
matrix format.

LCD interface on ARM Base board


 
Features
 The LCD module has 16 pins.
 It can be configured either in 4-bit or 8-bit mode. The 4-bit mode helps in saving an extra 4-
pins in the circuitry.
 Controller and driver are automatically reset after power on.
 Instruction functions that include: Display clear, cursor home, display on/off, cursor on/off,
display character blink, cursor shift, display shift
Connection diagram for data port and control port of LCD
 
Pin Functions
This is a list of pin functions interfaced with the micro-controller.

Registers
The two 8-bit registers in the HD44780U 16 x 2 character LCD display are
 Instruction Register (IR)
Instruction functions are written from the micro-controller to this register for Display Data RAM
(DDRAM) and Character Generator RAM (CGRAM).
 Data Register (IR)
This register temporarily holds the data to be written into the DDRAM or the CGRAM from the
micro-controller. It also temporarily holds the data that is being read from the two registers.
Programming
Interfacing the LCD module with any micro-controller is very easy. It involves
only a few functions that instruct the LCD to perform specific tasks and read or
write data in the LCD.
Here, we will be doing an 8-bit interfacing with the LPC1768 micro-controller
and only perform write operation on the LCD.
DEFINITIONS:
Default

1 //***********************************************************************************************
******
2
// Default DATA PORT in OpenLab is PORTB. P0[16:23]
3
// Default CONTROL PORT in OpenLab is PORTC  P1[16:23]
4
//***********************************************************************************************
5
******

6
#define DATA_BUS                              LPC_GPIO0-&gt;FIODIR

7
#define CONTROL_BUS                           LPC_GPIO1-&gt;FIODIR

8
//***********************************************************************************************

9 ******

1  
0
//***********************************************************************************************
1 ******
1
// Control Port definitions
1
//***********************************************************************************************
2
******
1
#define CONTROL_SET                           LPC_GPIO1-&gt;FIOSET
3

#define CONTROL_CLEAR                         LPC_GPIO1-&gt;FIOCLR


1
4 #define RS              18    

1 #define RW              17    
5
#define EN           16    
1
//***********************************************************************************************
6
******
1
 
7

//***********************************************************************************************
1
******
8

// Data Port definitions


1
9 //***********************************************************************************************

******
2
0 #define DATA_SET                             LPC_GPIO0-&gt;FIOSET

2 #define DATA_CLEAR                           LPC_GPIO0-&gt;FIOCLR


1
#define DATA0      16    
2
#define DATA1      17    
2

#define DATA2      18    
2
3 #define DATA3      19    

2
4 #define DATA4      20    

2 #define DATA5      21    
5
#define DATA6      22    
2
#define DATA7      23    
6

//***********************************************************************************************
2
******
7

 
2
8 //***********************************************************************************************

******
2
9 //Control and Data Mask

3 //***********************************************************************************************
0 ******

3 #define CONTROL_MASK     ((1&lt;&lt;RS)|(1&lt;&lt;RW)|(1&lt;&lt;EN))


1
#define DATA_MASK       ((1&lt;&lt;DATA7)|(1&lt;&lt;DATA6)|(1&lt;&lt;DATA5)|(1&lt;&lt;DATA4)|
3 (1&lt;&lt;DATA3)|(1&lt;&lt;DATA2)|(1&lt;&lt;DATA1)|(1&lt;&lt;DATA0))
2
//***********************************************************************************************
3 ******
3

3
4

3
5

3
6

3
7
3
8

3
9

 
PORT INITIALIZATION: The directions of the three instruction register pins and
the 8 data register pins must be set as output.
We have defined mask bits to achieve this.
Default

1 void INIT_PORTS()

2{

3     DATA_BUS = DATA_MASK;        

4     CONTROL_BUS    =    CONTROL_MASK;        

5     INIT_LCD();

6}

COMMAND FUNCTION: The LCD has to be configured using certain commands


in the instruction register. These commands or any data for that matter, has to
be written to the registers of the LCD by signalling a high to low transition on
the enable pin. The delay between the transition varies based on each
commands or data written. For a general function, a 1 millisecond delay would
be good enough. Every command should be sent with the RS and RW pins
cleared.
Default
1 void SEND_CMD(unsigned char cmd)

2{

3     SEND_BITWISE(cmd);                                                                              

4     CONTROL_CLEAR = (1&lt;&lt;RS)|(1&lt;&lt;RW);

5     CONTROL_SET = (1&lt;&lt;EN);                                          

6     delay_ms(1);

7     CONTROL_CLEAR = (1&lt;&lt;EN);

8}

COMMANDS: A basic set of commands that must be written to the LCD for
initialization are as follows. This will set the LCD in 8 bit mode. It can access
both the lines and display matrix is 5*7.
Default

1 void INIT_LCD()

2{

3     SEND_CMD(0x38);              //Initialize LCD in 8-bit mode. 2 Lines. 5*7 mode

4     SEND_CMD(0x0C);              //Display ON cursor OFF

5     SEND_CMD(0x01);              // Clear display

6     SEND_CMD(0x06);              //Shift cursor to the right


7     SEND_CMD(0x80);              //Force the cursor to the beginning of the line

8}

WRITE FUNCTIONS: Similar to writing the commands in the instruction


register, the functions for data write is pretty much the same with the exception
that the RS pin must be kept high before the high to low transition. The
programmer can develop functions that can send characters as well as strings.
A function to write the characters is shown below
Default

1 void SEND_CHAR_DATA(unsigned char data)

2{

3     SEND_BITWISE(data);    

4     CONTROL_CLEAR =  (1&lt;&lt;RS)|(1&lt;&lt;RW);

5     CONTROL_SET = (1&lt;&lt;RS)|(1&lt;&lt;EN);

6     delay_ms(1);

7     CONTROL_CLEAR = (1&lt;&lt;EN);

8     CONTROL_CLEAR = (1&lt;&lt;RS);    

9}

The character LCD can be used to display sensor values, count values or
display any kind of texts.
Another write function is created in order to send a string of data to the lcd
controller.
Default
1 void SEND_STRING_DATA(char *str)

2{

3     while(*str!='\0')

4     {

5         SEND_CHAR_DATA(*str);

6         str++;

7     }

8}

Summary
The above tutorial will guide you to interface an 16*2 Character LCD using a 32
bit LPC1768 ARM cortex M3 microcontroller. The sample code based on this
tutorial is available in the Code Library  under the section ARM.

   
LCD Commands
HD44780 instruction set

Execution
Code
Description time**

Instruction RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0

Clears display and returns


Clear display 0 0 0 0 0 0 0 0 0 1 cursor to the home position 1.64mS
(address 0).

Returns cursor to home


position (address 0). Also
returns display being shifted
Cursor home 0 0 0 0 0 0 0 0 1 * 1.64mS
to the original position.
DDRAM contents remains
unchanged.

Sets cursor move direction


(I/D), specifies to shift the
Entry mode set 0 0 0 0 0 0 0 1 I/D S display (S). These 40uS
operations are performed
during data read/write.

Sets On/Off of all display


Display On/Off (D), cursor On/Off (C) and
0 0 0 0 0 0 1 D C B 40uS
control blink of cursor position
character (B).

Sets cursor-move or display-


Cursor/display shift (S/C), shift direction
0 0 0 0 0 1 S/C R/L * * 40uS
shift (R/L). DDRAM contents
remains unchanged.

Sets interface data length


Function set 0 0 0 0 1 DL N F * * (DL), number of display line 40uS
(N) and character font(F).

Sets the CGRAM address.


Set CGRAM
0 0 0 1 CGRAM address CGRAM data is sent or 40uS
address
received after this setting.

Set DDRAM 0 0 1 DDRAM address Sets the DDRAM address. 40uS


address DDRAM data is sent or
received after this setting.

Reads Busy-flag (BF)


Read busy-flag indicating internal operation
and address 0 1 BF DDRAM address is being performed and 0uS
counter reads address counter
contents.

Write to CGRAM Writes data to CGRAM or


1 0 write data 40uS
or DDRAM DDRAM.

Read from
Reads data from CGRAM or
CGRAM or 1 1 read data 40uS
DDRAM.
DDRAM

Notes:

 DDRAM = Display Data RAM.


 CGRAM = Character Generator RAM.
 DDRAM address corresponds to cursor position.
 Address Counter is used for both DDRAM and CGRAM.
 * = Don't care.
 ** = Based on Fosc = 250KHz.

Bit names

Bit Settings

I/D 0 = Decrement cursor position 1 = Increment cursor position

S 0 = No display shift 1 = Display shift

D 0 = Display off 1 = Display on

C 0 = Cursor off 1 = Cursor on

B 0 = Cursor blink off 1 = Cursor blink on

S/C 0 = Move cursor 1 = Shift display

R/L 0 = Shift left 1 = Shift right

DL 0 = 4-bit interface 1 = 8-bit interface

N 0 = 1/8 or 1/11 Duty (1 line) 1 = 1/16 Duty (2 lines)


F 0 = 5x7 dots 1 = 5x10 dots

BF 0 = Can accept instruction 1 = Internal operation in progress

Clear Display
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 0 0 0 0 0 0 0 1
Clears all display and returns the cursor to the home position (Address 0).

Details 
Writes space code (20h) into all DD RAM addresses. Sets address counter to DD
RAM location 0. Returns display to its original state if it was shifted. In other words,
the display disappears and the cursor or blink goes to the left edge of the display (the
first line if 2 lines are displayed). Sets entry mode I/D to 1 (Increment Mode). Entry
mode shift (S) bit remains unchanged.

Execution Time = 82µs-1.64ms / 120µs-4.9ms

Cursor Home
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 0 0 0 1 *
Returns the cursor to the home position (Address 0). Returns display to its original
state if it was shifted.

Details 
Sets the address counter to DD RAM location 0 in the address counter. Returns
display to its original state if it was shifted. DD RAM contents remain unchanged.
The cursor or blink goes to the left edge of the display (the first line if 2 lines are
displayed).

Execution Time = 40µs-1.6ms / 120µs-4.8ms

Entry Mode Set


RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 0 0 0 0 0 1 I/D S
Sets the effect of subsequent DD RAM read or write operations. Sets the cursor move
direction and specifies or not to shift the display. These operations are performed
during data read and write.

Details 
Specifies whether to increment (I/D = 1) or decrement (I/D = 0) the address counter
after subsequent DD RAM read or write operations.

If S = 1 the display will be shifted to the left (if I/D = 1) or right (if I/D = 0) on
subsequent DD RAM write operations. This makes it looks as if the cursor stands still
and the display moves when each character is written to the DD RAM. if S = 0 the
display will not shift on subsequent DD RAM write operations.

Execution Time = 40µs / 120µs

Display ON/OFF
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 0 0 0 0 1 D C B

Controls display of characters and cursor.

Details 
D: The display is ON when D = 1 and OFF when D = 0. The DD RAM contents
remain unchanged.

C: The cursor is displayed when C = 1 and is not displayed when C = 0. 


The cursor is displayed as 5 dots in the 8th line when the 5 x 7 dot character font is
selected and as 5 dots in the 11th line when the 5 x 10 dot character font is selected.

B: The character at the the cursor position blinks when B = 1. 


Blinking is performed by switching between all blank dots and the display character
every 409.6 ms.

Execution Time = 40µs / 120µs

Cursor and Display Shift


RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 0 0 0 1 S/C R/L * *
Moves the cursor and shifts the display without changing DD RAM contents
Details 
Shifts cursor position or display to the right or left without writing or reading display
data. This function is used to correct or search for the display. In a 2-line display, the
cursor moves to the 2nd line when it passes the 40th digit of the 1st line. Notice that
the 1st and 2nd line displays will shift at the same time. When the displayed data is
shifted repeatedly each line only moves horizontally. The 2nd line of the display does
not shift into the 1st line position.
S/C R/L
=== ===
0 0 Shifts the cursor position to the left
(Address Counter is decremented by 1)
0 1 Shifts the cursor position to the right
(Address Counter is incremented by 1)
1 0 Shifts the entire display to the left
The cursor follows the display shift
1 1 Shifts the entire display to the right
The cursor follows the display shift
Execution Time = 40µs / 120µs

Function Set
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 0 0 1 DL N F * *
Sets interface data length (DL), number of display lines (N) and character font (F)

Details 
This command should be issued only after automatic power-on initialization has
occurred, or as part of the module initialization sequence. 
DL: Sets interface data length

Data sent or received in 8 bit lengths (DB7-DB0) when DL = 1

Data sent or received in 4 bit lengths (DB7-DB4) when DL = 0

When the 4 bit length is selected, data must be sent or received in pairs of 4-
bits each. The most-significant 4 bits are sent or received first.

N: Sets number of display lines

F: Sets character font


display Character Duty
N F lines Font Factor Remarks
=== ======= ========= ====== =======
0 0 1 5x 7 dots 1/8 -
0 1 1 5x10 dots 1/11 -
1 * 2 5x 7 dots 1/16 Cannot display 2 lines with
5x10 dot character font

Note that a 1 line x 16 character display is treated as a 2 line x 8 character display.


The first `line' is the left eight character positions on the display and the second `line'
is the right eight character positions. This also means that the 16 characters on the
display do not occupy 16 sequential DD RAM locations.

Execution Time = 40µs / 120µs

Set CG RAM Address


RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 0 1 A A A A A A

Sets the CG RAM address. Subsequent read or write operations refer to the CG RAM.

Details 
Sets the specified value (AAAAAA) into the address counter. Subsequent read or
write operations transfer data from, or to, the character generator RAM.

Execution Time = 40µs / 120µs

Set DDRAM Address


RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 1 A A A A A A A

Sets the DD RAM address. Subsequent read or writes refer to the DD RAM.

Details 
Sets the specified value (AAAAAAA) into the address counter. Subsequent read or
write operations transfer data from, or to, the display RAM. Note: Adjacent display
RAM locations do not necessarily refer to adjacent display positions.

Execution Time = 40µs / 120µs


Read busy flag and address counter
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 1 BF A A A A A A A

Reads the state of the busy flag (BF) and the contents of the address counter.

Details 
Reads the busy flag (BF) that indicates the state of the LCD module. BF = 1 indicates
that the module is busy processing the previous command. BF = 0 indicates that the
module is ready to perform another command.

The value of the address counter is also returned. The same address counter is used for
both CG and DD RAM transfers.

This command can be issued at any time. It is the only command which the LCD
module will accept while a previous command is still being processed.

Execution Time = 1µs

Write data to CG or DD RAM


RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
1 0 D D D D D D D D
Writes data into DD RAM or CG RAM

Details 
Writes a byte (DDDDDDDD) to the CG or the DD RAM. The destination (CG RAM
or DD RAM) is determined by the most recent `Set RAM Address' command. The
location to which the byte will be written is the current value of the address counter.
After the byte is written the address counter is automatically incremented or
decremented according to the entry mode. The entry mode also determines whether or
not the display will shift.

Execution Time = 40µs / 120µs

Read data from CG or DD RAM


RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
1 1 D D D D D D D D
Reads data from DD RAM or CG RAM.
Details 
Reads a byte (DDDDDDDD) from the CG or DD RAM. The source (CG RAM or DD
RAM) is determined by the most recent `Set RAM Address' command. The location
from which the byte will be read is the current value of the address counter. After the
byte is reda the address counter is automatically incremented or decremented
according to the entry mode.

Execution Time = 40µs / 120µs

You might also like