STM32使用OLED移植U8g2库

STM32CubeMX配置

硬件

  • STM32F103C8T6
  • I2C通信OLED 128*64

软件

  • System Core->RCC->HSE->Crystal/Ceramic Resonator

  • System Core->SYS->Debug->Serial Wire

  • Connectivity->I2C1->I2C->I2C I2C Speed Mode 为 Fast Mode

OLED使用I2C通信,不使用GPIO模拟I2C了

  • Timers->TIM3用于延时用

  • 设置时钟树为72MHz

  • Generate Code

U8g2库移植

下载

U8g2Github地址: https://github.com/olikraus/u8g2

克隆到本地

git clone https://github.com/olikraus/u8g2.git

主要用到U8g2库的csrc文件夹

删除多余驱动文件

本次使用的oled为I2C通信,屏幕分辨率为128x64,ssd1306驱动芯片,所以保留u8x8_d_ssd1306_128x64_noname.c文件,而对于其他u8x8_d_xxx_xxx.c的文件删除,注意xxx分别为驱动型号和分辨率!!!

最后保留的文件

精简U8g2程序代码

u8g2_d_setup.c

删除多余函数只保留void u8g2_Setup_ssd1306_i2c_128x64_noname_f函数主体

void u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)
{uint8_t tile_buf_height;uint8_t *buf;u8g2_SetupDisplay(u8g2, u8x8_d_ssd1306_128x64_noname, u8x8_cad_ssd13xx_fast_i2c, byte_cb, gpio_and_delay_cb);buf = u8g2_m_16_8_f(&tile_buf_height);u8g2_SetupBuffer(u8g2, buf, tile_buf_height, u8g2_ll_hvline_vertical_top_lsb, rotation);
}

u8g2_d_memory.c

删除多余函数只保留uint8_t *u8g2_m_16_8_f(uint8_t *page_cnt)函数主体

uint8_t *u8g2_m_16_8_f(uint8_t *page_cnt)
{
#ifdef U8G2_USE_DYNAMIC_ALLOC*page_cnt = 8;return 0;
#elsestatic uint8_t buf[1024];*page_cnt = 8;return buf;
#endif
}

Clion移植

Core文件夹下新建U8g2/IncU8g2/Src文件夹分别储存头文件和源文件

重构CMake项目

在CMakeList.txt包含头文件

include_directories(Core/Inc Core/U8g2/Inc....)

编写代码

定时延时微秒函数

void Tims_delay_us(uint32_t nus)
{uint16_t  differ = 0xffff-nus-5;//设置定时器2的技术初始值__HAL_TIM_SetCounter(&htim3,differ);c//开启定时器HAL_TIM_Base_Start(&htim3);while( differ<0xffff-5){differ = __HAL_TIM_GetCounter(&htim3);};//关闭定时器HAL_TIM_Base_Stop(&htim3);
}

然后在tim.h中声明

stm32_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

注意I2C定义变量的修改!!!

#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_I2C1_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(&hi2c1, 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 KHzTims_delay_us(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文件

新建test.h

#ifndef __TEST_H
#define __TEST_H#include "main.h"
#include "u8g2.h"
#include "stdio.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 testDrawXBM(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);}
}// width: 128, height: 48
const unsigned char bilibili[] U8X8_PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0xe0, 0x03, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xf0, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0x01, 0xfc, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x03, 0xfc, 0x00, 0x00, 0x3c, 0xc0, 0x0f, 0x00, 0x80, 0x03, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0x07, 0xfc, 0x00, 0x00, 0x3c, 0xc0, 0x0f, 0x00, 0xc0, 0x07, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0x00, 0x00, 0x3c, 0x80, 0x0f, 0x00, 0xc0, 0x07, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x80, 0x0f, 0xf8, 0x00, 0x00, 0x3c, 0x80, 0x0f, 0x00, 0x80, 0x07, 0x00, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x78, 0x80, 0x0f, 0x00, 0x80, 0x07, 0x00, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x78, 0x80, 0x0f, 0x00, 0x80, 0x07, 0x00, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x80, 0x79, 0x80, 0x0f, 0x00, 0x98, 0x07, 0x00, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0xe0, 0x79, 0x9f, 0x0f, 0x00, 0xbe, 0xe7, 0x01, 0xc0, 0x07, 0x10, 0x40, 0x00, 0x1f, 0xf8, 0x00, 0xe0, 0x7b, 0x1f, 0x0f, 0x00, 0xbe, 0xe7, 0x01, 0xc0, 0x87, 0x1f, 0xe0, 0x0f, 0x1f, 0xf8, 0x00, 0xe0, 0x7b, 0x1e, 0x0f, 0x00, 0x3e, 0xe7, 0x01, 0xc0, 0xe7, 0x3f, 0xe0, 0x3f, 0x1f, 0xf0, 0x00, 0xe0, 0x7b, 0x1e, 0x0f, 0x00, 0x3e, 0xe7, 0x01, 0xc0, 0xe7, 0x3f, 0xe0, 0x3f, 0x1f, 0xf0, 0x00, 0x60, 0x71, 0x1e, 0x0f, 0x00, 0x34, 0xe7, 0x01, 0xc0, 0xe7, 0x07, 0x00, 0x3f, 0x1f, 0xf0, 0x00, 0x00, 0x70, 0x00, 0x1f, 0x00, 0x00, 0x07, 0x00, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0xc0, 0x73, 0x1e, 0x1f, 0x00, 0x3c, 0xc7, 0x01, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0xc0, 0x73, 0x1e, 0x1f, 0x00, 0x7c, 0xe7, 0x01, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0xc0, 0x73, 0x1e, 0x1f, 0x00, 0x7c, 0xef, 0x01, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x01, 0xc0, 0x77, 0x1e, 0x1e, 0x00, 0x7c, 0xef, 0x01, 0xc0, 0x07, 0x00, 0x03, 0x00, 0x1f, 0xf0, 0xff, 0xc1, 0xf7, 0x1e, 0xfe, 0x1f, 0x78, 0xef, 0x01, 0xc0, 0x07, 0x70, 0x37, 0x00, 0x1f, 0xe0, 0xff, 0x87, 0xf7, 0x1e, 0xfe, 0xff, 0x78, 0xee, 0x01, 0xc0, 0x07, 0xe0, 0x3f, 0x00, 0x1f, 0xe0, 0xff, 0x9f, 0xf7, 0x1e, 0xfe, 0xff, 0x79, 0xce, 0x01, 0xc0, 0x07, 0xc0, 0x18, 0x00, 0x1f, 0xe0, 0xff, 0xbf, 0xe7, 0x1e, 0xfe, 0xff, 0x7b, 0xce, 0x01, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0xc7, 0xbf, 0xe7, 0x1e, 0xfe, 0xf8, 0x77, 0xce, 0x01, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x0f, 0x3f, 0xe7, 0x1c, 0xfe, 0xf0, 0x77, 0xce, 0x03, 0xc0, 0x07, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0xcf, 0x3f, 0xe7, 0x1c, 0xfe, 0xf8, 0xf3, 0xce, 0x03, 0xc0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xe0, 0xef, 0x1f, 0xe7, 0x1c, 0xfe, 0xfe, 0xf1, 0xce, 0x03, 0x80, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xe0, 0xff, 0x0f, 0xcf, 0x1c, 0xfc, 0xff, 0xf0, 0xc0, 0x03, 0x00, 0xff, 0xff, 0xff, 0xff, 0x07, 0xe0, 0xff, 0x03, 0x06, 0x1c, 0xfc, 0x7f, 0x60, 0xc0, 0x01, 0x00, 0xfe, 0xff, 0xff, 0xff, 0x03, 0xe0, 0xff, 0x00, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// width: 128, height: 48
const unsigned char three_support[] U8X8_PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0x00, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0x80, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x03, 0x00, 0x00, 0x80, 0x0f, 0xf0, 0x01, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x00, 0xc0, 0xfd, 0xff, 0x00, 0x00, 0xc0, 0x7f, 0xfe, 0x01, 0x00, 0x00, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x01, 0x00, 0xc0, 0x1f, 0xf8, 0x03, 0x00, 0x00, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x01, 0x00, 0xc0, 0x0f, 0xf0, 0x03, 0x00, 0x00, 0xfe, 0xff, 0x07, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x01, 0x00, 0xc0, 0x67, 0xe6, 0x03, 0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x01, 0x00, 0xc0, 0x67, 0xe6, 0x03, 0x00, 0x00, 0xf8, 0xff, 0x01, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x00, 0x00, 0xc0, 0x67, 0xe6, 0x03, 0x00, 0x00, 0xf0, 0x7f, 0x00, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x00, 0x00, 0xc0, 0x67, 0xee, 0x03, 0x00, 0x00, 0xe0, 0x7f, 0x00, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x00, 0x00, 0x80, 0x7f, 0xfe, 0x01, 0x00, 0x00, 0xe0, 0xff, 0x00, 0x00, 0x00, 0xe0, 0xfd, 0xff, 0x00, 0x00, 0x80, 0x7f, 0xfe, 0x01, 0x00, 0x00, 0xf0, 0xff, 0x00, 0x00, 0x00, 0xe0, 0xfd, 0x7f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x00, 0x00, 0x00, 0xe0, 0xfd, 0x7f, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x00, 0xf8, 0xf9, 0x01, 0x00, 0x00, 0xe0, 0xfd, 0x7f, 0x00, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0xf8, 0xf0, 0x00, 0x00, 0x00, 0xe0, 0xfd, 0x1f, 0x00, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0x00, 0x30, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };void testDrawXBM(u8g2_t *u8g2)
{int t = 1000;u8g2_ClearBuffer(u8g2);u8g2_DrawStr(u8g2,0, 14, "Draw OLED");u8g2_DrawXBM(u8g2,0, 16, 128, 48, bilibili);SEND_BUFFER_DISPLAY_MS(u8g2,t);u8g2_ClearBuffer(u8g2);u8g2_DrawStr(u8g2,0, 14, "bilibili");u8g2_DrawXBM(u8g2,0, 16, 128, 48, three_support);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);testDrawXBM(u8g2);}

主函数

main.c

#include "main.h"
#include "i2c.h"
#include "tim.h"
#include "gpio.h"#include "oled.h"
#include "u8g2.h"
#include "stdio.h"
#include "stm32_u8g2.h"
#include "test.h"void SystemClock_Config(void);int main(void)
{HAL_Init();SystemClock_Config();MX_GPIO_Init();MX_I2C1_Init();MX_TIM3_Init();u8g2_t u8g2;u8g2Init(&u8g2);while (1){u8g2_FirstPage(&u8g2);do{draw(&u8g2);u8g2DrawTest(&u8g2);} while (u8g2_NextPage(&u8g2));
}

运行效果

STM32HAL库移植U8g2

参考文章

STM32使用OLED移植U8g2库相关推荐

  1. [STM32] STM32 移植 U8g2库

    文章目录 前言 1. 准备 2. 移植步骤 2.1 建立有效的通信 2.2 导入U8g2库 2.3 用例测试 总结 前言 开发平台:Keil + 标准库 硬件平台:STM32F103RCT6 显示器件 ...

  2. STM32 HAL移植u8g2库(I2C)

    通过STM32CubeMX配置I2C. 复制相关文件到工程文件夹 保留对应的显示屏型号,如SSD1306,就保留u8x8_d_ssd1306_128x64_noname.c,其它u8x8_d_XXXX ...

  3. 基于STM32移植U8g2图形库——OLED显示(HAL库)

    前言:本文主要内容为将优秀的图形库U8g2移植到STM32单片机上,用于OLED显示精美UI.其实,目前GitHub上有需要优秀的开源GUI库,但是大部分的GUI解决方案并不适合0.96 OLED(1 ...

  4. U8G2库移植到STM32平台上

    U8G2库简介 U8g2是嵌入式设备的单色图形库,一句话简单明了.主要应用于嵌入式设备,包括我们常见的单片机: 建议先看这篇博客的介绍,写的比较好u8g2库的使用 这篇博客介绍了对于u8g2的使用,但 ...

  5. cebemax hal库 stm32 OLED移植 解析

    cebemax hal库 stm32 OLED移植 序言: 这是一篇对于作者对于将OLED的驱动代码移植成hal库的文章,会有完整的过程,后面自然也会有完整的代码,希望能够帮到大家,同时也是做一个记录 ...

  6. u8g2库stm32移植记录(硬件IIC)

    这是用stm32cubemx,hal库,硬件IIC的方式移植u8g2的教程: 0.移植其实非常简单,文中代码较多只是因为我把官方的模板放上来了,实际上要写的代码只有三部分,代码量非常小的!如果不想看那 ...

  7. 0.96寸OLED显示屏标准库移植HAL库(模拟IIC) - 基于STM32

    ** 0.96寸OLED显示屏标准库移植HAL库,使用模拟IIC ** 由于项目的需要使用OLED屏显示,并且现有的项目程序是基于HAL库编写的,而手头能找到的程序是标准库的驱动程序,大概看了一下代码 ...

  8. stm32移植U8G2图像库指南

    stm32移植U8G2图像库指南 U8G2图形库介绍 移植准备工作 开始移植文件 修改keil配置 修改U8G2源码 u8g2_d_setup.c修改 u8g2_d_memory.c修改 注意 U8G ...

  9. arduino使用oled代码_Arduino提高篇04—U8g2库驱动OLED

    OLED显示屏的驱动库非常多,本篇介绍强大的U8g2库,通过使用U8g2库来驱动OLED屏. 1. U8g2库介绍 U8g2库是嵌入式设备单色图形库,支持非常多的嵌入式设备平台,如单片机,STM32, ...

最新文章

  1. 互联网1分钟 |1217
  2. Java异常中受检异常非受检异常与RuntimeException异常关系
  3. [系统安全] 八.Windows漏洞利用之CVE-2019-0708复现及防御详解
  4. 我的醉驾拘留15日记----第二日凌晨 午夜惊梦
  5. 互联网java常用框架_来,带你鸟瞰 Java 中4款常用的并发框架!
  6. 在 vscode 中使用 Git :拉取、提交、克隆
  7. 游族内部信:年终奖如期发放 继续招聘全球化游戏人才
  8. Git push - fatal: write error: Bad file descriptor
  9. 【STM32学习笔记】(10)——蜂鸣器实验详解
  10. 项目启动管理、需求分析、可行性方案论证  IT项目管理
  11. java中wint是什么意思,Java线程的同步演示代码,java线程演示,package Wint
  12. php 正则车架号,js 正则校验车架号VIN
  13. Word 从任意页开始设置页码
  14. 为什么要读“无用”的古文
  15. python 方差_python 方差_python 方差齐性检验_方差 python - 云+社区 - 腾讯云
  16. 从再生龙(clonezilla)镜像直接提取文件
  17. F28335舵机控制器(3)——第三版PCB
  18. 腾讯地图Api 实现拾取坐标功能示例
  19. ubuntu 使用LVM修改分区大小后开机报错的解决办法
  20. 小程序里面关注公众号

热门文章

  1. 使用Python实现量化交易机器人定时启动或停止小工具
  2. TS与JS区别、优势
  3. 类型转换、强制类型转换
  4. 来11:1 信就是所望之事的实底,是未见之事的确据。
  5. 数仓工具—Hive实战之滑动/滚动时间窗口计算(19)
  6. 计算机科学与技术——软科中国大学专业排名
  7. 香饽饽:腾讯强推的Redis天花板笔记,帮助初学者快速入门和提高(核心笔记+面试高频解析)
  8. C. Equalize(贪心)
  9. python安装报_ssl问题
  10. 水卡解码 ,pn532