Objective

In this tutorial we are going to see how to interface a 2x16 LCD with LPC1768 in 4-bit mode. As per the name the 2x16 has 2 lines with 16 chars on each lines. It supports all the ascii chars and is basically used for displaying the alpha numeric characters. Here each character is displayed in a matrix of 5x7 pixels. Apart from alpha numeric chars it also provides the provision to display the custom characters by creating the pattern. Scope of this tutorial is to show how to display the alpha numeric chars on LCD, Generating and displaying the custom chars will be discussed in subsequent tutorials.


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.

fig LCD display
LCD Char 5x7 Matrix.jpg

PIN Diagram.PNG


All the pins are identically to the lcd internal controller discussed above

PIN NUMBER FUNCTION
1 Ground
2 VCC
3 Contrast adjustment (VO)
4 Register Select (RS). RS=0: Command, RS=1: Data
5 Read/Write (R/W). R/W=0: Write, R/W=1: Read
6 Clock (Enable). Falling edge triggered
7 Bit 0 (Not used in 4-bit operation)
8 Bit 1 (Not used in 4-bit operation)
9 Bit 2 (Not used in 4-bit operation)
10 Bit 3 (Not used in 4-bit operation)
11 Bit 4
12 Bit 5
13 Bit 6
14 Bit 7
15 Back-light Anode(+)
16 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 figure and table, an alpha numeric lcd has a 8-bit data bus referenced as D0-D7. As it is a 8-bit data bus, we can send the data/cmd to LCD in bytes. It also provides the provision to send the 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 finally displays on the LCD.

Read/Write(RW): This signal is used to write the data/cmd to LCD and reads the busy flag 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. A HIGH-to-LOW pulse has to be send on this enable pin which will latch the info into the LCD register and triggers the LCD to act accordingly.

Schematic

Port Connection

This section shows how to configure the GPIO for interfacing the LCD.
The below configuration 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

/* Configure the data bus and Control bus as per the hardware connection */
 
#define LcdDataBusPort       LPC_GPIO1->FIOPIN
#define LcdControlBusPort   LPC_GPIO2->FIOPIN
 
#define LcdDataBusDirnReg    LPC_GPIO1->FIODIR
#define LcdCtrlBusDirnReg      LPC_GPIO2->FIODIR
 
#define LCD_D4     24
#define LCD_D5     25
#define LCD_D6     26
#define LCD_D7     27
 
#define LCD_RS     0
#define LCD_RW     1
#define LCD_EN     2

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 lets see the timing diagram for sending the data and the command signals(RS,RW,EN) accordingly we write the algorithm and finally 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. figure: command write



Steps for Sending Command:

Below are the steps for sending the 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.
/* Function to send the command to LCD. As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */
void Lcd_CmdWrite(char cmd)
{
    sendNibble((cmd >> 0x04) & 0x0F);   //Send higher nibble
    LcdControlBusPort &= ~(1<<LCD_RS); // Send LOW pulse on RS pin for selecting Command register
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
    delay(1000);
    LcdControlBusPort &= ~(1<<LCD_EN);
 
    delay(10000);
 
    sendNibble(cmd & 0x0F);            //Send Lower nibble
    LcdControlBusPort &= ~(1<<LCD_RS); // Send LOW pulse on RS pin for selecting Command register
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
    delay(1000);
    LcdControlBusPort &= ~(1<<LCD_EN); 
 
    delay(10000);
}

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.

/* Function to send the data to LCD. As it is 4bit mode, a byte of data is sent in two 4-bit nibbles */
void Lcd_DataWrite(char dat)
{
    sendNibble((dat >> 0x04) & 0x0F);   //Send higher nibble
    LcdControlBusPort |= (1<<LCD_RS);  // Send HIGH pulse on RS pin for selecting data register
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
    delay(1000);
    LcdControlBusPort &= ~(1<<LCD_EN);
 
     delay(10000);
 
    sendNibble(dat & 0x0F);            //Send higher nibble
    LcdControlBusPort |= (1<<LCD_RS);  // Send HIGH pulse on RS pin for selecting data register
    LcdControlBusPort &= ~(1<<LCD_RW); // Send LOW pulse on RW pin for Write operation
    LcdControlBusPort |= (1<<LCD_EN);  // Generate a High-to-low pulse on EN pin
    delay(1000);
    LcdControlBusPort &= ~(1<<LCD_EN); 
 
    delay(10000);
}


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. The entire listing can be found here. }}



#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++);
}


Downloads

"The code is given for 4 and 8 bit modes with and without busy Flag"
Code


PREVIOUS TUTORIAL
NEXT TUTORIAL

{{#widget:Facebook_Like_Box|profile=https://www.facebook.com/ExploreEmbedded}}

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