Guide to Interfacing a 4-Bit LCD with the LPC2148 ARM7 Microcontroller
Getting Started with the ARM7 LPC2148 Primer Board
The ARM7 LPC2148 Primer board is an excellent platform for students and developers aiming to build their skills in embedded systems. This development kit is engineered to provide easy access to the microcontroller’s features, making it a great learning tool. It includes support for In-System Programming (ISP) via a standard serial port, which simplifies the process of loading and testing your code. This NXP ARM7 (LPC2148) based kit is designed to streamline the development and debugging process for a wide range of projects that utilize high-speed 32-bit microcontrollers.Understanding the LCD (Liquid Crystal Display)
A Liquid Crystal Display, or LCD, is a fundamental component for creating user interfaces and for debugging projects. It’s a flat-panel display that utilizes the light-modulating properties of liquid crystals to present information. With an LCD module, you can easily display text, status messages, and sensor data to the user.How to Interface an LCD with a Microcontroller
The diagram below illustrates a typical connection between an LCD and a microcontroller. This 2×16 character LCD interface card is versatile, supporting both 4-bit and 8-bit communication modes. It also features a trim pot for convenient contrast adjustment. For a 4-bit interface, which we will be using, a total of seven connections are required: four for data (D4-D7) and three for control signals (RS, R/W, and E).Connecting a 4-Bit LCD to the LPC2148
In this tutorial, we’ll focus on displaying text on the ARM7 LPC2148 Primer Board using a 4-bit LCD module. The LPC2148 Primer board requires the following seven connections for a 4-bit LCD interface:- Data Lines (D4-D7): Connected to microcontroller pins P0.19 through P0.22.
- Register Select (RS): Connected to pin P0.16.
- Read/Write (R/W): Connected to pin P0.17.
- Enable (E): Connected to pin P0.18.
LCD Interface Source Code
The following C code provides a straightforward example of how to display text on a 2×16 LCD module using a 4-bit data interface. Note that small delays are necessary after sending commands or data to ensure the LCD has enough time to process them.Program to 4-bit LCD Display (LPC2148)
In 4-bit mode, the LCD receives data nibble by nibble (4 bits at a time) rather than the full 8-bit byte. This is a common technique in embedded systems to save I/O pins on the microcontroller.
#include <lpc214x.h>
#include <stdio.h>
#define RS 0x10000
#define RW 0x20000
#define EN 0x40000
void lcd_cmd (unsigned char);
void lcd_data (unsigned char);
void lcd_initialize (void);
void lcd_display (void);
void LCD4_Convert(unsigned char);
const unsigned char cmd[4] = {0x28,0x0c,0x06,0x01};
unsigned char msg[] = {">PS-Primer 2148<"};
unsigned char msg1[]= {":: LCD Demo! ::"};
void main()
{
PINSEL1 = 0;
IODIR0 = 0xFF << 16;
lcd_initialize();
lcd_display();
while(1);
}
void delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<12000;j++);
}
void lcd_cmd(unsigned char data)
{
IOCLR0 |= RS; //RS
IOCLR0 |= RW; //RW
LCD4_Convert(data);
}
void lcd_initialize(void)
{
int i;
for(i=0;i<4;i++)
{
IOCLR0 = 0xF << 19;
lcd_cmd(cmd[i]);
delay(15);
}
}
void lcd_data (unsigned char data)
{
IOSET0 |= RS; //RS
IOCLR0 |= RW; //RW
LCD4_Convert(data);
}
void lcd_display (void)
{
char i;
lcd_cmd(0x80);
delay(15);
i=0;
while(msg[i]!='\0')
{
delay(5);
lcd_data(msg[i]);
i++;
}
delay(15);
lcd_cmd(0xc0);
delay(15);
i=0;
while(msg1[i]!='\0')
{
delay(5);
lcd_data(msg1[i]);
i++;
}
delay(15);
}
void LCD4_Convert(unsigned char c)
{
if(c & 0x80) IOSET0 = 1 << 22; else IOCLR0 = 1 << 22;
if(c & 0x40) IOSET0 = 1 << 21; else IOCLR0 = 1 << 21;
if(c & 0x20) IOSET0 = 1 << 20; else IOCLR0 = 1 << 20;
if(c & 0x10) IOSET0 = 1 << 19; else IOCLR0 = 1 << 19;
IOSET0 = EN;
delay(8);
IOCLR0 = EN;
if(c & 0x08) IOSET0 = 1 << 22; else IOCLR0 = 1 << 22;
if(c & 0x04) IOSET0 = 1 << 21; else IOCLR0 = 1 << 21;
if(c & 0x02) IOSET0 = 1 << 20; else IOCLR0 = 1 << 20;
if(c & 0x01) IOSET0 = 1 << 19; else IOCLR0 = 1 << 19;
IOSET0 = EN;
delay(8);
IOCLR0 = EN;
}
Detailed Explanation:
- LCD4_Convert Function: This is the heart of the 4-bit communication. It breaks an 8-bit byte into two 4-bit nibbles. It sends the high nibble (bits 7-4), triggers the Enable (EN) pin to latch the data, then sends the low nibble (bits 3-0) and triggers EN again.
- Control Pins:
RS(Register Select) determines if data sent is a Command (0) or actual Display Data (1).RW(Read/Write) is set to 0 as we are only writing to the LCD.EN(Enable) acts as the strobe to capture data on the falling edge. - Initialization: The
cmdarray contains initialization sequences (like setting 4-bit mode, display ON, cursor settings, and clearing the screen) essential for the LCD controller to function properly.
Compilation & Testing Tips:
To compile the above C code, use the Keil uVision IDE. Ensure the project settings are configured for the LPC2148 target. You can debug the port output without physical hardware using the Keil Simulator (Peripheral view). For physical testing, use Flash Magic to upload the generated HEX file through the UART0 port. If the LCD remains blank, verify your jumper wires, trim pot contrast level, and the 3.3V power supply.
