本文主要是介绍STM32F103C8T6移植U8g2图形库及基于I2C协议的OLED显示(HAL库方式)【U8g2】【STM32开发板】【STM32CubeMX】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
STM32F103C8T6移植U8g2图形库及基于I2C协议的OLED显示(HAL库方式)【U8g2】【STM32开发板】【STM32CubeMX】
实验说明
利用STM32F103的GPIO管脚、VCC和GND连接OLED屏的I2C接口,采用CubeMX设计一个HAL库程序框架,然后下载U8g2源码,针对STM32F103和0.96寸的I2C接口OLED屏,进行代码裁剪,然后移植到HAL程序程序中,完成OLED的显示。
关于U8g2
U8g2是什么
U8g2是GitHub上一款十分优秀的开源图形库(GUI库),其本质是嵌入式设备的单色图形库。在 Github 上超过3.2K Star,2.6K Commit。其开发语言90%为C语言,且代码简洁干练便于移植与后期修改。
U8g2支持的显示控制器
U8g2支持单色OLED和LCD,包括以下控制器:SSD1305, SSD1306, SSD1309, SSD1312, SSD1316, SSD1320, SSD1322, SSD1325, SSD1327, SSD1329, SSD1606, SSD1607, SH1106, SH1107, SH1108, SH1122, T6963, RA8835, LC7981, PCD8544, PCF8812, HX1230, UC1601, UC1604, UC1608, UC1610, UC1611, UC1617, UC1638, UC1701, ST7511, ST7528, ST7565, ST7567, ST7571, ST7586, ST7588, ST75256, ST75320, NT7534, ST7920, IST3020, IST7920, LD7032, KS0108, KS0713, HD44102, T7932, SED1520, SBN1661, IL3820, MAX7219。(可以看出适配的驱动IC是非常多的,考虑到后续U8g2图形库继续维护升级可能会支持更多控制器)
可以说,基本上主流的显示控制器都支持,比如我们常见的SSD1306等,读者在使用该库之前请查阅自己的OLED显示控制器是否处于支持列表中。
U8g2的优势
-
U8g2库平台支持性好,基本上支持绝大部分Arduino与STM32开发板,也包含物联网比较常用的esp8266;
-
U8g2库显示控制器支持性好,基本上市面上的OLED都完美支持;
-
U8g2库API函数众多,特别支持了中文,支持了不同字体,这是一个对于开发者俩说不小的福利;
-
U8g2库移植简单,容易使用。
STM32CubeMX配置
-
RCC配置:设置高速时钟 (HSE) 为晶振 (Crystal Resonator) 。
-
SYS配置:设置Debug为串行线 (Serial Wire) 。
-
I2C2配置:设置I2C为I2C。
-
TIM1配置:设置时钟源 (Clock Source) 为内部时钟 (Internal Clock) ,并在Parameter Settings中设置预分频系数 (Prescaler) 为71。计数周期 (Counter Period) 保持默认的65535不变。
-
时钟配置 (Clock Configuration) :
配置以上参数后,生成Keil工程即可。
U8g2移植
U8g2源码下载
U8g2下载地址:https://github.com/olikraus/u8g2
进入源码地址后下载整个U8g2图形库的压缩包。
精简U8g2源码
U8g2支持多种显示驱动的屏幕,因为源码中包含了各个驱动对应的文件,为减小整个工程的代码体积,在移植U8g2时,可以删除一些无用的文件。
打开csrc文件夹,找到所有u8x8_d_开头的c文件。本次实验使用ssd1306驱动的128×64分辨率的OLED,因此需要u8x8_d_ssd1306_128x64_noname.c这个文件,其它屏幕驱动和分辨率的文件可以删除。
精简u8g2_d_setup:本次实验用的OLED是I2C接口,因此保留u8g2_Setup_ssd1306_i2c_128x64_noname_f这个函数即可,其它函数可以删除或注释。
精简u8g2_d_memory:u8g2_Setup_ssd1306_i2c_128x64_noname_f函数中,只调用了u8g2_m_16_8_f这个函数,所以留下这个函数,其它的函数一定要删掉或注释掉,否则编译时很可能会提示内存不足!
将精简后的U8g2源码添加至Keil
在该工程的文件夹下新建文件夹U8g2,并将精简后csrc文件夹下的所有文件复制到此文件夹。同时新建文件夹Hardware,以便存放后面编写的移植函数和测试函数。
在管理工程项目界面,添加组U8g2,并将U8g2文件夹下的所有c文件添加进U8g2组中。同时添加组Hardware,以便将后面编写的移植函数和测试函数添加进此组中。
在选项→C/C++界面添加Hardware, U8g2两文件夹作为头文件路径。
编写程序
移植函数
stm32_u8g2.h:
#ifndef __STM32_U8G2_H
#define __STM32_U8G2_H/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "u8g2.h"
/* USER CODE BEGIN Includes *//* USER CODE END Includes *//* USER CODE BEGIN Private defines *//* USER CODE END Private defines */
#define u8 unsigned char // ?unsigned char ????
#define MAX_LEN 128 //
#define OLED_ADDRESS 0x78 // oled
#define OLED_CMD 0x00 //
#define OLED_DATA 0x40 // /* USER CODE BEGIN Prototypes */
uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
void u8g2Init(u8g2_t *u8g2);
void draw(u8g2_t *u8g2);
void testDrawPixelToFillScreen(u8g2_t *u8g2);#endif
stm32_u8g2.c:
#include "stm32_u8g2.h"
#include "tim.h"
#include "i2c.h"uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{/* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */static uint8_t buffer[128];static uint8_t buf_idx;uint8_t *data;switch (msg){case U8X8_MSG_BYTE_INIT:{/* add your custom code to init i2c subsystem */MX_I2C2_Init(); //I2C初始化}break;case U8X8_MSG_BYTE_START_TRANSFER:{buf_idx = 0;}break;case U8X8_MSG_BYTE_SEND:{data = (uint8_t *)arg_ptr;while (arg_int > 0){buffer[buf_idx++] = *data;data++;arg_int--;}}break;case U8X8_MSG_BYTE_END_TRANSFER:{if (HAL_I2C_Master_Transmit(&hi2c2, OLED_ADDRESS, buffer, buf_idx, 1000) != HAL_OK)return 0;}break;case U8X8_MSG_BYTE_SET_DC:break;default:return 0;}return 1;
}uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{switch (msg){case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds__NOP();break;case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro secondsfor (uint16_t n = 0; n < 320; n++){__NOP();}break;case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli secondHAL_Delay(1);break;case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHzHAL_Delay(5);break; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25uscase U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pinbreak; // arg_int=1: Input dir with pullup high for I2C clock pincase U8X8_MSG_GPIO_I2C_DATA: // arg_int=0: Output low at I2C data pinbreak; // arg_int=1: Input dir with pullup high for I2C data pincase U8X8_MSG_GPIO_MENU_SELECT:u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);break;case U8X8_MSG_GPIO_MENU_NEXT:u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);break;case U8X8_MSG_GPIO_MENU_PREV:u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);break;case U8X8_MSG_GPIO_MENU_HOME:u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);break;default:u8x8_SetGPIOResult(u8x8, 1); // default return valuebreak;}return 1;
}//U8g2的初始化,需要调用下面这个u8g2_Setup_ssd1306_128x64_noname_f函数,该函数的4个参数含义:
//u8g2:传入的U8g2结构体
//U8G2_R0:默认使用U8G2_R0即可(用于配置屏幕是否要旋转)
//u8x8_byte_sw_i2c:使用软件IIC驱动,该函数由U8g2源码提供
//u8x8_gpio_and_delay:就是上面我们写的配置函数void u8g2Init(u8g2_t *u8g2)
{u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay); // 初始化u8g2 结构体u8g2_InitDisplay(u8g2); // u8g2_SetPowerSave(u8g2, 0); // u8g2_ClearBuffer(u8g2);
}void draw(u8g2_t *u8g2)
{u8g2_ClearBuffer(u8g2); u8g2_SetFontMode(u8g2, 1); /*字体模式选择*/u8g2_SetFontDirection(u8g2, 0); /*字体方向选择*/u8g2_SetFont(u8g2, u8g2_font_inb24_mf); /*字库选择*/u8g2_DrawStr(u8g2, 0, 20, "U");u8g2_SetFontDirection(u8g2, 1);u8g2_SetFont(u8g2, u8g2_font_inb30_mn);u8g2_DrawStr(u8g2, 21,8,"8");u8g2_SetFontDirection(u8g2, 0);u8g2_SetFont(u8g2, u8g2_font_inb24_mf);u8g2_DrawStr(u8g2, 51,30,"g");u8g2_DrawStr(u8g2, 67,30,"\xb2");u8g2_DrawHLine(u8g2, 2, 35, 47);u8g2_DrawHLine(u8g2, 3, 36, 47);u8g2_DrawVLine(u8g2, 45, 32, 12);u8g2_DrawVLine(u8g2, 46, 33, 12);u8g2_SetFont(u8g2, u8g2_font_4x6_tr);u8g2_DrawStr(u8g2, 1,54,"github.com/olikraus/u8g2");u8g2_SendBuffer(u8g2);HAL_Delay(1000);
}//画点填充
void testDrawPixelToFillScreen(u8g2_t *u8g2)
{int t = 1000;u8g2_ClearBuffer(u8g2);for (int j = 0; j < 64; j++){for (int i = 0; i < 128; i++){u8g2_DrawPixel(u8g2,i, j);}}HAL_Delay(1000);
}
测试函数
本实验的测试函数中包含了进度条显示、字体显示、图形显示等。
test.h:
#ifndef __TEST_H
#define __TEST_H#include "main.h"
#include "u8g2.h"void testDrawProcess(u8g2_t *u8g2);
void testShowFont(u8g2_t *u8g2);
void testDrawFrame(u8g2_t *u8g2);
void testDrawRBox(u8g2_t *u8g2);
void testDrawCircle(u8g2_t *u8g2);
void testDrawFilledEllipse(u8g2_t *u8g2);
void testDrawMulti(u8g2_t *u8g2);
void testDrawStrings(u8g2_t *u8g2);void u8g2DrawTest(u8g2_t *u8g2);#endif
test.c:
#include "test.h"//---------------U8g2测试函数#define SEND_BUFFER_DISPLAY_MS(u8g2, ms)\do {\u8g2_SendBuffer(u8g2); \HAL_Delay(ms);\}while(0);//进度条显示
void testDrawProcess(u8g2_t *u8g2)
{for(int i=10;i<=80;i=i+2){u8g2_ClearBuffer(u8g2); char buff[20];sprintf(buff,"%d%%",(int)(i/80.0*100));u8g2_SetFont(u8g2,u8g2_font_ncenB12_tf);u8g2_DrawStr(u8g2,16,32,"STM32 U8g2");//字符显示u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);u8g2_DrawStr(u8g2,100,49,buff);//当前进度显示u8g2_DrawRBox(u8g2,16,40,i,10,4);//圆角填充框矩形框u8g2_DrawRFrame(u8g2,16,40,80,10,4);//圆角矩形u8g2_SendBuffer(u8g2);}HAL_Delay(500);
}//字体测试 数字英文可选用 u8g2_font_ncenB..(粗) 系列字体
//u8g2_font_unifont_t_symbols/u8g2_font_unifont_h_symbols(细 圆润)
void testShowFont(u8g2_t *u8g2)
{int t = 1000;char testStr[14] = "STM32F103C8T6";u8g2_ClearBuffer(u8g2);u8g2_SetFont(u8g2,u8g2_font_u8glib_4_tf);u8g2_DrawStr(u8g2,0,5,testStr);SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);u8g2_DrawStr(u8g2,0,30,testStr);SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_SetFont(u8g2,u8g2_font_ncenB10_tr);u8g2_DrawStr(u8g2,0,60,testStr);SEND_BUFFER_DISPLAY_MS(u8g2,t);
}//画空心矩形
void testDrawFrame(u8g2_t *u8g2)
{int t = 1000;int x = 16;int y = 32;int w = 50;int h = 20;u8g2_ClearBuffer(u8g2);u8g2_DrawStr(u8g2,0, 15, "DrawFrame");u8g2_DrawFrame(u8g2, x, y, w, h);SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawFrame(u8g2, x+w+5, y-10, w-20, h+20);SEND_BUFFER_DISPLAY_MS(u8g2,t);
}//画实心圆角矩形
void testDrawRBox(u8g2_t *u8g2)
{int t = 1000;int x = 16;int y = 32;int w = 50;int h = 20;int r = 3;u8g2_ClearBuffer(u8g2);u8g2_DrawStr(u8g2,0, 15, "DrawRBox");u8g2_DrawRBox(u8g2, x, y, w, h, r);SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawRBox(u8g2, x+w+5, y-10, w-20, h+20, r);SEND_BUFFER_DISPLAY_MS(u8g2,t);
}//画空心圆
void testDrawCircle(u8g2_t *u8g2)
{int t = 600;int stx = 0; //画图起始xint sty = 16; //画图起始yint with = 16;//一个图块的间隔int r = 15; //圆的半径u8g2_ClearBuffer(u8g2);u8g2_DrawStr(u8g2, 0, 15, "DrawCircle");u8g2_DrawCircle(u8g2, stx, sty - 1 + with, r, U8G2_DRAW_UPPER_RIGHT); //右上SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawCircle(u8g2, stx + with, sty, r, U8G2_DRAW_LOWER_RIGHT); //右下SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawCircle(u8g2, stx - 1 + with * 3, sty - 1 + with, r, U8G2_DRAW_UPPER_LEFT); //左上SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawCircle(u8g2, stx - 1 + with * 4, sty, r, U8G2_DRAW_LOWER_LEFT); //左下SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawCircle(u8g2, stx - 1 + with * 2, sty - 1 + with * 2, r, U8G2_DRAW_ALL);//整个圆SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawCircle(u8g2, 32*3, 32, 31, U8G2_DRAW_ALL);//右侧整个圆SEND_BUFFER_DISPLAY_MS(u8g2,t);
}//画实心椭圆
void testDrawFilledEllipse(u8g2_t *u8g2)
{int t = 800;int with = 16;//一个图块的间隔int rx = 27; //椭圆x方向的半径int ry = 22; //椭圆y方向的半径u8g2_ClearBuffer(u8g2);u8g2_DrawStr(u8g2,0, 14, "DrawFilledEllipse");SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawFilledEllipse(u8g2, 0, with, rx, ry, U8G2_DRAW_LOWER_RIGHT);//右下SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with, rx, ry, U8G2_DRAW_LOWER_LEFT); //左下SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawFilledEllipse(u8g2, 0, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_RIGHT); //右上SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_LEFT); //左上SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_DrawFilledEllipse(u8g2, with * 6, with * 2.5, rx, ry, U8G2_DRAW_ALL);//整个椭圆SEND_BUFFER_DISPLAY_MS(u8g2,t);
}//环形测试
void testDrawMulti(u8g2_t *u8g2)
{u8g2_ClearBuffer(u8g2);for (int j = 0; j < 64; j+=16){for (int i = 0; i < 128; i+=16){u8g2_DrawPixel(u8g2, i, j);u8g2_SendBuffer(u8g2);}}//实心矩形逐渐变大u8g2_ClearBuffer(u8g2);for(int i=30; i>0; i-=2){u8g2_DrawBox(u8g2,i*2,i,128-i*4,64-2*i);u8g2_SendBuffer(u8g2);}//空心矩形逐渐变小u8g2_ClearBuffer(u8g2);for(int i=0; i<32; i+=2){u8g2_DrawFrame(u8g2,i*2,i,128-i*4,64-2*i);u8g2_SendBuffer(u8g2);}//实心圆角矩形逐渐变大u8g2_ClearBuffer(u8g2);for(int i=30; i>0; i-=2){u8g2_DrawRBox(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);u8g2_SendBuffer(u8g2);}//空心圆角矩形逐渐变小u8g2_ClearBuffer(u8g2);for(int i=0; i<32; i+=2){u8g2_DrawRFrame(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);u8g2_SendBuffer(u8g2);}//实心圆逐渐变大u8g2_ClearBuffer(u8g2);for(int i=2; i<64; i+=3){u8g2_DrawDisc(u8g2,64,32,i, U8G2_DRAW_ALL);u8g2_SendBuffer(u8g2);}//空心圆逐渐变小u8g2_ClearBuffer(u8g2);for(int i=64; i>0; i-=3){u8g2_DrawCircle(u8g2,64,32,i, U8G2_DRAW_ALL);u8g2_SendBuffer(u8g2);}//实心椭圆逐渐变大u8g2_ClearBuffer(u8g2);for(int i=2; i<32; i+=3){u8g2_DrawFilledEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);u8g2_SendBuffer(u8g2);}//空心椭圆逐渐变小u8g2_ClearBuffer(u8g2);for(int i=32; i>0; i-=3){u8g2_DrawEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);u8g2_SendBuffer(u8g2);}
}void testDrawStrings(u8g2_t *u8g2)
{int t=1000;u8g2_ClearBuffer(u8g2);u8g2_SetFont(u8g2,u8g2_font_ncenB08_tr);u8g2_DrawStr(u8g2,0,10,"632207060215");SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_SetFont(u8g2,u8g2_font_ncenB18_tr);u8g2_DrawStr(u8g2,0,40,"penCKG");SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_SetFont(u8g2,u8g2_font_ncenB10_tr);u8g2_DrawStr(u8g2,0,60,"Test Program");SEND_BUFFER_DISPLAY_MS(u8g2,t);}void u8g2DrawTest(u8g2_t *u8g2)
{testDrawProcess(u8g2);testDrawMulti(u8g2);//testDrawFrame(u8g2);//testDrawRBox(u8g2);//testDrawCircle(u8g2);//testDrawFilledEllipse(u8g2);testShowFont(u8g2);testDrawStrings(u8g2);
}
主函数
main.c:
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2024 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "tim.h"
#include "gpio.h"
#include "u8g2.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes *//* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_I2C2_Init();MX_TIM1_Init();/* USER CODE BEGIN 2 */u8g2_t u8g2;u8g2Init(&u8g2);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */u8g2_FirstPage(&u8g2);do{draw(&u8g2);u8g2DrawTest(&u8g2);} while (u8g2_NextPage(&u8g2));}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef USE_FULL_ASSERT
/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
STM32开发板实现(实验结果)
将STM32开发板插入面包板并用ST-link与电脑相接。将OLED屏的VCC接至3.3V端口,GND接至GND端口,SCL接至PB10端口,SDA接至PB11端口。
编译通过后将程序下载至开发板中,结果如下。
【U8g2】【STM32开发板】OLED运行结果
参考文章
基于STM32移植U8g2图形库——OLED显示(HAL库)_stm32 u8g2-CSDN博客
这篇关于STM32F103C8T6移植U8g2图形库及基于I2C协议的OLED显示(HAL库方式)【U8g2】【STM32开发板】【STM32CubeMX】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!