[Revised]Working with STM32 and Graphics LCD: GLCD 128×64 ST7920

In this revised guide, we shall see how interface GLCD 128×64 with STM32 using SPI method (Serial mode).

In this guide, we shall cover the following:

  • GLCD 12864.
  • Interface with STM32.
  • Developing the driver.
  • Code
  • Demo.

1. GLCD 12864:

At first glance, the 128×64 Graphical LCD Module seems like a bigger brother to the famous 16×2 LCD or 20×4 LCD Modules, with their similar construction and almost similar pin layout.

But there is a significant difference between those two. 16×2 or 20×4 LCDs are essentially character displays. They can only display alpha-numeric characters and some simple custom characters that are confined to a 5×8 matrix.

Coming to the 128×64 Graphical LCD, as the name suggests, it is a Graphical Display consisting of 128×64 i.e., 8192 individually controllable dots.

By using different combinations of pixels, we can basically display characters of various sizes. But the magic doesn’t end there. You can display images and graphics (small animations) as well. In a 128×64 LCD Module, there are 64 rows and 128 columns.

ST7920 LCD Controller

There are several versions of the Graphical LCD in the market. Even though the usage, application and implementations are almost identical, the main difference lies in the internal LCD Controller used to drive the dot matrix display.

Some of the commonly used LCD Controllers are KS0108, SSD1306, ST7920, SH1106, SSD1322, etc. The pin out of the final LCD Module might vary depending on the LCD Controller used. So, please verify the LCD Controller as well as the pin out before making a purchase.

The Graphical LCD Module I purchased consists of ST7920 Controller. It is manufactured by Sitronix and supports three types of bus interfaces i.e., 8-bit mode, 4-bit mode and Serial interface.

If you have used 16×2 LCD Display earlier, then you might be familiar with both 4-bit as well as 8-bit parallel interfaces. The serial interface is something new and we will explore this option in this project.

128×64 LCD Pinout

As I already mentioned, double-check with the manufacturer about the pinout of the Graphical LCD Module. The following table describes the pinout of the 128×64 LCD Module that I have.

Pin NumberPin NamePin Description
1GNDGround
2VCCSupply Voltage
3VOContrast Adjust
4RSRegister Select (CS in Serial)
5RWRead / Write Control (Serial Data In)
6EEnable (Serial CLK)
7 – 14D0 – D7Data
15PSBInterface Selection (0: Serial, 1: 8-bit/4-bit Parallel)
16NCNot Connected
17RSTReset
18VOUTLCD Voltage Doubler Output
19BLABacklight LED Anode
20BLKBacklight LED Cathode

2. Interface with STM32:

The connection as following:

STM32F411 Nucleo-64GLCD12864
5VVdd and LED+ (A)
GNDVss, LED- (K) and PSB
PA0RS
PA1RST
PA7 (MOSI)RW
PA5 (SCK)En

3. Developing the driver:

Before we develop the driver, take a look at the following:

  • SPI and how to use it from here.

We start off by creating new spi.h header file.

Within the header file, include the header guard:

C

Include stdint as following:

C

Declare the following functions:

C

Hence, the header file as following:

C

Now, create spi.c source file.

Within the source file, include the following:

  • Main spi header file.
  • stm32f4 main header file
C

Fot st7920_spi_pins_init function:

  • Enable clock access to GPIOA.
  • Set PA5, PA6 and PA5 to alternate function and set AF05 (SPI1) as alternate function.
  • Set PA0 and PA1 as general output
C

For st7920_spi_config function:

  • Enable clock access to SPI1.
  • Set SPI to master mode.
  • slave management to be software.
  • SPI mode to mode1 (CPHA1 and CPOL0)
  • Enable the module.
C

For st7920_spi_transmit, it takes two parameters:

  • Pointer to uint8_t array that holds the data to be written to GLCD.
  • Size of the buffer.
C

Hence, the source file as following:

C

Thats all for the SPI section.

Now create new header file with name of ST7920.h .

With the header file, include the following:

C

Declare the following functions:

C

Hence the entire header file as following:

C

Now, create new source file with name of ST7920.c.

Within the source, include the following:

C

Then define some macros for CS and RST pins as following:

C

Array to hold the data to be sent out to the display:

C

Variables to hold the display control:

C

Some functions required for the operation:

C

In order to send data/command, the datasheet clearly states the following:

  • Set CS pin to high.
  • Send 5bits of 1 to sync the display.
  • Send RW and RS state.
  • Followed by zero.
  • Then send data/command as two byte as shown in figure below:

Hence, the send data command functions as following which have been defined as static:

C

The delay will allow the GLCD to process the data since it doesn’t have internal buffer to store the data.

For the GLCD initializing sequence:

  • Wait at least 40ms to ensure that GLCD is powered up
  • Reset the GLCD by setting the reset pin low for 40 ms, then high.
  • Send the initializing command.
C

Functions that allow us to display shapes, text pictures etc:

C

In main.c:

C

4. Code:

You may download the entire code from here:

5. Results:

Happy coding 🙂

15 Comments

  • Paul Posted November 10, 2023 1:28 am

    Good evening, I would like to contact you to find out if there is a solution to correct the pixel bug at the top left of the screen, thank you in advance for your response. Paul

    • Husamuldeen Posted November 10, 2023 4:10 am

      Hi,
      post your bug here and we will look at it.

      • Paul Posted November 10, 2023 11:08 am

        In your YouTube video, you can see that there are extra pixels at the top left of the screen when you draw a shape.

        • Paul Posted November 10, 2023 11:16 am

          I have the solution, you just have to delete the line “*image = *p;” in the “SetPixel()” function

  • TESSON Paul Posted November 10, 2023 11:06 am

    I have the solution to fix the pixel bug at the top left of the screen, just remove “*image = *p;” in ST7920.c

  • Paul Posted November 10, 2023 3:50 pm

    I have another question how I could display a custom font and font size with free coordinates?

    • Husamuldeen Posted November 14, 2023 4:31 pm

      Hi,
      you could use graphic mode to display string in the desired size.

      • Paul Posted December 3, 2023 5:46 pm

        Ok thanks you!

        • Aziz Posted December 26, 2024 11:35 pm

          Hello paul, did you manage to display a custom font and font size?

        • billah Posted December 26, 2024 11:36 pm

          Hi Paul, did you managed to display a custom Font and its size?

  • Paul Posted December 3, 2023 5:46 pm

    Hello, I would like to bother you again because I cannot get the program to work to display on a st7920 with an stm32f767zi even though I have adapted each command for the microcontroller, is it possible to help me, thank you in advance

  • Paul Posted December 10, 2023 9:35 pm
    • Husamuldeen Posted December 13, 2023 3:48 am

      Hi,
      what was the issue?

      • Paul Posted December 30, 2023 9:12 pm

        The issue stemmed from the SPI speed, the corrected output voltage of PA7 (2.6V) achieved by removing a jumper on the card, and the required data format for transmission.

  • billah Posted December 26, 2024 11:37 pm

    Hello, how to display a custom font and font size use this library?
    Thank you

Add Comment

Leave a Reply to Husamuldeen Cancel reply

Your email address will not be published. Required fields are marked *