在【NUCLEO-U545RE-Q】上移植u8g2。
一、下载u8g2
官网下载地址:https://github.com/olikraus/u8g2/tree/master
二、复制文件
复制csrc文件到项目工程目录
三、添加文件到工程
将csrc文件下所有文件添加到工程
四、配置外设
项目中用到I2C和定时器1
I2C在https://shequ.stmicroelectronics.cn/thread-641714-1-1.html中已经配置,这里只配置定时器
五、代码
5.1、修改u8g2_d_setup.c
只保留下面文件
5.2、stm32_u8g2.c
- <font size="3">#include "main.h"
- #include "stm32_u8g2.h"
- #include "/tim/tim.h"
- extern I2C_HandleTypeDef hi2c1;
- 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 seconds
- for (uint16_t n = 0; n < 320; n++)
- {
- __NOP();
- }
- break;
- case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
- HAL_Delay(1);
- break;
- case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
- Tims_delay_us(5);
- break; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
- case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
- break; // arg_int=1: Input dir with pullup high for I2C clock pin
- case U8X8_MSG_GPIO_I2C_DATA: // arg_int=0: Output low at I2C data pin
- break; // arg_int=1: Input dir with pullup high for I2C data pin
- case 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 value
- break;
- }
- return 1;
- }
- void u8g2Init(u8g2_t *u8g2)
- {
- u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay);
- 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);
- }
- #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);
- }
- void testShowFont(u8g2_t *u8g2)
- {
- int t = 1000;
- char testStr[14] = "STM32U545";
- 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;
- int sty = 16;
- int 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;
- int ry = 22;
- 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, 0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06, 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);
- }
- </font>
复制代码
5.3、stm32_u8g2.h
- <font size="3">#ifndef __STM32_U8G2_H
- #define __STM32_U8G2_H
- #include "main.h"
- #include "u8g2.h"
- #define u8 unsigned char
- #define MAX_LEN 128
- #define OLED_ADDRESS 0x78
- #define OLED_CMD 0x00
- #define OLED_DATA 0x40
-
- 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);
- 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);
- #endif</font>
复制代码
5.4、tim.c
- <font size="3">#include "main.h"
- #include "tim/tim.h"
- extern TIM_HandleTypeDef htim1;
- void Tims_delay_us(uint32_t nus)
- {
- uint16_t differ = 0xffff-nus-5;
- __HAL_TIM_SetCounter(&htim1,differ);
- HAL_TIM_Base_Start(&htim1);
- while( differ<0xffff-5)
- {
- differ = __HAL_TIM_GetCounter(&htim1);
- };
- //?????
- HAL_TIM_Base_Stop(&htim1);
- }</font>
复制代码
5.5、main.c
- <font size="3">int main(void)
- {
- /* USER CODE BEGIN 1 */
- u8g2_t u8g2;
- /* 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();
- /* Configure the System Power */
- SystemPower_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_ADC1_Init();
- MX_ICACHE_Init();
- MX_USART1_UART_Init();
- MX_USB_DRD_FS_HCD_Init();
- MX_I2C1_Init();
- MX_TIM1_Init();
- /* USER CODE BEGIN 2 */
-
- u8g2Init(&u8g2);
-
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- //HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
- //HAL_Delay(200);
- u8g2_FirstPage(&u8g2);
- do
- {
- draw(&u8g2);
- u8g2DrawTest(&u8g2);
- } while (u8g2_NextPage(&u8g2));
- }
- /* USER CODE END 3 */
- }</font>
复制代码
六、运行结果
|
这个不错,可以参考一下
效果不错,赞一个