Page Links
MSP430F5529LP accessory Library: LCD_HD44780_4BIT


The LCD_HD44780_4BIT accessory library provides a 4-bit parallel interface to a 20x4 character LCD display, based on the Hitachi HD44780 LCD Display controller. This provides a very simple and inexpensive way to add a user interface to any project. These displays are available from several vendors with prices typically ranging from $10-$20. In order to use the parallel interface it is best to purchase a bare display (as shown below), or one with a simple header loaded.

lcd_4bit_front_600.png lcd_4bit_back_370.png


The following files must be included in the project to use the LCD_HD44780_4BIT accessory library:


If the LCD_HD44780_4BIT accessory library is included in a project, the following additional files are also required.

Public Definitions

#define LCD_ROW1  0x80 
#define LCD_ROW2  0xC0 
#define LCD_ROW3  0x94 
#define LCD_ROW4  0xD4    
 These public definitions provide an easy way to locate the cursor at the starting position of any of the 4 LCD rows. They can also be used to find a position relative to the start of a row. These definitions are intended to be used with the LCD_SetPosition function. Once the cursor position is set, then the LCD_Print function will start printing text at the cursor position defined. Example usages are provided below that begin printing at the 1st position of row 1, or the 6th position of row 2.  
LCD_SetPosition(LCD_ROW1);    or    
LCD_SetPosition(LCD_ROW2 + 5);

Public Functions

        uint16_t LcdPin_RS,
        uint16_t LcdPin_RW,
        uint16_t LcdPin_EN,
        uint16_t LcdPin_D4,
        uint16_t LcdPin_D5,
        uint16_t LcdPin_D6,
        uint16_t LcdPin_D7);
 Calling this function initializes the LCD display in 4-bit interface mode using the pin numbers provided. Pin numbers must be provided as the package pin numbers (see GPIO hardware library for more information). During initialization the display is turned on and cleared, and the cursor is set to position 0 (LCD_ROW1), with the cursor set to off and no blinking (i.e. not visible). If any of the pin numbers passed to this function are invalid, no action will be taken. 
        uint16_t value);
 Calling this function results in placing the LCD cursor at the location requested. The next character written to the LCD display will be written at this location. If the cursor position requested is invalid, no action will be taken and the cursor position will not be changed. It is recommended that the defined values LCD_ROW1 through LCD_ROW4 be used with this function. The cursor can be placed at any location between LCD_ROWx to LCD_ROWx + 20.       
        const char * format, ...);
 Calling this function prints the characters defined by the format string. This function works the same as the printf function in the C standard library. The format string can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. The format tags prototype is %[flags][width][.precision][length]specifier (see the C standard library for more information). 

CAUTION: All of the LCD functions contain blocking delays to ensure that critical timing requirements are met. Interrupts are not disabled during these delays, however the application will pause execution until the LCD function call completes.

Example Code

The following example code is provided to demonstrate the functionality of this library. All examples are presented as main.c files, which are available on GitHub. In order to build the example projects, follow the instructions here for how to create and configure a new project.

Example Summary Description
LCD Counting Example using 4-bit Interface
This example demonstrates writing to a 20x4 character LCD module based on the HD44780 display controller using the 4-bit parallel interface mode. The example uses a blocking delay to set the update speed of an 8-bit up-counter which is displayed in decimal, hexadecimal, and binary formats on the LCD. This example can be used to explore display characteristics such as refresh rate.
Requires: A 20x4 character LCD display based on the HD44780 controller and the LCD_HD44780_4BIT accessory library files.

Purchasing a Bare Display

The display images shown at the top of this page are of the Cofufu 20x4 Black on Green LCD module from Amazon. This particular display comes with headers and 10k potentiometers to implement the contrast adjustment. If a white on blue version is desired one can be found at AdaFruit that also comes with a header and potentiometer. Typically if you order a display with a header and potentiometer, you will receive a bare display. If you order what appears to be a bare display by itself, you may find that the order is "upgraded" to include a free I2C daughter card already attached to the display. This can be annoying if what you wanted was in fact a bare display. The Cofufu is my go-to display for these applications.

Backlight Options

The most common LCD backlight options are either Black on Yellow/Green or White on Blue (both are shown below). The choice of one versus the other is primarily one of aesthetics, but there are some things to consider. The white on blue has a "softer" look to it than the Black on Yellow/Green which can be a little "glaring". However, the Black on Yellow/Green has several advantages over the White on Blue displays. Both LCDs have a fairly slow refresh rate which produces a "ghosting" effect that lasts a little after each update, however this is more noticeable on the White on Blue displays. The viewing angle on the Black on Yellow/Green is much better, making it possible to read the display from the sides much farther than with the White on Blue displays. The Black on Yellow/Green displays are also far less dependent on the backlight for readability, which makes it much easier to fine tune the backlight level. In fact, if the backlight breaks on the White on Blue display, it becomes useless.

Dispay examples with backlight on

lcd_green_front_backlight_ON.png lcd_blue_front_backlight_ON.png
Dispay examples with backlight off

lcd_green_front_backlight_OFF.png lcd_blue_front_backlight_OFF.png

Backlight Current Limiting

Most of the LCD displays appear to be based on the 2004A LCM module, but there are a lot of inconsistencies between displays that appear to be either vendor-specific or just time-dependent. Either way it is possible that you may receive a display with different current limiting resistors, or possibly no current limiting at all! Always check the value of these resistors before applying power! If power is applied to a module with no current limiting, the backlight will be destroyed. The resistance between the "A" (Anode) and "K" (Kathode) input pins and the backlight connections should generally be around 51-68 ohms. You won't be able to measure directly between A and K, but will have to follow the path between the A and K pins to the backlight pins.

A picture is shown of the current limiting resistors on one example module. The resistors may be located differently depending on LCM revision. In this example, there are two resistors (R8 and R9) which are each in series with one of the backlight pins. The total resistance being the series combination of both values. The resistors are 1206 size (a short-hand code for having a length and width of 0.125" x 0.06") and are marked with "0" and "510". The "0" is a special designator for a jumper that provides no resistance. The "510" is a 3-digit code for standard parts, where the first 2 digits define the base resistance and the final digit defines the number of zeroes to be added to the base value (Or for a code "XXY", you could think of it as XX * 10^Y). In this case "510" = 51 ohms (51 + no zeroes).


The LCD backlight connections are intended to be connected to a +5V source (A=+5V, K=GND), however both the Yellow/Green and Blue backlights will function at +3.3V which is also available on the MSP430F5529LP board. This can provide a less harsh and glaring look in the case of Yellow/Green, and a less washed out look for Blue. The example photos above were both taken with a backlight connection to +3.3V, which operates the backlights at ~10 mA as opposed to ~40 mA when connected to a 5V supply.

The following tables provide measured values of backlight current and backlight voltage when supply voltages between 3V-5.5V were applied to the A-K pins. Both of the boards used for data collection had a 51 Ohm current limiting resistor installed. This affects the amount of current obtained for a given A to K voltage applied, however the backlight voltage to current relationship is unaffected.

green_VI_table.png     blue_VI_table.png

Also provided below is the regression fit of the IV relationship for the Yellow/Green and Blue backlights.

green_VI_plot.png     blue_VI_plot.png