你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

ST NUCLEO-U575ZI HAL移植U8G2的测评

[复制链接]
STMCU小助手 发布时间:2023-1-10 15:51

原来在arduino上使用了u8g2.今天偿试用stm32cubeIde进行移植。

一、用stm32cubeIDE进行配置i2c:


164453psp1szp3sz2fm88v.png.thumb.png


  这次用到的为PB8-SCL、PB9-SDA,在管脚上配置好:

164756wg6ghdphmphhh3pl.png.thumb.png



保存生成工程文件。


二、下载u8g2

下载后解压,把csrc整个目录拷到IDE的目录下面。把工程添加到工程里面:

项目右键

--properties

----C/C++ General

------Paths and Symbols

--------Includes

----------Add

------------WorkSpaces

--------------选择自己在项目中新建的文件夹

大家记得把Release也要加一下,要不然编译时老是提示找不到头文件。

二、删除多除的以8x8-d的其他屏的驱动文件

我这里只留下 u8x8_d_ssd1306_128x64_noname.c


三、删除u8g2_d_setup.c文件里面其他不用的文件,我这里只留下了:

  1. /* u8g2_d_setup.c */
  2. /* generated code, codebuild, u8g2 project */

  3. #include "u8g2.h"


  4. /* ssd1306 f */
  5. 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)
  6. {
  7.   uint8_t tile_buf_height;
  8.   uint8_t *buf;
  9.   u8g2_SetupDisplay(u8g2, u8x8_d_ssd1306_128x64_noname, u8x8_cad_ssd13xx_fast_i2c, byte_cb, gpio_and_delay_cb);
  10.   buf = u8g2_m_16_8_f(&tile_buf_height);
  11.   u8g2_SetupBuffer(u8g2, buf, tile_buf_height, u8g2_ll_hvline_vertical_top_lsb, rotation);
  12. }

  13. /* end of generated code */
复制代码


四、新建oled.h、oled.c以及测试test.h、test.c

  1. /*
  2. * oled.h
  3. *
  4. *  Created on: Dec 20, 2022
  5. *      Author: liujianhua
  6. */

  7. #ifndef INC_OLED_H_
  8. #define INC_OLED_H_
  9. #ifdef __cplusplue
  10.         extern "C" {
  11. #endif
  12. #include "main.h"
  13. #include "u8g2.h"

  14. #define u8 unsigend char
  15. #define MAX_LEN 128
  16. #define OLED_ADDRES 0x78
  17. #define OLED_CMD 0x00
  18. #define OLED_DATA 0x40

  19. /* USER CODE BEGIN Prototypes */
  20.         uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  21.         uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  22.         void u8g2Init(u8g2_t *u8g2);
  23. #ifdef __cplusplue
  24.         }
  25. #endif

  26. #endif /* INC_OLED_H_ */
复制代码
  1. /*
  2. * oled.c
  3. *
  4. *  Created on: Dec 20, 2022
  5. *      Author: liujianhua
  6. */
  7. #include "oled.h"
  8. #include "main.h"
  9. #include "stm32u5xx_hal_i2c.h"

  10. extern I2C_HandleTypeDef hi2c1;
  11. uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  12. {
  13.     /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */
  14.     static uint8_t buffer[128];
  15.     static uint8_t buf_idx;
  16.     uint8_t *data;

  17.     switch (msg)
  18.     {
  19.     case U8X8_MSG_BYTE_INIT:
  20.     {
  21.         /* add your custom code to init i2c subsystem */
  22.         //MX_I2C1_Init(); //I2C初始化
  23.     }
  24.     break;

  25.     case U8X8_MSG_BYTE_START_TRANSFER:
  26.     {
  27.         buf_idx = 0;
  28.     }
  29.     break;

  30.     case U8X8_MSG_BYTE_SEND:
  31.     {
  32.         data = (uint8_t *)arg_ptr;

  33.         while (arg_int > 0)
  34.         {
  35.             buffer[buf_idx++] = *data;
  36.             data++;
  37.             arg_int--;
  38.         }
  39.     }
  40.     break;

  41.     case U8X8_MSG_BYTE_END_TRANSFER:
  42.     {
  43.         if (HAL_I2C_Master_Transmit(&hi2c1, (OLED_ADDRES), buffer, buf_idx, 1000) != HAL_OK)
  44.             return 0;
  45.     }
  46.     break;

  47.     case U8X8_MSG_BYTE_SET_DC:
  48.         break;

  49.     default:
  50.         return 0;
  51.     }

  52.     return 1;
  53. }

  54. void delay_us(uint32_t time)
  55. {
  56.     uint32_t i = 8 * time;
  57.     while (i--)
  58.         ;
  59. }

  60. uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  61. {
  62.     switch (msg)
  63.     {
  64.     case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  65.         __NOP();
  66.         break;
  67.     case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  68.         for (uint16_t n = 0; n < 320; n++)
  69.         {
  70.             __NOP();
  71.         }
  72.         break;
  73.     case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  74.         HAL_Delay(1);
  75.         break;
  76.     case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  77.         delay_us(5);
  78.         break;                    // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  79.     case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
  80.         break;                    // arg_int=1: Input dir with pullup high for I2C clock pin
  81.     case U8X8_MSG_GPIO_I2C_DATA:  // arg_int=0: Output low at I2C data pin
  82.         break;                    // arg_int=1: Input dir with pullup high for I2C data pin
  83.     case U8X8_MSG_GPIO_MENU_SELECT:
  84.         u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);
  85.         break;
  86.     case U8X8_MSG_GPIO_MENU_NEXT:
  87.         u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);
  88.         break;
  89.     case U8X8_MSG_GPIO_MENU_PREV:
  90.         u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);
  91.         break;
  92.     case U8X8_MSG_GPIO_MENU_HOME:
  93.         u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);
  94.         break;
  95.     default:
  96.         u8x8_SetGPIOResult(u8x8, 1); // default return value
  97.         break;
  98.     }
  99.     return 1;
  100. }
  101. void u8g2Init(u8g2_t *u8g2)
  102. {

  103.         u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay); // 初始化 u8g2 结构体(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay); // 初始化 u8g2 结构体
  104.         u8g2_InitDisplay(u8g2);                                                                       // 根据所选的芯片进行初始化工作,初始化完成后,显示器处于关闭状态
  105.         u8g2_SetPowerSave(u8g2, 0);                                                                   // 打开显示器
  106.         u8g2_ClearBuffer(u8g2);
  107. }




  108. /*
  109. * test.h
  110. *
  111. *  Created on: Dec 20, 2022
  112. *      Author: liujianhua
  113. */

  114. #ifndef INC_TEST_H_
  115. #define INC_TEST_H_


  116. #include "main.h"
  117. #include "u8g2.h"

  118. void testDrawProcess(u8g2_t *u8g2);
  119. void testShowFont(u8g2_t *u8g2);
  120. void testDrawFrame(u8g2_t *u8g2);
  121. void testDrawRBox(u8g2_t *u8g2);
  122. void testDrawCircle(u8g2_t *u8g2);
  123. void testDrawFilledEllipse(u8g2_t *u8g2);
  124. void testDrawMulti(u8g2_t *u8g2);
  125. void testDrawXBM(u8g2_t *u8g2);

  126. void u8g2DrawTest(u8g2_t *u8g2);


  127. #endif /* INC_TEST_H_ */
复制代码
  1. /*
  2. * test.c
  3. *
  4. *  Created on: Dec 20, 2022
  5. *      Author: liujianhua
  6. */

  7. #include "test.h"

  8. //---------------U8g2测试函数

  9. #define SEND_BUFFER_DISPLAY_MS(u8g2, ms)\
  10.   do {\
  11.     u8g2_SendBuffer(u8g2); \
  12.     HAL_Delay(ms);\
  13.   }while(0);


  14. //进度条显示
  15. void testDrawProcess(u8g2_t *u8g2)
  16. {
  17.         for(int i=10;i<=80;i=i+2)
  18.         {
  19.                 u8g2_ClearBuffer(u8g2);

  20.                 char buff[20];
  21.                 sprintf(buff,"%d%%",(int)(i/80.0*100));

  22.                 u8g2_SetFont(u8g2,u8g2_font_ncenB12_tf);
  23.                 u8g2_DrawStr(u8g2,16,32,"STM32 U8g2");//字符显示

  24.                 u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);
  25.                 u8g2_DrawStr(u8g2,100,49,buff);//当前进度显示

  26.                 u8g2_DrawRBox(u8g2,16,40,i,10,4);//圆角填充框矩形框
  27.                 u8g2_DrawRFrame(u8g2,16,40,80,10,4);//圆角矩形

  28.                 u8g2_SendBuffer(u8g2);
  29.         }
  30.         HAL_Delay(500);
  31. }


  32. //字体测试 数字英文可选用 u8g2_font_ncenB..(粗) 系列字体
  33. //u8g2_font_unifont_t_symbols/u8g2_font_unifont_h_symbols(细 圆润)
  34. void testShowFont(u8g2_t *u8g2)
  35. {
  36.         int t = 1000;
  37.         char testStr[14] = "STM32U575";

  38.         u8g2_ClearBuffer(u8g2);

  39.         u8g2_SetFont(u8g2,u8g2_font_u8glib_4_tf);
  40.         u8g2_DrawStr(u8g2,0,5,testStr);
  41.         SEND_BUFFER_DISPLAY_MS(u8g2,t);

  42.         u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);
  43.         u8g2_DrawStr(u8g2,0,30,testStr);
  44.         SEND_BUFFER_DISPLAY_MS(u8g2,t);

  45.     u8g2_SetFont(u8g2,u8g2_font_ncenB10_tr);
  46.         u8g2_DrawStr(u8g2,0,60,testStr);
  47.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  48. }

  49. //画空心矩形
  50. void testDrawFrame(u8g2_t *u8g2)
  51. {
  52.         int t = 1000;
  53.         int x = 16;
  54.         int y = 32;
  55.         int w = 50;
  56.         int h = 20;
  57.         u8g2_ClearBuffer(u8g2);
  58.         u8g2_DrawStr(u8g2,0, 15, "DrawFrame");

  59.         u8g2_DrawFrame(u8g2, x, y, w, h);
  60.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  61.         u8g2_DrawFrame(u8g2, x+w+5, y-10, w-20, h+20);
  62.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  63. }

  64. //画实心圆角矩形
  65. void testDrawRBox(u8g2_t *u8g2)
  66. {
  67.         int t = 1000;
  68.         int x = 16;
  69.         int y = 32;
  70.         int w = 50;
  71.         int h = 20;
  72.         int r = 3;
  73.         u8g2_ClearBuffer(u8g2);
  74.         u8g2_DrawStr(u8g2,0, 15, "DrawRBox");

  75.         u8g2_DrawRBox(u8g2, x, y, w, h, r);
  76.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  77.         u8g2_DrawRBox(u8g2, x+w+5, y-10, w-20, h+20, r);
  78.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  79. }

  80. //画空心圆
  81. void testDrawCircle(u8g2_t *u8g2)
  82. {
  83.         int t = 600;
  84.         int stx = 0;  //画图起始x
  85.         int sty = 16; //画图起始y
  86.         int with = 16;//一个图块的间隔
  87.         int r = 15;   //圆的半径
  88.         u8g2_ClearBuffer(u8g2);
  89.         u8g2_DrawStr(u8g2, 0, 15, "DrawCircle");

  90.         u8g2_DrawCircle(u8g2, stx, sty - 1 + with, r, U8G2_DRAW_UPPER_RIGHT); //右上
  91.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  92.         u8g2_DrawCircle(u8g2, stx + with, sty, r, U8G2_DRAW_LOWER_RIGHT); //右下
  93.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  94.         u8g2_DrawCircle(u8g2, stx - 1 + with * 3, sty - 1 + with, r, U8G2_DRAW_UPPER_LEFT); //左上
  95.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  96.         u8g2_DrawCircle(u8g2, stx - 1 + with * 4, sty, r, U8G2_DRAW_LOWER_LEFT); //左下
  97.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  98.         u8g2_DrawCircle(u8g2, stx - 1 + with * 2, sty - 1 + with * 2, r, U8G2_DRAW_ALL);//整个圆
  99.         SEND_BUFFER_DISPLAY_MS(u8g2,t);

  100.     u8g2_DrawCircle(u8g2, 32*3, 32, 31, U8G2_DRAW_ALL);//右侧整个圆
  101.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  102. }

  103. //画实心椭圆
  104. void testDrawFilledEllipse(u8g2_t *u8g2)
  105. {
  106.         int t = 800;
  107.         int with = 16;//一个图块的间隔
  108.         int rx = 27;  //椭圆x方向的半径
  109.         int ry = 22;  //椭圆y方向的半径
  110.         u8g2_ClearBuffer(u8g2);
  111.         u8g2_DrawStr(u8g2,0, 14, "DrawFilledEllipse");

  112.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  113.         u8g2_DrawFilledEllipse(u8g2, 0, with, rx, ry, U8G2_DRAW_LOWER_RIGHT);//右下
  114.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  115.         u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with, rx, ry, U8G2_DRAW_LOWER_LEFT); //左下
  116.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  117.         u8g2_DrawFilledEllipse(u8g2, 0, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_RIGHT); //右上
  118.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  119.         u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_LEFT); //左上
  120.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  121.         u8g2_DrawFilledEllipse(u8g2, with * 6, with * 2.5, rx, ry, U8G2_DRAW_ALL);//整个椭圆
  122.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  123. }

  124. //环形测试
  125. void testDrawMulti(u8g2_t *u8g2)
  126. {
  127.         u8g2_ClearBuffer(u8g2);
  128.         for (int j = 0; j < 64; j+=16)
  129.         {
  130.                 for (int i = 0; i < 128; i+=16)
  131.                 {
  132.                         u8g2_DrawPixel(u8g2, i, j);
  133.                         u8g2_SendBuffer(u8g2);
  134.                 }
  135.         }

  136.         //实心矩形逐渐变大
  137.     u8g2_ClearBuffer(u8g2);
  138.         for(int i=30; i>0; i-=2)
  139.         {
  140.                 u8g2_DrawBox(u8g2,i*2,i,128-i*4,64-2*i);
  141.                 u8g2_SendBuffer(u8g2);
  142.         }
  143.         //空心矩形逐渐变小
  144.         u8g2_ClearBuffer(u8g2);
  145.         for(int i=0; i<32; i+=2)
  146.         {
  147.                 u8g2_DrawFrame(u8g2,i*2,i,128-i*4,64-2*i);
  148.                 u8g2_SendBuffer(u8g2);
  149.         }

  150.         //实心圆角矩形逐渐变大
  151.         u8g2_ClearBuffer(u8g2);
  152.         for(int i=30; i>0; i-=2)
  153.         {
  154.                 u8g2_DrawRBox(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);
  155.                 u8g2_SendBuffer(u8g2);
  156.         }
  157.     //空心圆角矩形逐渐变小
  158.         u8g2_ClearBuffer(u8g2);
  159.         for(int i=0; i<32; i+=2)
  160.         {
  161.                 u8g2_DrawRFrame(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);
  162.                 u8g2_SendBuffer(u8g2);
  163.         }

  164.         //实心圆逐渐变大
  165.         u8g2_ClearBuffer(u8g2);
  166.         for(int i=2; i<64; i+=3)
  167.         {
  168.                 u8g2_DrawDisc(u8g2,64,32,i, U8G2_DRAW_ALL);
  169.                 u8g2_SendBuffer(u8g2);
  170.         }
  171.         //空心圆逐渐变小
  172.         u8g2_ClearBuffer(u8g2);
  173.         for(int i=64; i>0; i-=3)
  174.         {
  175.                 u8g2_DrawCircle(u8g2,64,32,i, U8G2_DRAW_ALL);
  176.                 u8g2_SendBuffer(u8g2);
  177.         }

  178.         //实心椭圆逐渐变大
  179.     u8g2_ClearBuffer(u8g2);
  180.         for(int i=2; i<32; i+=3)
  181.         {
  182.                 u8g2_DrawFilledEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);
  183.                 u8g2_SendBuffer(u8g2);
  184.         }
  185.     //空心椭圆逐渐变小
  186.     u8g2_ClearBuffer(u8g2);
  187.         for(int i=32; i>0; i-=3)
  188.         {
  189.                 u8g2_DrawEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);
  190.                 u8g2_SendBuffer(u8g2);
  191.         }
  192. }


  193. // width: 128, height: 48
  194. 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 };
  195. // width: 128, height: 48
  196. 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 };

  197. void testDrawXBM(u8g2_t *u8g2)
  198. {
  199.         int t = 1000;
  200.         u8g2_ClearBuffer(u8g2);
  201.         u8g2_DrawStr(u8g2,0, 14, "DrawXBM");

  202.         u8g2_DrawXBM(u8g2,0, 16, 128, 48, bilibili);
  203.         SEND_BUFFER_DISPLAY_MS(u8g2,t);

  204.         u8g2_ClearBuffer(u8g2);
  205.         u8g2_DrawStr(u8g2,0, 14, "bilib");
  206.         u8g2_DrawXBM(u8g2,0, 16, 128, 48, three_support);
  207.         SEND_BUFFER_DISPLAY_MS(u8g2,t);
  208. }

  209. void u8g2DrawTest(u8g2_t *u8g2)
  210. {
  211.         testDrawProcess(u8g2);
  212.         testDrawMulti(u8g2);
  213.         testDrawFrame(u8g2);
  214.         testDrawRBox(u8g2);
  215.         testDrawCircle(u8g2);
  216.         testDrawFilledEllipse(u8g2);
  217.         testShowFont(u8g2);
  218.         testDrawXBM(u8g2);

  219. }
复制代码

五、主函数main.c

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @File : main.c
  5.   * @brief : Main program body
  6.   ******************************************************************************
  7.   * @attention *
  8.   * Copyright (c) 2022 STMicroelectronics.
  9.   * All rights reserved.
  10.   *
  11.   * This software is licensed under terms that can be found in the LICENSE file
  12.   * in the root directory of this software component.
  13.   * If no LICENSE file comes with this software, it is provided AS-IS.
  14.   *
  15.   ******************************************************************************
  16.   */
  17. /* USER CODE END Header */
  18. /* Includes ------------------------------------------------------------------*/
  19. #include "main.h"


  20. /* Private includes ----------------------------------------------------------*/

  21. /* USER CODE BEGIN Includes */
  22. #include "oled.h"
  23. #include "u8g2.h"
  24. #include "test.h"

  25. /* USER CODE END Includes */

  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */

  28. /* USER CODE END PTD */

  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */

  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */

  34. /* USER CODE END PM */

  35. /* Private variables ---------------------------------------------------------*/
  36. ADC_HandleTypeDef hadc1;

  37. I2C_HandleTypeDef hi2c1;

  38. UART_HandleTypeDef huart1;

  39. PCD_HandleTypeDef hpcd_USB_OTG_FS;

  40. /* USER CODE BEGIN PV */

  41. /* USER CODE END PV */

  42. /* Private function prototypes -----------------------------------------------*/
  43. void SystemClock_Config(void);
  44. static void SystemPower_Config(void);
  45. static void MX_GPIO_Init(void);
  46. static void MX_ADC1_Init(void);
  47. static void MX_UCPD1_Init(void);
  48. static void MX_USART1_UART_Init(void);
  49. static void MX_USB_OTG_FS_PCD_Init(void);
  50. static void MX_I2C1_Init(void);
  51. /* USER CODE BEGIN PFP */

  52. /* USER CODE END PFP */

  53. /* Private user code ---------------------------------------------------------*/
  54. /* USER CODE BEGIN 0 */
  55. #include <stdio.h>

  56. #ifdef __GNUC__
  57. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  58. #else
  59. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  60. #endif
  61. PUTCHAR_PROTOTYPE
  62. {
  63.         HAL_UART_Transmit(&huart1,  (uint8_t *)&ch, 1, 0xFFFF);
  64.     return ch;
  65. }
  66. /* USER CODE END 0 */

  67. /**
  68.   * @brief  The application entry point.
  69.   * @retval int
  70.   */
  71. int main(void)
  72. {
  73.   /* USER CODE BEGIN 1 */
  74.         u8g2_t u8g2;                                                                                     // a structure which will contain all the data for one display

  75.   /* USER CODE END 1 */

  76.   /* MCU Configuration--------------------------------------------------------*/

  77.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78.   HAL_Init();

  79.   /* USER CODE BEGIN Init */
  80.   char txbuff[] = "hellworld\r\n";
  81.   /* USER CODE END Init */

  82.   /* Configure the system clock */
  83.   SystemClock_Config();

  84.   /* Configure the System Power */
  85.   SystemPower_Config();

  86.   /* USER CODE BEGIN SysInit */

  87.   /* USER CODE END SysInit */

  88.   /* Initialize all configured peripherals */
  89.   MX_GPIO_Init();
  90.   MX_ADC1_Init();
  91.   MX_UCPD1_Init();
  92.   MX_USART1_UART_Init();
  93.   MX_USB_OTG_FS_PCD_Init();
  94.   MX_I2C1_Init();
  95.   /* USER CODE BEGIN 2 */
  96.   
  97.   u8g2Init(&u8g2);
  98.   /* USER CODE END 2 */

  99.   /* Infinite loop */
  100.   /* USER CODE BEGIN WHILE */
  101.   while (1)
  102.   {
  103.     /* USER CODE END WHILE */
  104.            u8g2_FirstPage(&u8g2);
  105.        do
  106.        {
  107.                                  //draw(&u8g2);

  108.                                  u8g2DrawTest(&u8g2);
  109.        } while (u8g2_NextPage(&u8g2));


  110.     /* USER CODE BEGIN 3 */
  111.   }
  112.   /* USER CODE END 3 */
  113. }

  114. /**
  115.   * @brief System Clock Configuration
  116.   * @retval None
  117.   */
  118. void SystemClock_Config(void)
  119. {
  120.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  121.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  122.   /** Configure the main internal regulator output voltage
  123.   */
  124.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  125.   {
  126.     Error_Handler();
  127.   }

  128.   /** Initializes the CPU, AHB and APB buses clocks
  129.   */
  130.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI
  131.                               |RCC_OSCILLATORTYPE_MSI;
  132.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  133.   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  134.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  135.   RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  136.   RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  137.   RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  138.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  139.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  140.   RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  141.   RCC_OscInitStruct.PLL.PLLM = 1;
  142.   RCC_OscInitStruct.PLL.PLLN = 80;
  143.   RCC_OscInitStruct.PLL.PLLP = 2;
  144.   RCC_OscInitStruct.PLL.PLLQ = 2;
  145.   RCC_OscInitStruct.PLL.PLLR = 2;
  146.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
  147.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  148.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  149.   {
  150.     Error_Handler();
  151.   }

  152.   /** Initializes the CPU, AHB and APB buses clocks
  153.   */
  154.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  155.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  156.                               |RCC_CLOCKTYPE_PCLK3;
  157.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  158.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  159.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  160.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  161.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  162.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  163.   {
  164.     Error_Handler();
  165.   }
  166. }

  167. /**
  168.   * @brief Power Configuration
  169.   * @retval None
  170.   */
  171. static void SystemPower_Config(void)
  172. {
  173.   HAL_PWREx_EnableVddIO2();

  174.   /*
  175.    * Switch to SMPS regulator instead of LDO
  176.    */
  177.   if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
  178.   {
  179.     Error_Handler();
  180.   }
  181. }

  182. /**
  183.   * @brief ADC1 Initialization Function
  184.   * @param None
  185.   * @retval None
  186.   */
  187. static void MX_ADC1_Init(void)
  188. {

  189.   /* USER CODE BEGIN ADC1_Init 0 */

  190.   /* USER CODE END ADC1_Init 0 */

  191.   /* USER CODE BEGIN ADC1_Init 1 */

  192.   /* USER CODE END ADC1_Init 1 */

  193.   /** Common config
  194.   */
  195.   hadc1.Instance = ADC1;
  196.   hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  197.   hadc1.Init.Resolution = ADC_RESOLUTION_14B;
  198.   hadc1.Init.GainCompensation = 0;
  199.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  200.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  201.   hadc1.Init.LowPowerAutoWait = DISABLE;
  202.   hadc1.Init.ContinuousConvMode = DISABLE;
  203.   hadc1.Init.NbrOfConversion = 1;
  204.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  205.   hadc1.Init.DMAContinuousRequests = DISABLE;
  206.   hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  207.   hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  208.   hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  209.   hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  210.   hadc1.Init.OversamplingMode = DISABLE;
  211.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  212.   {
  213.     Error_Handler();
  214.   }
  215.   /* USER CODE BEGIN ADC1_Init 2 */

  216.   /* USER CODE END ADC1_Init 2 */

  217. }

  218. /**
  219.   * @brief I2C1 Initialization Function
  220.   * @param None
  221.   * @retval None
  222.   */
  223. static void MX_I2C1_Init(void)
  224. {

  225.   /* USER CODE BEGIN I2C1_Init 0 */

  226.   /* USER CODE END I2C1_Init 0 */

  227.   /* USER CODE BEGIN I2C1_Init 1 */

  228.   /* USER CODE END I2C1_Init 1 */
  229.   hi2c1.Instance = I2C1;
  230.   hi2c1.Init.Timing = 0x00F07BFF;
  231.   hi2c1.Init.OwnAddress1 = 0;
  232.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  233.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  234.   hi2c1.Init.OwnAddress2 = 0;
  235.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  236.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  237.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  238.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  239.   {
  240.     Error_Handler();
  241.   }

  242.   /** Configure Analogue filter
  243.   */
  244.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  245.   {
  246.     Error_Handler();
  247.   }

  248.   /** Configure Digital filter
  249.   */
  250.   if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  251.   {
  252.     Error_Handler();
  253.   }
  254.   /* USER CODE BEGIN I2C1_Init 2 */

  255.   /* USER CODE END I2C1_Init 2 */

  256. }

  257. /**
  258.   * @brief UCPD1 Initialization Function
  259.   * @param None
  260.   * @retval None
  261.   */
  262. static void MX_UCPD1_Init(void)
  263. {

  264.   /* USER CODE BEGIN UCPD1_Init 0 */

  265.   /* USER CODE END UCPD1_Init 0 */

  266.   LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  267.   /* Peripheral clock enable */
  268.   LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_UCPD1);

  269.   LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  270.   LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  271.   /**UCPD1 GPIO Configuration
  272.   PB15   ------> UCPD1_CC2
  273.   PA15 (JTDI)   ------> UCPD1_CC1
  274.   */
  275.   GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  276.   GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  277.   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  278.   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  279.   GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  280.   GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  281.   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  282.   LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  283.   /* USER CODE BEGIN UCPD1_Init 1 */

  284.   /* USER CODE END UCPD1_Init 1 */
  285.   /* USER CODE BEGIN UCPD1_Init 2 */

  286.   /* USER CODE END UCPD1_Init 2 */

  287. }

  288. /**
  289.   * @brief USART1 Initialization Function
  290.   * @param None
  291.   * @retval None
  292.   */
  293. static void MX_USART1_UART_Init(void)
  294. {

  295.   /* USER CODE BEGIN USART1_Init 0 */

  296.   /* USER CODE END USART1_Init 0 */

  297.   /* USER CODE BEGIN USART1_Init 1 */

  298.   /* USER CODE END USART1_Init 1 */
  299.   huart1.Instance = USART1;
  300.   huart1.Init.BaudRate = 115200;
  301.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  302.   huart1.Init.StopBits = UART_STOPBITS_1;
  303.   huart1.Init.Parity = UART_PARITY_NONE;
  304.   huart1.Init.Mode = UART_MODE_TX_RX;
  305.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  306.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  307.   huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_ENABLE;
  308.   huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  309.   huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  310.   if (HAL_UART_Init(&huart1) != HAL_OK)
  311.   {
  312.     Error_Handler();
  313.   }
  314.   if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  315.   {
  316.     Error_Handler();
  317.   }
  318.   if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  319.   {
  320.     Error_Handler();
  321.   }
  322.   if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  323.   {
  324.     Error_Handler();
  325.   }
  326.   /* USER CODE BEGIN USART1_Init 2 */

  327.   /* USER CODE END USART1_Init 2 */

  328. }

  329. /**
  330.   * @brief USB_OTG_FS Initialization Function
  331.   * @param None
  332.   * @retval None
  333.   */
  334. static void MX_USB_OTG_FS_PCD_Init(void)
  335. {

  336.   /* USER CODE BEGIN USB_OTG_FS_Init 0 */

  337.   /* USER CODE END USB_OTG_FS_Init 0 */

  338.   /* USER CODE BEGIN USB_OTG_FS_Init 1 */

  339.   /* USER CODE END USB_OTG_FS_Init 1 */
  340.   hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  341.   hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
  342.   hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  343.   hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  344.   hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  345.   hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  346.   hpcd_USB_OTG_FS.Init.battery_charging_enable = DISABLE;
  347.   hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  348.   hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
  349.   if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  350.   {
  351.     Error_Handler();
  352.   }
  353.   /* USER CODE BEGIN USB_OTG_FS_Init 2 */

  354.   /* USER CODE END USB_OTG_FS_Init 2 */

  355. }

  356. /**
  357.   * @brief GPIO Initialization Function
  358.   * @param None
  359.   * @retval None
  360.   */
  361. static void MX_GPIO_Init(void)
  362. {
  363.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  364.   /* GPIO Ports Clock Enable */
  365.   __HAL_RCC_GPIOC_CLK_ENABLE();
  366.   __HAL_RCC_GPIOB_CLK_ENABLE();
  367.   __HAL_RCC_GPIOG_CLK_ENABLE();
  368.   __HAL_RCC_GPIOA_CLK_ENABLE();

  369.   /*Configure GPIO pin Output Level */
  370.   HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);

  371.   /*Configure GPIO pin Output Level */
  372.   HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);

  373.   /*Configure GPIO pin Output Level */
  374.   HAL_GPIO_WritePin(GPIOB, UCPD_DBn_Pin|LED_BLUE_Pin, GPIO_PIN_RESET);

  375.   /*Configure GPIO pin : USER_BUTTON_Pin */
  376.   GPIO_InitStruct.Pin = USER_BUTTON_Pin;
  377.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  378.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  379.   HAL_GPIO_Init(USER_BUTTON_GPIO_Port, &GPIO_InitStruct);

  380.   /*Configure GPIO pin : UCPD_FLT_Pin */
  381.   GPIO_InitStruct.Pin = UCPD_FLT_Pin;
  382.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  383.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  384.   HAL_GPIO_Init(UCPD_FLT_GPIO_Port, &GPIO_InitStruct);

  385.   /*Configure GPIO pin : LED_RED_Pin */
  386.   GPIO_InitStruct.Pin = LED_RED_Pin;
  387.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  388.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  389.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  390.   HAL_GPIO_Init(LED_RED_GPIO_Port, &GPIO_InitStruct);

  391.   /*Configure GPIO pin : LED_GREEN_Pin */
  392.   GPIO_InitStruct.Pin = LED_GREEN_Pin;
  393.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  394.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  395.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  396.   HAL_GPIO_Init(LED_GREEN_GPIO_Port, &GPIO_InitStruct);

  397.   /*Configure GPIO pin : UCPD_DBn_Pin */
  398.   GPIO_InitStruct.Pin = UCPD_DBn_Pin;
  399.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  400.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  401.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  402.   HAL_GPIO_Init(UCPD_DBn_GPIO_Port, &GPIO_InitStruct);

  403.   /*Configure GPIO pin : LED_BLUE_Pin */
  404.   GPIO_InitStruct.Pin = LED_BLUE_Pin;
  405.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  406.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  407.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  408.   HAL_GPIO_Init(LED_BLUE_GPIO_Port, &GPIO_InitStruct);

  409. }

  410. /* USER CODE BEGIN 4 */

  411. /* USER CODE END 4 */

  412. /**
  413.   * @brief  This function is executed in case of error occurrence.
  414.   * @retval None
  415.   */
  416. void Error_Handler(void)
  417. {
  418.   /* USER CODE BEGIN Error_Handler_Debug */
  419.   /* User can add his own implementation to report the HAL error return state */
  420.   __disable_irq();
  421.   while (1)
  422.   {
  423.   }
  424.   /* USER CODE END Error_Handler_Debug */
  425. }

  426. #ifdef  USE_FULL_ASSERT
  427. /**
  428.   * @brief  Reports the name of the source file and the source line number
  429.   *         where the assert_param error has occurred.
  430.   * @param  file: pointer to the source file name
  431.   * @param  line: assert_param error line source number
  432.   * @retval None
  433.   */
  434. void assert_failed(uint8_t *file, uint32_t line)
  435. {
  436.   /* USER CODE BEGIN 6 */
  437.   /* User can add his own implementation to report the file name and line number,
  438.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  439.   /* USER CODE END 6 */
  440. }
  441. #endif /* USE_FULL_ASSERT */
复制代码


总结】整个工程主要就是改写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)这两个函数,最关键的一个函数就是:

  1.   case U8X8_MSG_BYTE_END_TRANSFER:
  2.     {
  3.         if (HAL_I2C_Master_Transmit(&hi2c1, (OLED_ADDRES), buffer, buf_idx, 1000) != HAL_OK)
  4.             return 0;
  5.     }
复制代码

效果经测试非常不错。


出处:社区用户:lugl发布



收藏 评论0 发布时间:2023-1-10 15:51

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版