16x2 LCD Interfacing Using GPIO in Embedded C | LPC1768

16x2 LCD Interfacing Using GPIO in Embedded C LPC1768

A 16×2 LCD is one of the most commonly used display modules in embedded systems. It is simple, low-cost, and useful for displaying sensor values, messages, menu options, time, temperature, and many other types of information. LPC1768, based on the ARM Cortex-M3 processor, provides multiple GPIO pins that can be used to interface external devices. In this tutorial, we will learn how to interface a 16×2 character LCD with the LPC1768 using GPIO and Embedded C programming. We will use the LCD in 8-bit mode, which means eight GPIO pins are used for the LCD data lines. We will also understand the purpose of the LCD control pins and important GPIO registers used in the program.

This tutorial explains how to interface a 16×2 LCD with the LPC1768 microcontroller using GPIO in 8-bit mode. Learn LCD pin connections, RS/RW/EN control, GPIO registers, LCD commands, and   initialization. The tutorial also includes a practical Embedded C program for displaying text on the LCD.

What is a 16×2 LCD?

A 16×2 LCD can display:

  • 16 characters in each row
  • 2 rows

Therefore, it can display a total of 32 characters at a time.

Most 16×2 LCD modules are compatible with the HD44780 LCD controller. The LCD contains data pins and control pins that allow a microcontroller to send commands and display characters.

LCD Pins

A typical 16×2 LCD has the following pins:

PinNameFunction
1VSSGround
2VDD+5V Supply
3VEEContrast control
4RSRegister Select
5RWRead/Write
6ENEnable
7–14D0–D7Data lines
15ALED backlight anode
16KLED backlight cathode

In this tutorial, the LCD is used in write mode, so the RW pin is connected to GND.

registor_now_P

LPC1768 to LCD Pin Connection

We will use the following GPIO connections:

 

 

LPC1768 to LCD Pin Connection

 

 

LCD PinFunctionLPC1768 Pin
RSRegister SelectP0.10
RWWrite ModeGND
ENEnableP0.11
D0Data Bit 0P0.15
D1Data Bit 1P0.16
D2Data Bit 2P0.17
D3Data Bit 3P0.18
D4Data Bit 4P0.19
D5Data Bit 5P0.20
D6Data Bit 6P0.21
D7Data Bit 7P0.22

The data pins are connected to consecutive GPIO pins, making the program easier to understand.

 

 

LPC1768 to LCD Pin Connection

 

 

Important LCD Control Pins

RS – Register Select

The RS pin decides whether the LCD receives a command or character data.

RS = 0 → Command Register

RS = 1 → Data Register

For example, when we want to clear the LCD or move the cursor, we send a command.

When we want to display a character such as A, H, or 1, we send data.

RW – Read/Write

The RW pin determines whether the LCD is reading or writing.

RW = 0 → Write

RW = 1 → Read

Since this tutorial only sends data to the LCD, RW is permanently connected to GND.

EN – Enable

The Enable pin is used to latch the command or data.

The microcontroller places the required data on the LCD pins and then generates an Enable pulse:

EN = 1

Short delay

EN = 0

The LCD reads the data when the Enable pulse is applied.

LPC1768 GPIO Registers Used

The LPC1768 provides Fast GPIO registers for controlling GPIO pins.

FIODIR – Direction Register

The FIODIR register configures a pin as input or output.

0 → Input

1 → Output

For LCD interfacing, all RS, EN, and data pins are configured as outputs.

Example:

LPC_GPIO0->FIODIR |= (1 << 10);

This configures P0.10 as an output.

Explore Courses - Learn More

FIOSET – Set GPIO Pin

The FIOSET register makes a GPIO output pin HIGH.

LPC_GPIO0->FIOSET = (1 << 10);

This makes P0.10 logic HIGH.

For the LCD, this is useful when setting:

  • RS = 1
  • EN = 1

FIOCLR – Clear GPIO Pin

The FIOCLR register makes a GPIO output pin LOW.

LPC_GPIO0->FIOCLR = (1 << 10);

This clears P0.10.

For the LCD, this can be used when setting:

  • RS = 0
  • EN = 0

FIOPIN – Read or Write Pin Value

FIOPIN represents the current value of the GPIO pins.

It can be used to read inputs or write output values. However, when working with individual pins, FIOSET and FIOCLR are generally easier to understand.

LCD Initialization Commands

Before displaying data, the LCD must be initialized.

Some commonly used commands are:

CommandFunction
0x388-bit mode, 2 lines, 5×7 font
0x0CDisplay ON, Cursor OFF
0x01Clear LCD
0x06Cursor increments automatically
0x80Move cursor to first line
0xC0Move cursor to second line

These commands configure the LCD before normal data is displayed.

LPC1768 LCD Interfacing Program

The following program displays messages on a 16×2 LCD.

#include 

#define RS       (1 << 10)
#define EN       (1 << 11)
#define DATA_MASK (0xFF << 15) void delay(unsigned int count); void lcd_cmd(unsigned char cmd); void lcd_data(unsigned char data); void lcd_string(char *str); void lcd_init(void); int main(void) { /* Configure P0.10 and P0.11 as GPIO */ LPC_PINCON->PINSEL0 &= ~((3 << 20) | (3 << 22)); /* Configure P0.15 to P0.22 as GPIO */ LPC_PINCON->PINSEL0 &= ~(3 << 30); LPC_PINCON->PINSEL1 &= ~((0x3FFF) << 0); /* Configure LCD pins as output */ LPC_GPIO0->FIODIR |= RS | EN | DATA_MASK;

    lcd_init();

    lcd_string("Embedded Empire");

    lcd_cmd(0xC0);

    lcd_string("LPC1768 LCD");

    while(1);
}

void lcd_cmd(unsigned char cmd)
{
    /* RS = 0 for command */
    LPC_GPIO0->FIOCLR = RS;

    /* Clear old data */
    LPC_GPIO0->FIOCLR = DATA_MASK;

    /* Send command */
    LPC_GPIO0->FIOSET = ((unsigned int)cmd << 15); /* Enable pulse */ LPC_GPIO0->FIOSET = EN;

    delay(100);

    LPC_GPIO0->FIOCLR = EN;

    delay(3000);
}

void lcd_data(unsigned char data)
{
    /* RS = 1 for data */
    LPC_GPIO0->FIOSET = RS;

    /* Clear old data */
    LPC_GPIO0->FIOCLR = DATA_MASK;

    /* Send character data */
    LPC_GPIO0->FIOSET = ((unsigned int)data << 15); /* Enable pulse */ LPC_GPIO0->FIOSET = EN;

    delay(100);

    LPC_GPIO0->FIOCLR = EN;

    delay(3000);
}

void lcd_string(char *str)
{
    while(*str)
    {
        lcd_data(*str++);
    }
}

void lcd_init(void)
{
    delay(50000);

    lcd_cmd(0x38);
    lcd_cmd(0x0C);
    lcd_cmd(0x01);
    lcd_cmd(0x06);
}

void delay(unsigned int count)
{
    volatile unsigned int i;

    for(i = 0; i < count; i++);
}

Program Explanation

Configuring the GPIO Pins

The RS and EN pins are configured as GPIO:

LPC_PINCON->PINSEL0 &= ~((3 << 20) | (3 << 22));

This selects the GPIO function for P0.10 and P0.11.

The data pins P0.15 to P0.22 are also configured for GPIO. After selecting the GPIO function, all the LCD pins are configured as output:

LPC_GPIO0->FIODIR |= RS | EN | DATA_MASK;

The LCD requires the microcontroller to send signals to it, so these pins must operate as output pins.

Sending a Command

The function lcd_cmd() is used to send LCD commands.

First:

LPC_GPIO0->FIOCLR = RS;

sets RS to 0, selecting the command register.

Then the command is placed on the eight data lines:

LPC_GPIO0->FIOSET = ((unsigned int)cmd << 15);

The value is shifted by 15 because the LCD data pins start from P0.15.

Finally, the Enable pin is pulsed HIGH and LOW so that the LCD accepts the command.

Sending Display Data

The function lcd_data() sends characters to the LCD.

LPC_GPIO0->FIOSET = RS;

sets RS to 1, selecting the data register.

For example, if the program sends:

lcd_data('A');

the ASCII value of the character A is placed on the data pins, and an Enable pulse transfers it to the LCD.

Displaying a String

The lcd_string() function sends one character at a time:

while(*str)
{
    lcd_data(*str++);
}

If the string is:

"Embedded Empire"

each character is transmitted individually until the null character \0 is reached.

How LCD Interfacing Works

The overall process can be understood as:

Initialize GPIO Pins
        ↓
Configure LCD Pins as Output
        ↓
Initialize LCD
        ↓
RS = 0 → Send Command
        ↓
RS = 1 → Send Character Data
        ↓
Generate Enable Pulse
        ↓
LCD Displays the Character

Talk to Academic Advisor

Frequently Asked Questions

A 16×2 LCD can be interfaced with the LPC1768 using GPIO pins for the RS, EN, and D0–D7 data lines. The LCD can be operated in 8-bit mode by configuring these GPIO pins as outputs.

 The main GPIO registers used are FIODIR for configuring pins as outputs, FIOSET for setting pins HIGH, FIOCLR for setting pins LOW, and FIOPIN for reading or writing GPIO values.

 An LCD command controls the display, such as clearing the screen or moving the cursor, while data represents the characters displayed on the LCD. The RS pin selects between command mode (RS = 0) and data mode (RS = 1).

Author

Embedded Systems and IOT Trainer– IIES

Updated On: 23-08-26


10+ years of hands-on experience delivering practical training in Embedded Systems and it's design