{"id":4721,"date":"2020-06-24T03:41:19","date_gmt":"2020-06-24T01:41:19","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=4721"},"modified":"2024-02-18T13:56:39","modified_gmt":"2024-02-18T11:56:39","slug":"stm32-lcd-16x2-tutorial-library-alphanumeric-lcd-16x2-interfacing","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/stm32-lcd-16x2-tutorial-library-alphanumeric-lcd-16x2-interfacing\/","title":{"rendered":"STM32 LCD 16×2 Library & Example | LCD Display Interfacing"},"content":{"rendered":"\n
In this tutorial, you’ll learn how to interface STM32 with LCD 16×2 display and integrate the STM32 LCD library into your project. We’ll start with an introduction to the LCD 16×2 display, how to implement an STM32 LCD 16×2 Library (driver), and test it on a real STM32 blue pill board. <\/p>\n\n\n\n
The STM32 LCD library we’ll be using in this tutorial is carefully designed to give you a highly configurable and scalable firmware driver to meet the needs of any project you might be working on. It might take some work to go through this tutorial and learn how this driver works & how to integrate it into your project, but it’s well worth it in the end. Without further ado, let’s get right into it!<\/p>\n\n\n
We typically add a 16\u00d72 Alphanumeric LCD to small embedded systems & projects to enhance the user experience and UI of the device\/project. You can use it to display text messages to the user, number, etc. Other types of LCDs provide different features such as the number of columns and rows (characters) and maybe colored display, and also different interfaces (parallel, spi, i2c, etc).<\/p>\n\n\n\n <\/p>\n\n\n\n For this tutorial, we\u2019ll consider the 16\u00d72 LCD with a 16-pin header interface. Assuming it has the standard Hitachi LCD driver HD44780<\/strong> controller. The Alphanumeric LCD 16×2 Tutorial linked below highlights everything you need to know. It’ll help you learn the internals of the LCD driver IC, its registers, commands, and how it works & gets initialized, etc. I highly recommend that you check it out.<\/p>\n\n\n This article will give more in-depth information about the 16×2 LCD display with the Hitachi HD44780 driver IC, its registers, configurations, instructions, initialization sequence, and much more.<\/p>\n\n<\/div><\/div><\/div>\n<\/div>\n<\/div><\/div>\n\n\n The best way in my opinion for interfacing alphanumeric LCD screens is using an external I2C LCD driver chip. In this way, you save up a lot of valuable GPIO pins for other uses and it only requires 2 wires on the I2C bus. However, it’s going to be a topic for a future tutorial as we didn’t cover the I2C in STM32 MCUs yet.<\/p>\n\n\n In this tutorial, we’ll be interfacing the LCD 16×2 display in the 4-bit mode which requires 6 GPIO pins. And as you know the STM32 microcontroller is a 3.3v logic device and the LCD is 5v. But it is not a big deal, as the STM32 output (3.3v) pins will be correctly detected by the LCD (5v) input pins. And the only 5v line that is required is the LCD VDD<\/sub><\/strong>, and you can supply it from the blue pill board 5v pin.<\/p>\n\n\n\n Don’t also forget to connect the contrast control potentiometer as indicated in the diagram shown above. Low contrast may seem to you like a not-working-LCD and hence unnecessarily waste so much time debugging a code that actually works!<\/p>\n\n\n After flashing the code to your STM32 blue pill board, the LCD may not work from the USB voltage supply coming from the debugger. It’s recommended to un-plug the programmer and use an external power supply or a USB power bank. The LCD may not work at all from the laptop USB or in some cases misbehave, so stay safe with an external power source.<\/p>\n\n<\/div><\/div>\n\n The STM32 microcontroller has to first initialize the LCD display before it can actually send any characters to be displayed correctly. The initialization procedure is step-by-step indicated in the LCD driver datasheet for both modes 4-bit and 8-bit. And it requires a few delay instructions, so I’ll be using the DWT delay which we’ve developed in the previous tutorial.<\/p>\n\n\n\n <\/p>\n\n\n The available instructions that the LCD driver IC can execute are listed in the datasheet. As well as the execution time for each instruction. Therefore, you should be careful at this time! you can use an extra pin to read the busy flag bit from the LCD to know whether it has executed the last instruction or not. Otherwise, it’s mandatory to use time delays according to the datasheet specs. Here is a list of the LCD instructions.<\/p>\n\n\n\n <\/p>\n\n\n One thing that we should care about is the enable pulse that we should send to the LCD driver (on the EN pin) after each command in order to transfer the 8-bit CMD word (whether at once or at 2 stages in 4bit mode).<\/p>\n\n\n\n The datasheet says it should be no less than 200nSec. However, an old LCD with me didn’t receive any data until this pulse width was increased up to 500\u00b5s (which is so long in fact). Another LCD could work just fine with 50\u00b5s pulses but no less than that. Another one with a different color did work absolutely fine with a 1\u00b5s pulse, which is a pretty reasonable amount of delay.<\/p>\n\n\n\n We can tweak this parameter in the code later on but I’ll keep it at 5\u00b5s as a reference value that should work just as fine on most hardware setups.<\/p>\n\n\n\n Let’s now move on to the STM32 LCD 16×2 library that I’ve developed for this STM32 series of tutorials which is probably what we’ve searched for before landing on this article. In this section, we’ll explore the library files, functions, and configurations. So you can make the best use of it and also know how to modify or add any extra functionalities that your project needs. <\/p>\n\n\n\n In the next section, we’ll discuss how to integrate this STM32 LCD 16×2 library into your STM32 CubeIDE projects step-by-step.<\/p>\n\n\n The STM32 LCD16X2<\/strong> library is part of the ECUAL<\/strong> (ECU abstraction layer) that we’re continuously building through this STM32 Series of Tutorials. The driver consists of 4 files: 2 core files (a header file & a source file) + 2 configuration files (a header & a source).<\/p>\n\n\n The STM32 LCD16X2 library needs you to also include the util<\/strong> library as a dependency because it uses the delay<\/strong> utility built into the util driver. The util library is also provided in the download section & code examples near the end of this tutorial.<\/p>\n\n\n The core header file ( LCD16x2.h File<\/strong><\/span><\/p>\n\n\n\n <\/p>\n\n\n\n LCD16x2.c File<\/strong><\/span><\/p>\n\n\n\n <\/p>\n\n\n Note that all of the library interface functions have the first input argument as The STM32 LCD16X2 library can be easily configured using the configuration files ending with (_cfg<\/strong>), and here is how it works. The configuration header file declares a struct that packs up all the configuration parameters into one variable type as shown below.<\/p>\n\n\n\n LCD16x2_cfg.h File<\/strong><\/span><\/p>\n\n\n\n <\/p>\n\n\n\n The configuration parameter type declared above will allow the user to choose the following: (LCD instance index, GPIO port & pin for all LCD pins).<\/p>\n\n\n\n The configuration source file will have the actual configuration parameter variable creation and assignation. This is the file you need to edit in order to configure this STM32 LCD16X2 library. So you can assign the desired IO pins for the LCD parallel communication.<\/p>\n\n\n\n LCD16x2_cfg.c File<\/strong><\/span><\/p>\n\n\n\n <\/p>\n\n\n\n Note that the configuration parameter structure is extern<\/strong>ed to the header file so that in the This type of configuration is called linking configuration, as it gets resolved during the compilation time in the linking stage and the user (the programmer) doesn’t have to compile the whole project in order to change the configurations, only compile the configuration source and link it with your application object files. This topic and other types of configurations will be discussed in a future tutorial.<\/p>\n\n\n\n Download The ECUAL\/ LCD16X2 Driver Folder<\/strong><\/a><\/p>\n\n\n\n In this section, we’ll discuss how to integrate the STM32 LCD16X2 library into your projects step-by-step. Assuming you’ve downloaded the provided library files from the links in this tutorial or from the GitHub Repo<\/a><\/strong> for this STM32 Tutorials Series<\/a><\/strong>, here is how you can use it in action.<\/p>\n\n\n\n The steps below apply to any other driver (library) in the ECUAL software components directory.<\/p>\n\n\n Step #1<\/strong><\/p>\n\n<\/div>\n\n\n Configure The Required Hardware Peripherals in STM32CubeMX<\/strong><\/p>\n\n\n\n The first step is to head over to STM32CubeMX, create a new project, and configure the hardware needed for the library.<\/p>\n\n\n\n STM32 LCD16X2 Library HW Requirements<\/strong><\/span><\/p>\n\n\n\n <\/p>\n\n\n\n You can choose any 6x GPIO pins you want, they don’t have to be in the same GPIOx port.<\/p>\n\n\n\n Add any other peripherals you need in your project and configure the RCC clock however you want. There is no conflict between the LCD library and any other hardware configurations you may do in CubeMX.<\/p>\n\n\n\n Once you’re done with CubeMX configurations, generate the project code and head over to STM32CubeIDE.<\/p>\n\n\n Step #2<\/strong><\/p>\n\n<\/div>\n\n\n Copy & Paste The Library Folders<\/strong><\/p>\n\n\n\n Copy the provided library folders (ECUAL\/LCD16X2<\/strong> + util<\/strong>) and open the STM32CubeIDE. <\/p>\n\n\n\n In STM32CubeIDE, right-click the project name and paste the library folders.<\/p>\n\n\n\n Step #3<\/strong><\/p>\n\n<\/div>\n\n\n Add The Library Directories To Paths & Symbols<\/strong><\/p>\n\n\n\n The library folders have been added to our project but it’s not yet considered as a source code directory, so it won’t be compiled and give you a linking error in the linking stage if you call any of its functions. <\/p>\n\n\n\n Right-click the project name and click Properties<\/strong>. And navigate to C\/C++ paths and symbols<\/strong>, and source locations<\/strong> tab. In the source locations tab, click add folder and add our library folders (ECUAL + util).<\/p>\n\n\n\n <\/p>\n\n\n Step #4<\/strong><\/p>\n\n<\/div>\n\n\n Configure The LCD16X2 Library Using The cfg Parameter<\/strong><\/p>\n\n\n\n Open the Step #5<\/strong><\/p>\n\n<\/div>\n\n\n Include The Library Header File & Use it!<\/strong><\/p>\n\n\n\n Now you’re ready to use the STM32 LCD16X2 library, just include the header file as shown in the test code below.<\/p>\n\n\n\n <\/p>\n\n\n\n Objectives of This STM32 LCD 16×2 Example Project<\/strong>:<\/p>\n\n\n\n In this LAB, our goal is to build a system that initializes the LCD driver. Which in turn initializes the configuration-defined GPIO pins and therefore sends the initialization commands to the LCD as described in its datasheet. After this, we can easily call the LCD driver functions to set the cursor position, print strings, and shift the entire display on the LCD right and left. It’s a very basic and simple LAB.<\/p>\n\n\n\n Follow The step-by-step Library Integration Guide shown earlier to create the project and add the LCD16X2 library folders. Copy the example application code below, build the project, and flash it to your STM32 blue pill board.<\/p>\n\n\n The Application Code For This LAB (main.c)<\/strong><\/span><\/p>\n\n\n\n <\/p>\n\n\n <\/p>\n\n\n
\n\n\nSTM32 LCD 16×2 Interfacing<\/strong><\/h2>\n\n\n
STM32 LCD Interface Connection<\/strong><\/h3>\n\n\n
STM32 LCD Initialization Procedure<\/strong><\/h3>\n\n\n
LCD Instructions (Commands)<\/strong><\/h3>\n\n\n
STM32 LCD EN Signal Timing<\/strong><\/h3>\n\n\n
\n\n\nSTM32 LCD 16×2 Library (Driver)<\/strong><\/h2>\n\n\n
STM32 LCD 16×2 Library Directory & Files<\/strong><\/h3>\n\n\n
\n
STM32 LCD 16×2 Library APIs (Functions)<\/strong><\/h3>\n\n\n
LCD16X2.h<\/code>) has all functions’ declarations (APIs) that we’ll be using in our application layer.<\/p>\n\n\n\n
#define LCD16X2_MAX\t1 \/\/ Maximum Number of LCD16x2 Modules in Your Project\n#define LCD16X2_1\t0 \/\/ LCD16X2 Instance Number 1 (Add more if you need)\n\n\/\/-----[ Prototypes For All Functions ]-----\n\nextern void LCD16X2_Init(uint8_t); \/\/ Initialize The LCD For 4-Bit Interface\nextern void LCD16X2_Clear(uint8_t); \/\/ Clear The LCD Display\n\nextern void LCD16X2_Set_Cursor(uint8_t, unsigned char, unsigned char); \/\/ Set Cursor Position\n\nextern void LCD16X2_Write_Char(uint8_t, char); \/\/ Write Character To LCD At Current Position\nextern void LCD16X2_Write_String(uint8_t, char*); \/\/ Write A String To LCD\n\nextern void LCD16X2_SL(uint8_t); \/\/ Shift The Entire Display To The Left\nextern void LCD16X2_SR(uint8_t); \/\/ Shift The Entire Display To The Right<\/pre>\n\n\n\n
\/\/ Too long to paste here, but it has the definition of all API functions declared in the header file \n\/\/ + any internal supplementary static functions<\/pre>\n\n\n\n
uint8_t<\/code> which is inteded to be the index of the LCD instance you’re addressing. This library supports multiple instances of creation and individual addressing for multiple LCD16X2 display units. We’ll create an example for this multi-LCD application later on in this tutorial.<\/p>\n\n<\/div><\/div>\n\n
STM32 LCD 16×2 Library Configurations<\/strong><\/h3>\n\n\n
typedef struct\n{\n\t\/\/ LCD Module Instance Index\n\tuint8_t LCD16X2_Instance;\n\n\t\/\/ LCD Pin: D4\n\tGPIO_TypeDef * D4_GPIOx;\n\tuint16_t D4_PINx;\n\n\t\/\/ LCD Pin: D5\n\tGPIO_TypeDef * D5_GPIOx;\n\tuint16_t D5_PINx;\n\n\t\/\/ LCD Pin: D6\n\tGPIO_TypeDef * D6_GPIOx;\n\tuint16_t D6_PINx;\n\n\t\/\/ LCD Pin: D7\n\tGPIO_TypeDef * D7_GPIOx;\n\tuint16_t D7_PINx;\n\n\t\/\/ LCD Pin: EN\n\tGPIO_TypeDef * EN_GPIOx;\n\tuint16_t EN_PINx;\n\n\t\/\/ LCD Pin: RS\n\tGPIO_TypeDef * RS_GPIOx;\n\tuint16_t RS_PINx;\n\n}LCD16X2_CfgType;\n\nextern const LCD16X2_CfgType LCD16X2_CfgParam[LCD16X2_MAX];<\/pre>\n\n\n\n
const LCD16X2_CfgType LCD16X2_CfgParam[LCD16X2_MAX] =\n{\n\t{ \/* Configuration Parameter For LCD Instance #1 *\/\n\t\tLCD16X2_1,\t\t\t\/* Index of LCD Instance #1 *\/\n\t\tGPIOA,\tGPIO_PIN_2, \/* LCD D4 Pin GPIO Port & Pin *\/\n\t\tGPIOA,\tGPIO_PIN_3, \/* LCD D5 Pin GPIO Port & Pin *\/\n\t\tGPIOA,\tGPIO_PIN_4, \/* LCD D6 Pin GPIO Port & Pin *\/\n\t\tGPIOA,\tGPIO_PIN_5, \/* LCD D7 Pin GPIO Port & Pin *\/\n\t\tGPIOA,\tGPIO_PIN_6, \/* LCD EN Pin GPIO Port & Pin *\/\n\t\tGPIOA,\tGPIO_PIN_7 \/* LCD RS Pin GPIO Port & Pin *\/\n\t}\n};<\/pre>\n\n\n\n
LCD16X2.c<\/code> source code we can include the configuration header file
LCD16X2_cfg.h<\/code> and see that global config parameter and use it. <\/p>\n\n\n\n
\n\n\nSTM32 LCD 16×2 Library Integration<\/strong><\/h2>\n\n\n
\n
<\/td> <\/td> So it’ll look like this at the end:
<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\nLCD16X2_cfg.c<\/code> file and modify the configuration parameter items to remap the GPIO pins to match the pins you’ve selected in CubeMX<\/p>\n\n\n
#include \"main.h\"\n#include \"..\/..\/ECUAL\/LCD16X2\/LCD16X2.h\"\n\n#define MyLCD LCD16X2_1\n\nvoid SystemClock_Config(void);\nstatic void MX_GPIO_Init(void);\n\nint main(void)\n{\n HAL_Init();\n SystemClock_Config();\n MX_GPIO_Init();\n\n LCD16X2_Init(MyLCD);\n LCD16X2_Clear(MyLCD);\n LCD16X2_Set_Cursor(MyLCD, 1, 1);\n LCD16X2_Write_String(MyLCD, \" DeepBlue\");\n LCD16X2_Set_Cursor(MyLCD, 2, 1);\n LCD16X2_Write_String(MyLCD, \"STM32 Course\");\n\n while (1)\n {\n \tLCD16X2_SR(MyLCD); HAL_Delay(1500);\n LCD16X2_SR(MyLCD); HAL_Delay(1500);\n LCD16X2_SR(MyLCD); HAL_Delay(1500);\n LCD16X2_SR(MyLCD); HAL_Delay(1500);\n\n LCD16X2_SL(MyLCD); HAL_Delay(1500);\n LCD16X2_SL(MyLCD); HAL_Delay(1500);\n LCD16X2_SL(MyLCD); HAL_Delay(1500);\n LCD16X2_SL(MyLCD); HAL_Delay(1500);\n }\n}<\/pre>\n\n\n\n
\n\n\nSTM32 LCD 16×2 Example<\/strong><\/h2>\n\n\n
\n
\n\n\nSTM32 LCD 16×2 Example Code Project<\/strong><\/h2>\n\n\n
STM32 LCD 16×2 Example Code<\/strong><\/h3>\n\n\n
#include \"main.h\"\n#include \"..\/..\/ECUAL\/LCD16X2\/LCD16X2.h\"\n\n#define MyLCD LCD16X2_1\n\nvoid SystemClock_Config(void);\nstatic void MX_GPIO_Init(void);\n\nint main(void)\n{\n HAL_Init();\n SystemClock_Config();\n MX_GPIO_Init();\n\n LCD16X2_Init(MyLCD);\n LCD16X2_Clear(MyLCD);\n LCD16X2_Set_Cursor(MyLCD, 1, 1);\n LCD16X2_Write_String(MyLCD, \" DeepBlue\");\n LCD16X2_Set_Cursor(MyLCD, 2, 1);\n LCD16X2_Write_String(MyLCD, \"STM32 Course\");\n\n while (1)\n {\n \tLCD16X2_SR(MyLCD); HAL_Delay(450);\n LCD16X2_SR(MyLCD); HAL_Delay(450);\n LCD16X2_SR(MyLCD); HAL_Delay(450);\n LCD16X2_SR(MyLCD); HAL_Delay(450);\n\n LCD16X2_SL(MyLCD); HAL_Delay(450);\n LCD16X2_SL(MyLCD); HAL_Delay(450);\n LCD16X2_SL(MyLCD); HAL_Delay(450);\n LCD16X2_SL(MyLCD); HAL_Delay(450);\n }\n}<\/pre>\n\n\n\n
STM32 LCD 16×2 Example Connections<\/strong><\/h3>\n\n\n
Testing Result (Demo Video)<\/strong><\/h3>\n\n