{"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

Table of Contents<\/h2>\n
    \n
  1. STM32 LCD Display<\/a>\n\n<\/li>\n
  2. STM32 LCD 16×2 Interfacing<\/a>\n\n\n<\/li>\n\n<\/li>\n\n<\/li>\n\n
  3. STM32 LCD 16×2 Library (Driver)<\/a>\n\n\n<\/li>\n\n<\/li>\n\n
  4. STM32 LCD 16×2 Library Integration<\/a>\n\n<\/li>\n
  5. STM32 LCD 16×2 Example<\/a>\n\n<\/li>\n
  6. STM32 LCD 16×2 Example Code Project<\/a>\n\n\n<\/li>\n\n<\/li>\n\n
  7. Wrap Up<\/a>\n<\/li><\/ol>\n\n\n
    \n\n\n

    STM32 LCD Display<\/strong><\/h2>\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

    \"COMPLETE<\/figure>\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

    \n
    \n
    \n\n
    \"LCD<\/a><\/figure>\n\n<\/div><\/div><\/div>\n\n
    \n
    Alphanumeric LCD 16×2 Display Interfacing<\/a><\/div>\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


    \n\n\n

    STM32 LCD 16×2 Interfacing<\/strong><\/h2>\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

    STM32 LCD Interface Connection<\/strong><\/h3>\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

    \n
    \u2755 Note<\/div>\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

    STM32 LCD Initialization Procedure<\/strong><\/h3>\n\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

    \"LCD<\/figure>\n\n\n\n

    <\/p>\n\n\n

    LCD Instructions (Commands)<\/strong><\/h3>\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

    \"LCD<\/figure>\n\n\n\n

    <\/p>\n\n\n

    STM32 LCD EN Signal Timing<\/strong><\/h3>\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


    \n\n\n

    STM32 LCD 16×2 Library (Driver)<\/strong><\/h2>\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

    STM32 LCD 16×2 Library Directory & Files<\/strong><\/h3>\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

    \n
    \n\n