GD32L233C-START evaluation 0.96 OLED display (I2C bus) display code transplantation
[Copy link]
1. Introduction to 0.96 OLED display
The resolution of 0.96-inch OLED is 128*64; that is, the OLED display is 128 rows*64 columns, and uses the IIC interface for communication (default address 0x78). The display module is shown in the figure below. The 0.96'OLED (4Pin) module uses SSD1306 as the driver chip. The module has a voltage regulator chip, supports software simulation IIC communication and hardware IIC communication, automatic reset on power-on, low power consumption, self-luminous, and free viewing angle.
Interface definition
GND: power ground
VCC: power positive (3.3 ~ 5V)
SCL: OLED's D0 pin, the clock pin in IIC communication
SDA: OLED's D1 pin, the data pin in IIC communication
2. GD32L233C-START DEMO board and OLED hardware circuit connection
The GD32L233C-START DEMO board opens many GPIOs for testing. This code transplantation uses PA1 and PA2. The transplanted code is the underlying driver of the display code completed by GPIO hardware simulation of I2C timing.
Connect the module's VCC, GND, SCL, and SDA to 3.3V, GND, PA1, and PA2 on the DEMO board through cables to complete the hardware circuit connection.
3. Project code migration
First, download the official reference information provided by the OLED shop. If necessary, you can go to the link below to download the relevant information of Zhongjingyuan's OLED screen. The information is relatively complete. In addition to the documents, it also provides I2C interface and SPI interface reference codes on different platforms for us to transplant and use.
Link: https://pan.baidu.com/s/1gw5hNr5oGIVyxUZZb3189Q Extraction code: gl0z
Find 03-Program source code -> Zhongjingyuan IIC interface display routine -> STM32 routine -> 01-0.96OLED display STM32F103C8T6_IIC routine -> HARDWARE -> OLED, copy the OLED folder to your new project directory. In this folder, the relevant underlying interface and related display driver code of OLED are provided, and we can complete the OLED display transplantation work with appropriate modifications.
1) Create a new project and transplant the standard library files. You can directly transplant the project through the routines, or refer to other friends' posts to transplant the project code, and put the OLED folder in the HARDWARE folder under the project directory, and add oled.c to the project. Here it is placed in the HARDWARE project directory, and the files related to the peripheral hardware resources are placed under this file. The project directory is shown in the figure below:
Also note that you need to set the file path of oled.h, etc. Just select the corresponding folder as shown below:
2) Just modify the underlying GPIO related code, and the application layer related code does not need to be modified. Mainly modify the port definition in oled.h and the GPIO setting related code in the OLED initialization in oled.c, as follows
#ifndef __OLED_H
#define __OLED_H
#include "stdlib.h"
#include "gd32l23x.h"
#include "systick.h"
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
//-----------------OLED端口定义----------------
#define OLED_SCL_Clr() gpio_bit_reset(GPIOA,GPIO_PIN_1)//SCL
#define OLED_SCL_Set() gpio_bit_set(GPIOA,GPIO_PIN_1)
#define OLED_SDA_Clr() gpio_bit_reset(GPIOA,GPIO_PIN_2)//DIN
#define OLED_SDA_Set() gpio_bit_set(GPIOA,GPIO_PIN_2)
//剩余的无需修改
// .....
#endif
It is also necessary to modify the GPIO mode settings in the OLED initialization in oled.c as follows:
void OLED_Init(void)
{
/* 使能GPIOA时钟 */
rcu_periph_clock_enable(RCU_GPIOA);
/* 配置端口模式 */
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_1);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_2);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
//下面的剩余无需改动
// ....
}
3) Then write the corresponding test code in main.c to complete the relevant tests. In the transplanted oled code, a series of application layer display driver function interface codes are provided for users to call to complete the display functions of various characters, Chinese characters, numbers, pictures, etc., as follows
void OLED_ClearPoint(u8 x,u8 y);
void OLED_ColorTurn(u8 i);
void OLED_DisplayTurn(u8 i);
void OLED_DisPlay_On(void);
void OLED_DisPlay_Off(void);
void OLED_Refresh(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_DrawLine(u8 x1,u8 y1,u8 x2,u8 y2,u8 mode);
void OLED_DrawCircle(u8 x,u8 y,u8 r);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1,u8 mode);
void OLED_ShowChar6x8(u8 x,u8 y,u8 chr,u8 mode);
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 size1,u8 mode);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1,u8 mode);
void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1,u8 mode);
void OLED_ScrollDisplay(u8 num,u8 space,u8 mode);
void OLED_ShowPicture(u8 x,u8 y,u8 sizex,u8 sizey,u8 BMP[],u8 mode);
void OLED_Init(void);
Note that you need to create your own Chinese character library or picture library file according to your needs, such as displaying Chinese characters or pictures. The document in the above link also provides instructions for generating the corresponding character library, which is convenient. For example, if this test code wants to display the Chinese characters "Beijing Zhaoyi Innovation", you need to generate the Chinese character library yourself and replace the corresponding Chinese character library code in the oledfont.h file.
Write the test code in main.c as follows, which can complete the relevant display test functions, which is very convenient.
#include "gd32l23x.h"
#include "systick.h"
#include "oled.h"
int main(void)
{
systick_config();
OLED_Init();
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
while(1)
{
OLED_ShowChinese(8*2,0,0,16,1);//北
OLED_ShowChinese(8*4,0,1,16,1);//京
OLED_ShowChinese(8*6,0,2,16,1);//兆
OLED_ShowChinese(8*8,0,3,16,1);//易
OLED_ShowChinese(8*10,0,4,16,1);//创
OLED_ShowChinese(8*12,0,5,16,1);//新
OLED_ShowString(16,16,"EEWORLD & GD ",16,1);
OLED_ShowString(8,32,"GD32L233C-START",16,1);
OLED_ShowString(20,48,"2022/02/03",16,1);
OLED_Refresh();
}
}
4) Compile, download, and display the results as follows:
5) Summary
The code transplantation of OLED display is not complicated. You only need to transplant the underlying driver code according to the modular thinking and modify the configuration of the relevant hardware related to the interface to complete the transplantation of the corresponding functional code. Code transplantation is a skill that we must master in embedded development. With the transplantation skills, the switching between different platforms will be more comfortable and the development efficiency will be appropriately improved.
|