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

【NUCLEO-U545RE-Q】移植u8g2

[复制链接]
TLLED 发布时间:2023-11-9 18:43
在【NUCLEO-U545RE-Q】上移植u8g2。

一、下载u8g2


官网下载地址:https://github.com/olikraus/u8g2/tree/master
004.png


二、复制文件


复制csrc文件到项目工程目录
100.png

三、添加文件到工程


将csrc文件下所有文件添加到工程
101.png


四、配置外设


项目中用到I2C和定时器1
I2C在https://shequ.stmicroelectronics.cn/thread-641714-1-1.html中已经配置,这里只配置定时器
102.png


五、代码


5.1、修改u8g2_d_setup.c
只保留下面文件
103.png




5.2、stm32_u8g2.c
  1. <font size="3">#include "main.h"
  2. #include "stm32_u8g2.h"
  3. #include "/tim/tim.h"

  4. extern I2C_HandleTypeDef hi2c1;


  5. uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  6. {
  7.     /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */
  8.     static uint8_t buffer[128];
  9.     static uint8_t buf_idx;
  10.     uint8_t *data;

  11.     switch (msg)
  12.     {
  13.     case U8X8_MSG_BYTE_INIT:
  14.     {
  15.         /* add your custom code to init i2c subsystem */
  16.   //      MX_I2C1_Init(); //I2C???
  17.     }
  18.     break;

  19.     case U8X8_MSG_BYTE_START_TRANSFER:
  20.     {
  21.         buf_idx = 0;
  22.     }
  23.     break;

  24.     case U8X8_MSG_BYTE_SEND:
  25.     {
  26.         data = (uint8_t *)arg_ptr;

  27.         while (arg_int > 0)
  28.         {
  29.             buffer[buf_idx++] = *data;
  30.             data++;
  31.             arg_int--;
  32.         }
  33.     }
  34.     break;

  35.     case U8X8_MSG_BYTE_END_TRANSFER:
  36.     {
  37.         if (HAL_I2C_Master_Transmit(&hi2c1, OLED_ADDRESS, buffer, buf_idx, 1000) != HAL_OK)
  38.             return 0;
  39.     }
  40.     break;

  41.     case U8X8_MSG_BYTE_SET_DC:
  42.         break;

  43.     default:
  44.         return 0;
  45.     }

  46.     return 1;
  47. }



  48. uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  49. {
  50.     switch (msg)
  51.     {
  52.     case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  53.         __NOP();
  54.         break;
  55.     case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  56.         for (uint16_t n = 0; n < 320; n++)
  57.         {
  58.             __NOP();
  59.         }
  60.         break;
  61.     case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  62.         HAL_Delay(1);
  63.         break;
  64.     case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  65.         Tims_delay_us(5);
  66.         break;                    // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  67.     case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
  68.         break;                    // arg_int=1: Input dir with pullup high for I2C clock pin
  69.     case U8X8_MSG_GPIO_I2C_DATA:  // arg_int=0: Output low at I2C data pin
  70.         break;                    // arg_int=1: Input dir with pullup high for I2C data pin
  71.     case U8X8_MSG_GPIO_MENU_SELECT:
  72.         u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);
  73.         break;
  74.     case U8X8_MSG_GPIO_MENU_NEXT:
  75.         u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);
  76.         break;
  77.     case U8X8_MSG_GPIO_MENU_PREV:
  78.         u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);
  79.         break;
  80.     case U8X8_MSG_GPIO_MENU_HOME:
  81.         u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);
  82.         break;
  83.     default:
  84.         u8x8_SetGPIOResult(u8x8, 1); // default return value
  85.         break;
  86.     }
  87.     return 1;
  88. }

  89. void u8g2Init(u8g2_t *u8g2)
  90. {
  91.         u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay);  
  92.         u8g2_InitDisplay(u8g2);                                                                        
  93.         u8g2_SetPowerSave(u8g2, 0);                                                                    
  94.         u8g2_ClearBuffer(u8g2);
  95. }

  96. void draw(u8g2_t *u8g2)
  97. {
  98.         u8g2_ClearBuffer(u8g2);
  99.         
  100.     u8g2_SetFontMode(u8g2, 1);  
  101.     u8g2_SetFontDirection(u8g2, 0);
  102.     u8g2_SetFont(u8g2, u8g2_font_inb24_mf);  
  103.     u8g2_DrawStr(u8g2, 0, 20, "U");
  104.    
  105.     u8g2_SetFontDirection(u8g2, 1);
  106.     u8g2_SetFont(u8g2, u8g2_font_inb30_mn);
  107.     u8g2_DrawStr(u8g2, 21,8,"8");
  108.         
  109.     u8g2_SetFontDirection(u8g2, 0);
  110.     u8g2_SetFont(u8g2, u8g2_font_inb24_mf);
  111.     u8g2_DrawStr(u8g2, 51,30,"g");
  112.     u8g2_DrawStr(u8g2, 67,30,"\xb2");
  113.    
  114.     u8g2_DrawHLine(u8g2, 2, 35, 47);
  115.     u8g2_DrawHLine(u8g2, 3, 36, 47);
  116.     u8g2_DrawVLine(u8g2, 45, 32, 12);
  117.     u8g2_DrawVLine(u8g2, 46, 33, 12);
  118.   
  119.     u8g2_SetFont(u8g2, u8g2_font_4x6_tr);
  120.     u8g2_DrawStr(u8g2, 1,54,"github.com/olikraus/u8g2");
  121.                
  122.         u8g2_SendBuffer(u8g2);
  123.         HAL_Delay(1000);
  124. }

  125. //????
  126. void testDrawPixelToFillScreen(u8g2_t *u8g2)
  127. {
  128.   int t = 1000;
  129.         u8g2_ClearBuffer(u8g2);

  130.   for (int j = 0; j < 64; j++)
  131.   {
  132.     for (int i = 0; i < 128; i++)
  133.     {
  134.       u8g2_DrawPixel(u8g2,i, j);
  135.     }
  136.   }
  137.   HAL_Delay(1000);
  138. }




  139. #define SEND_BUFFER_DISPLAY_MS(u8g2, ms)\
  140.   do {\
  141.     u8g2_SendBuffer(u8g2); \
  142.     HAL_Delay(ms);\
  143.   }while(0);


  144. void testDrawProcess(u8g2_t *u8g2)
  145. {
  146.     for(int i=10;i<=80;i=i+2)
  147.     {
  148.         u8g2_ClearBuffer(u8g2);

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

  151.         u8g2_SetFont(u8g2,u8g2_font_ncenB12_tf);
  152.         u8g2_DrawStr(u8g2,16,32,"STM32 U8g2");

  153.         u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);
  154.         u8g2_DrawStr(u8g2,100,49,buff);

  155.         u8g2_DrawRBox(u8g2,16,40,i,10,4);
  156.         u8g2_DrawRFrame(u8g2,16,40,80,10,4);

  157.         u8g2_SendBuffer(u8g2);
  158.     }
  159.     HAL_Delay(500);
  160. }


  161. void testShowFont(u8g2_t *u8g2)
  162. {
  163.     int t = 1000;
  164.     char testStr[14] = "STM32U545";

  165.     u8g2_ClearBuffer(u8g2);

  166.     u8g2_SetFont(u8g2,u8g2_font_u8glib_4_tf);
  167.     u8g2_DrawStr(u8g2,0,5,testStr);
  168.     SEND_BUFFER_DISPLAY_MS(u8g2,t);

  169.     u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);
  170.     u8g2_DrawStr(u8g2,0,30,testStr);
  171.     SEND_BUFFER_DISPLAY_MS(u8g2,t);

  172.     u8g2_SetFont(u8g2,u8g2_font_ncenB10_tr);
  173.     u8g2_DrawStr(u8g2,0,60,testStr);
  174.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  175. }

  176. void testDrawFrame(u8g2_t *u8g2)
  177. {
  178.     int t = 1000;
  179.     int x = 16;
  180.     int y = 32;
  181.     int w = 50;
  182.     int h = 20;
  183.     u8g2_ClearBuffer(u8g2);
  184.     u8g2_DrawStr(u8g2,0, 15, "DrawFrame");

  185.     u8g2_DrawFrame(u8g2, x, y, w, h);
  186.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  187.     u8g2_DrawFrame(u8g2, x+w+5, y-10, w-20, h+20);
  188.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  189. }

  190. void testDrawRBox(u8g2_t *u8g2)
  191. {
  192.     int t = 1000;
  193.     int x = 16;
  194.     int y = 32;
  195.     int w = 50;
  196.     int h = 20;
  197.     int r = 3;
  198.     u8g2_ClearBuffer(u8g2);
  199.     u8g2_DrawStr(u8g2,0, 15, "DrawRBox");

  200.     u8g2_DrawRBox(u8g2, x, y, w, h, r);
  201.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  202.     u8g2_DrawRBox(u8g2, x+w+5, y-10, w-20, h+20, r);
  203.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  204. }

  205. void testDrawCircle(u8g2_t *u8g2)
  206. {
  207.     int t = 600;
  208.     int stx = 0;  
  209.     int sty = 16;
  210.     int with = 16;
  211.     int r = 15;   
  212.     u8g2_ClearBuffer(u8g2);
  213.     u8g2_DrawStr(u8g2, 0, 15, "DrawCircle");

  214.     u8g2_DrawCircle(u8g2, stx, sty - 1 + with, r, U8G2_DRAW_UPPER_RIGHT); //??
  215.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  216.     u8g2_DrawCircle(u8g2, stx + with, sty, r, U8G2_DRAW_LOWER_RIGHT); //??
  217.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  218.     u8g2_DrawCircle(u8g2, stx - 1 + with * 3, sty - 1 + with, r, U8G2_DRAW_UPPER_LEFT); //??
  219.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  220.     u8g2_DrawCircle(u8g2, stx - 1 + with * 4, sty, r, U8G2_DRAW_LOWER_LEFT); //??
  221.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  222.     u8g2_DrawCircle(u8g2, stx - 1 + with * 2, sty - 1 + with * 2, r, U8G2_DRAW_ALL);//???
  223.     SEND_BUFFER_DISPLAY_MS(u8g2,t);

  224.     u8g2_DrawCircle(u8g2, 32*3, 32, 31, U8G2_DRAW_ALL);
  225.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  226. }

  227. void testDrawFilledEllipse(u8g2_t *u8g2)
  228. {
  229.     int t = 800;
  230.     int with = 16;
  231.     int rx = 27;  
  232.     int ry = 22;  
  233.     u8g2_ClearBuffer(u8g2);
  234.     u8g2_DrawStr(u8g2,0, 14, "DrawFilledEllipse");

  235.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  236.     u8g2_DrawFilledEllipse(u8g2, 0, with, rx, ry, U8G2_DRAW_LOWER_RIGHT);
  237.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  238.     u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with, rx, ry, U8G2_DRAW_LOWER_LEFT);  
  239.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  240.     u8g2_DrawFilledEllipse(u8g2, 0, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_RIGHT);  
  241.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  242.     u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_LEFT);  
  243.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  244.     u8g2_DrawFilledEllipse(u8g2, with * 6, with * 2.5, rx, ry, U8G2_DRAW_ALL);
  245.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  246. }

  247. void testDrawMulti(u8g2_t *u8g2)
  248. {
  249.     u8g2_ClearBuffer(u8g2);
  250.     for (int j = 0; j < 64; j+=16)
  251.     {
  252.         for (int i = 0; i < 128; i+=16)
  253.         {
  254.             u8g2_DrawPixel(u8g2, i, j);
  255.             u8g2_SendBuffer(u8g2);
  256.         }
  257.     }

  258.     u8g2_ClearBuffer(u8g2);
  259.     for(int i=30; i>0; i-=2)
  260.     {
  261.         u8g2_DrawBox(u8g2,i*2,i,128-i*4,64-2*i);
  262.         u8g2_SendBuffer(u8g2);
  263.     }

  264.     u8g2_ClearBuffer(u8g2);
  265.     for(int i=0; i<32; i+=2)
  266.     {
  267.         u8g2_DrawFrame(u8g2,i*2,i,128-i*4,64-2*i);
  268.         u8g2_SendBuffer(u8g2);
  269.     }

  270.     u8g2_ClearBuffer(u8g2);
  271.     for(int i=30; i>0; i-=2)
  272.     {
  273.         u8g2_DrawRBox(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);
  274.         u8g2_SendBuffer(u8g2);
  275.     }

  276.     u8g2_ClearBuffer(u8g2);
  277.     for(int i=0; i<32; i+=2)
  278.     {
  279.         u8g2_DrawRFrame(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);
  280.         u8g2_SendBuffer(u8g2);
  281.     }

  282.     u8g2_ClearBuffer(u8g2);
  283.     for(int i=2; i<64; i+=3)
  284.     {
  285.         u8g2_DrawDisc(u8g2,64,32,i, U8G2_DRAW_ALL);
  286.         u8g2_SendBuffer(u8g2);
  287.     }

  288.     u8g2_ClearBuffer(u8g2);
  289.     for(int i=64; i>0; i-=3)
  290.     {
  291.         u8g2_DrawCircle(u8g2,64,32,i, U8G2_DRAW_ALL);
  292.         u8g2_SendBuffer(u8g2);
  293.     }

  294.     u8g2_ClearBuffer(u8g2);
  295.     for(int i=2; i<32; i+=3)
  296.     {
  297.         u8g2_DrawFilledEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);
  298.         u8g2_SendBuffer(u8g2);
  299.     }

  300.     u8g2_ClearBuffer(u8g2);
  301.     for(int i=32; i>0; i-=3)
  302.     {
  303.         u8g2_DrawEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);
  304.         u8g2_SendBuffer(u8g2);
  305.     }
  306. }


  307. // width: 128, height: 48
  308. 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, 0x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c, 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 };
  309. // width: 128, height: 48
  310. 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 };

  311. void testDrawXBM(u8g2_t *u8g2)
  312. {
  313.     int t = 1000;
  314.     u8g2_ClearBuffer(u8g2);
  315.     u8g2_DrawStr(u8g2,0, 14, "Draw OLED");

  316.     u8g2_DrawXBM(u8g2,0, 16, 128, 48, bilibili);
  317.     SEND_BUFFER_DISPLAY_MS(u8g2,t);

  318.     u8g2_ClearBuffer(u8g2);
  319.     u8g2_DrawStr(u8g2,0, 14, "bilibili");
  320.     u8g2_DrawXBM(u8g2,0, 16, 128, 48, three_support);
  321.     SEND_BUFFER_DISPLAY_MS(u8g2,t);
  322. }

  323. void u8g2DrawTest(u8g2_t *u8g2)
  324. {
  325.     testDrawProcess(u8g2);
  326.     testDrawMulti(u8g2);
  327.     testDrawFrame(u8g2);
  328.     testDrawRBox(u8g2);
  329.     testDrawCircle(u8g2);
  330.     testDrawFilledEllipse(u8g2);
  331.     testShowFont(u8g2);
  332.     testDrawXBM(u8g2);

  333. }
  334. </font>
复制代码


5.3、stm32_u8g2.h
  1. <font size="3">#ifndef __STM32_U8G2_H
  2. #define __STM32_U8G2_H

  3. #include "main.h"
  4. #include "u8g2.h"

  5. #define u8         unsigned char  
  6. #define MAX_LEN    128  
  7. #define OLED_ADDRESS  0x78
  8. #define OLED_CMD   0x00  
  9. #define OLED_DATA  0x40  

  10. uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  11. uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  12. void u8g2Init(u8g2_t *u8g2);
  13. void draw(u8g2_t *u8g2);
  14. void testDrawPixelToFillScreen(u8g2_t *u8g2);

  15. void testDrawProcess(u8g2_t *u8g2);
  16. void testShowFont(u8g2_t *u8g2);
  17. void testDrawFrame(u8g2_t *u8g2);
  18. void testDrawRBox(u8g2_t *u8g2);
  19. void testDrawCircle(u8g2_t *u8g2);
  20. void testDrawFilledEllipse(u8g2_t *u8g2);
  21. void testDrawMulti(u8g2_t *u8g2);
  22. void testDrawXBM(u8g2_t *u8g2);


  23. #endif</font>
复制代码


5.4、tim.c
  1. <font size="3">#include "main.h"
  2. #include "tim/tim.h"

  3. extern TIM_HandleTypeDef htim1;


  4. void Tims_delay_us(uint32_t nus)
  5. {

  6.     uint16_t  differ = 0xffff-nus-5;
  7.                 __HAL_TIM_SetCounter(&htim1,differ);

  8.     HAL_TIM_Base_Start(&htim1);

  9.     while( differ<0xffff-5)
  10.     {
  11.         differ = __HAL_TIM_GetCounter(&htim1);
  12.     };
  13.     //?????
  14.     HAL_TIM_Base_Stop(&htim1);
  15. }</font>
复制代码


5.5、main.c
  1. <font size="3">int main(void)
  2. {
  3.   /* USER CODE BEGIN 1 */
  4.         u8g2_t u8g2;
  5.   /* USER CODE END 1 */

  6.   /* MCU Configuration--------------------------------------------------------*/

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

  9.   /* USER CODE BEGIN Init */

  10.   /* USER CODE END Init */

  11.   /* Configure the system clock */
  12.   SystemClock_Config();

  13.   /* Configure the System Power */
  14.   SystemPower_Config();

  15.   /* USER CODE BEGIN SysInit */

  16.   /* USER CODE END SysInit */

  17.   /* Initialize all configured peripherals */
  18.   MX_GPIO_Init();
  19.   MX_ADC1_Init();
  20.   MX_ICACHE_Init();
  21.   MX_USART1_UART_Init();
  22.   MX_USB_DRD_FS_HCD_Init();
  23.   MX_I2C1_Init();
  24.   MX_TIM1_Init();
  25.   /* USER CODE BEGIN 2 */
  26.         
  27.   u8g2Init(&u8g2);
  28.         


  29.   /* USER CODE END 2 */

  30.   /* Infinite loop */
  31.   /* USER CODE BEGIN WHILE */
  32.   while (1)
  33.   {
  34.     /* USER CODE END WHILE */

  35.     /* USER CODE BEGIN 3 */
  36.                 //HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
  37.     //HAL_Delay(200);

  38.                 u8g2_FirstPage(&u8g2);
  39.                 do
  40.                 {
  41.                                 draw(&u8g2);

  42.                                 u8g2DrawTest(&u8g2);
  43.                 } while (u8g2_NextPage(&u8g2));

  44.   }
  45.   /* USER CODE END 3 */
  46. }</font>
复制代码


六、运行结果


u8g2-.gif


收藏 评论2 发布时间:2023-11-9 18:43

举报

2个回答
落花又见流水 回答时间:2023-12-12 09:34:56

这个不错,可以参考一下

STMWoodData 回答时间:2023-12-12 10:13:09

效果不错,赞一个

所属标签

ST中文论坛活动

即日起开启活动话题入口,之后的活动统一都放在此处,欢迎大家的加入!


最新内容

相似分享

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