Interfacing a GLCD 128×64 ST7920 with an STM32 microcontroller can be a great way to display information and graphics in embedded systems applications. This process requires appropriate software and hardware setup, and our article includes an example code to help with the implementation.

Disclaimer:

Advertisements

Basics:

Interfacing a GLCD 128×64 ST7920 with an STM32 microcontroller can be a bit more complex than a standard 16×2 LCD due to its larger size and graphic capabilities. However, it is still possible to achieve this with the right hardware connections and software programming. In this guide, we will go over the steps required to interface a GLCD 128×64 ST7920 with an STM32 microcontroller, including the pin configuration, initialization, and example code for displaying graphics and text on the GLCD screen.

Interfacing the GLCD 128×64 ST7920 with an STM32 microcontroller

To begin interfacing the GLCD 128×64 ST7920 with an STM32 microcontroller, the following hardware connections will be required:

  • Pin 1 (VSS) of the GLCD should be connected to GND
  • Pin 2 (VDD) of the GLCD should be connected to +5V
  • Pin 3 (Vo) of the GLCD should be connected to a potentiometer or voltage divider to adjust the contrast of the screen
  • Pin 4 (RS) of the GLCD should be connected to a GPIO pin on the STM32
  • Pin 5 (R/W) of the GLCD should be connected to GND
  • Pin 6 (E) of the GLCD should be connected to a GPIO pin on the STM32
  • Pins 7-14 (DB0-DB7) of the GLCD should be connected to GPIO pins on the STM32
  • Pin 15 (CS1) of the GLCD should be connected to a GPIO pin on the STM32
  • Pin 16 (CS2) of the GLCD should be connected to a GPIO pin on the STM32
  • Pin 17 (RST) of the GLCD should be connected to a GPIO pin on the STM32

Once the hardware connections are made, the next step is to initialize the GLCD using the ST7920 controller. This involves sending a series of commands to the GLCD to set up the display parameters and configure the graphics modes.

interfacing the GLCD 128x64 ST7920 with an STM32 microcontroller

Here is an example code for interfacing the GLCD 128×64 ST7920 with an STM32 microcontroller using the STM32CubeIDE and HAL libraries:

Example Code:

#include "stm32f4xx_hal.h"
#include <stdio.h>
#include <string.h>

#define GLCD_RS_Pin GPIO_PIN_3
#define GLCD_RS_GPIO_Port GPIOA
#define GLCD_RW_Pin GPIO_PIN_2
#define GLCD_RW_GPIO_Port GPIOA
#define GLCD_E_Pin GPIO_PIN_0
#define GLCD_E_GPIO_Port GPIOB
#define GLCD_CS1_Pin GPIO_PIN_1
#define GLCD_CS1_GPIO_Port GPIOB
#define GLCD_CS2_Pin GPIO_PIN_2
#define GLCD_CS2_GPIO_Port GPIOB
#define GLCD_RST_Pin GPIO_PIN_12
#define GLCD_RST_GPIO_Port GPIOB
#define GLCD_DB0_Pin GPIO_PIN_15
#define GLCD_DB0_GPIO_Port GPIOA
#define GLCD_DB1_Pin GPIO_PIN_8
#define GLCD_DB1_GPIO_Port GPIOB
#define GLCD_DB2_Pin GPIO_PIN_9
#define GLCD_DB2_GPIO_Port GPIOB
#define GLCD_DB3_Pin GPIO_PIN_0
#define GLCD_DB3_GPIO_Port GPIOE
#define GLCD_DB4_Pin GPIO_PIN_1
#define GLCD_DB4_GPIO_Port GPIOE
#define GLCD_DB5_Pin GPIO_PIN_2
#define GLCD_DB5_GPIO_Port GPIOE
#define GLCD_DB6_Pin GPIO_PIN_3
#define GLCD_DB6_GPIO_Port GPIOE
#define GLCD_DB7_Pin GPIO_PIN_4
#define GLCD_DB7_GPIO_Port GPIOE

void GLCD_SendCommand(uint8_t command) {
  HAL_GPIO_WritePin(GLCD_RS_GPIO_Port, GLCD_RS_Pin, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GLCD_RW_GPIO_Port, GLCD_RW_Pin, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GLCD_E_GPIO_Port, GLCD_E_Pin, GPIO_PIN_RESET);
  GPIOE->ODR = command << 8;
  HAL_GPIO_WritePin(GLCD_E_GPIO_Port, GLCD_E_Pin, GPIO_PIN_SET);
  HAL_Delay(1);
  HAL_GPIO_WritePin(GLCD_E_GPIO_Port, GLCD_E_Pin, GPIO_PIN_RESET);
}

void GLCD_SendData(uint8_t data) {
HAL_GPIO_WritePin(GLCD_RS_GPIO_Port, GLCD_RS_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GLCD_RW_GPIO_Port, GLCD_RW_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GLCD_E_GPIO_Port, GLCD_E_Pin, GPIO_PIN_RESET);
GPIOE->ODR = data << 8;
HAL_GPIO_WritePin(GLCD_E_GPIO_Port, GLCD_E_Pin, GPIO_PIN_SET);
HAL_Delay(1);
HAL_GPIO_WritePin(GLCD_E_GPIO_Port, GLCD_E_Pin, GPIO_PIN_RESET);
}

void GLCD_Init() {
HAL_GPIO_WritePin(GLCD_RST_GPIO_Port, GLCD_RST_Pin, GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GLCD_RST_GPIO_Port, GLCD_RST_Pin, GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(GLCD_RST_GPIO_Port, GLCD_RST_Pin, GPIO_PIN_SET);
HAL_Delay(10);

GLCD_SendCommand(0b00111000); // function set
GLCD_SendCommand(0b00001100); // display on, cursor off, blink off
GLCD_SendCommand(0b00000110); // entry mode set
GLCD_SendCommand(0b00000001); // clear display
HAL_Delay(10);
}

void GLCD_SetPage(uint8_t page) {
uint8_t cmd = 0b10111000 | page;
GLCD_SendCommand(cmd);
}

void GLCD_SetColumn(uint8_t column) {
uint8_t cmd1 = 0b00000000 | (column & 0x0F);
uint8_t cmd2 = 0b00010000 | ((column >> 4) & 0x0F);
GLCD_SendCommand(cmd1);
GLCD_SendCommand(cmd2);
}

void GLCD_WriteChar(uint8_t ch) {
for (int i = 0; i < 5; i++) {
GLCD_SendData(font[ch - 32][i]);
}
}

void GLCD_Clear() {
for (int i = 0; i < 8; i++) {
GLCD_SetPage(i);
GLCD_SetColumn(0);
for (int j = 0; j < 128; j++) {
GLCD_SendData(0);
}
}
}

void GLCD_WriteString(uint8_t row, uint8_t col, char* str) {
GLCD_SetPage(row);
GLCD_SetColumn(col);
while (*str) {
GLCD_WriteChar(*str++);
GLCD_SetColumn(GLCD_Column + 5);
}
}

int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();

GLCD_Init();
GLCD_Clear();
GLCD_WriteString(0, 0, "Hello, world!");

while (1) {
}
}

In this example code, the GLCD initialization sequence and functions for setting the page, column, and writing characters to the screen are implemented. The GLCD_Clear() and GLCD_WriteString() functions are also included to clear the screen and write a string of characters to the screen, respectively.

With this code, the GLCD 128×64 ST7920 can be easily interfaced with an STM32 microcontroller and used to display text and graphics.

It is worth noting that this code is specific to the GLCD 128×64 ST7920 and may need to be modified if using a different type of GLCD or microcontroller.

Additionally, this example code assumes that the necessary header files and font data are included in the project. It also assumes that the necessary pin configurations are set up in the STM32’s CubeMX software or in the HAL initialization code.

Conclusion:

Overall, interfacing a GLCD 128×64 ST7920 with an STM32 microcontroller can be a useful way to display information and graphics in embedded systems applications. With the appropriate software and hardware setup, this process can be straightforward and relatively easy to implement.

Read more on:

For Professional Designs or Help:

Loading

MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

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