STM32F103 SPL Tutorial 7 – Interfacing LCD16x2


LCD16x2 is a popular display module and commonly used in various devices. In this tutorial, I will explain about how to use LCD16x2 with STM32F103 microcontroller. If you want to know the detail how LCD16x2 works, you can follow this tutorial. To interface the LCD with STM32F103, you need the library files (lcd16x2.h and lcd16x2.c). You can get the library files and from here.

STM32F103 LCD16x2

LCD16x2 Library

In this library, we can configure the GPIO for LCD in lcd16x2.h file. This definitions (in source code below) in lcd16x2.h contain the GPIO configuration for the LCD. In this definitions, you can configure the GPIO for LCD control lines (RS, RW, EN) and LCD data lines (D4-D7). This library only support 4-bit data mode.

Example Code

To use this library, in the main.c file, you must call lcd16x2_init() function. There are 5 parameters for this function that can be used, depending on what type of cursor you want to use:


This library is also support custom character display. This code below is an example how to use the library for displaying a string and also a custom character. The custom character is created by defining the pattern. In this example, I use the pattern for battery indicator.

This is the result:

Go to the part 8 of the tutorial.