Interfacing 5V LCD with a 3.3V controller like LPC1768 is a bit complicated to handle. This is the article that explains how a 16x2 LCD interfaces with LPC1768 in 4-bit mode. LCD in 4 bits which means we will use 4 lines instead of 8 lines which saves 4 GPIOs which can be used for other purposes. The environment setup for ARM cortex M3 development is well discussed in this article.
The LPC 1768 is an ARM Cortex-M3-based microcontroller for embedded application capabilities with low power consumption and a high level of integration. The ARM Cortex M3 is designed in a way to enhance debugging capabilities and a higher level of system integration. It runs at a CPU frequency of 100 MHz and incorporates a 3-stage pipeline and uses a Harvard architecture with separate local instruction and data buses for third-bus peripherals. The ARM Cortex-M3 CPU has an internal prefetch unit to support speculative branches. Peripheral components include 512 KB flash memory, 64 KB data memory, Ethernet MAC, USB OTG, 4 UARTs, 8-channel general-purpose DMA controller, 2 SSP controllers, 10-bit DAC, quadrature encoder interface, SPI interface, 3 bus I2C interface, 2-input plus 2-output I2S bus interface, 4 general-purpose timers, ultra-low power real-time clock (RTC) with separate battery supply, and up to 70 user I/O pins General, 6-output general-purpose PWM. The LPC1768/66/65/64 are pin compatible with the 100-pin LPC236x ARM7 based series of microcontrollers.
16X2 LCD Basics:
The Chinese LCD used here is the JHD162A. It has a KS0066U/ HD44780U controller. It has a 16-pin interface device that consists of 2 lines with 16 characters each. The operating voltage is 5V. Additionally, it has LED backlighting. There are 2 modes of operation:
1) Instruction Mode: To initialize and configure the LCD before use
2) Data Mode: Displays the characters of the respective codes that are supplied to the LCD through the Data Pins.
LCD Pin Description:
Pin No |
Symbol |
I/O |
Description |
1 |
VSS |
– |
Floor |
two |
CCV |
|
+5V |
3 |
VEE |
|
Contrast control |
4 |
LOL |
Prohibited |
Command/data logging |
5 |
R/W |
Prohibited |
Read/write log |
6 |
AND |
Entrance exit |
Enable |
7 |
DB0 |
Entrance exit |
Not used in 4-bit mode |
8 |
DB1 |
Entrance exit |
Not used in 4-bit mode |
9 |
DB2 |
Entrance exit |
Not used in 4-bit mode |
10 |
DB3 |
Entrance exit |
Not used in 4-bit mode |
11 |
DB4 |
Entrance exit |
Data bus in 4-bit mode |
12 |
DB5 |
Entrance exit |
Data bus in 4-bit mode |
13 |
DB6 |
Entrance exit |
Data bus in 4-bit mode |
14 |
DB7 |
Entrance exit |
Data bus in 4-bit mode |
15 |
CCV |
– |
For LCD back light |
16 |
VSS |
– |
For LCD back light |
Fig. 1: LCD pin description
Initializing LCD Module:
After checking all the connections from the MCU to the HFC4050 and the LCD Module, we can proceed to display the 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 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 RS and Enable are kept LOW. The next step is to enter some commands into the LCD using the data pins. These commands will not be executed until a pulse is supplied to the Enable pin. After providing a pulse to the command, Enable should be set to High and then Low after a short delay. Then, the command is executed.
The LCD can be set to 4-bit mode by sending the appropriate command called “Function set” to it. The function set is a hexadecimal command from the LCD MPU unit, which selects the LCD operating modes. The “Function Set” is mentioned in the following table:
Fig. 2: Set of LCD functions to configure in 4-bit mode
Description:
DL – Data length (DL = 1 8 bits, DL = 0 4 bits)
N – Number of lines (N = 1 2Lines, N = 0 1Lines)
F – Sources (F = 1 5×10 points, F = 0 5×7 points)
According to the table, the Function Set value for 4-bit mode will be 0010 0000(0x20) because DL=0. The “Function Set” value for the LCD configuration 2 lines (N=1), 5X7 points (F=0) and 4-bit mode (DL=0) will be 0010 1000(0x28).
When power supply is supplied to the LCD, it remains in 8-bit mode. Now if 0x20 is sent, the lower nibble will not be received by the LCD because four data lines (D4-D7) are connected, so 0x02 is sent instead of 0x20. For more details on the LCD interface, see 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 start your hands-on experiment.
Code.rar
Code description:
These explained functions are included in the creation of an LCD library and included in the encoding files.
The function below is to send command bytes to the LCD:
Lcd_CmdWrite
The function below is to send bytes of data to the LCD:
LCD_DataWrite
4-bit mode initialization:
LCD_CmdWrite(0x02); // Initialize the LCD in 4-bit mode
LCD_CmdWrite(0x28); // enable 5×7 mode for characters
Lcd_CmdWrite(0x0E); // Display OFF, Cursor ON
LCD_CmdWrite(0x01); //Clears the display
LCD_CmdWrite(0x80); // Move the cursor to the beginning of the first line
Project source code
###
Circuit diagrams
LCD interface circuit diagram in 4-bit mode with LPC1768 |
Project Components
- LCD
- LED
- Resistor
Project video