当前位置:   article > 正文

基于I2C协议的OLED显示(利用U82G库)_u2g8库功能

u2g8库功能

目录

一、I2C通讯协议

1.基本原理

2.时序协议

二、demo例子U8g2

 1.Cube配置

 2.U8g2移植

​编辑

​编辑

3.KEil代码配置

4.实验效果

三、OLED屏显示昵称

Cube配置和u8g2配置同上

1.KEil代码配置

2.实验效果

参考博客:


一、I2C通讯协议

1.基本原理

I2C是一种串行通信协议,通常用于连接微控制器和各种外部设备,如传感器、存储器、显示屏等。 I2C总线由两根线构成:串行数据线(SDA)和串行时钟线(SCL)。在I2C总线上,可以同时连接多个设备,每个设备都有一个唯一的7位地址,用于在总线上唯一识别该设备。通过发送起始条件和设备地址,主设备(通常是微控制器)可以选择与特定设备通信。通信过程中,数据通过SDA线传输,时钟信号通过SCL线同步传输。

其分为两个模式:

一主多从模式:一个主设备,多个从设备。主设备负责发送起始条件、设备地址以及读写指令,从而选择特定的从设备进行数据交换。其他从设备则根据其地址是否匹配来决定是否响应主设备的通信请求。

多主多从模式: 多个主设备,多个从设备。不同主设备之间会通过仲裁机制来协调总线的访问权,避免通信冲突和数据丢失。当一个主设备想要访问总线上的某个从设备时,它必须首先获得总线的控制权,其他主设备则处于被动监听状态。

2.时序协议

起始条件:

SCL高电平期间,SDA由高电平变为低电平

当主机进行收发时,让SDA产生下降沿;从机捕获SCL高电平、SDA下降沿时,会进行复位等待主机命令。

在SDA下降沿之后,主机会让SCL变成低电平,一方面是占用这个总线,另一方面是为了进行拼接。因为除了起始和终止条件,每个时序单元的SCL都是低电平开始低电平结束。

终止条件:

SCL高电平期间,SDA由低电平变为高电平

发送一个字节:

在SCL低电平时SDA要先确定好发送的数据,在SCL上升沿时会立刻读取SDA的数据,然后在高电平期间,从机获取SDA的数据,然后SCL回到低电平。

传输过程是从高位开始。SCL高电平期间,SDA不允许有数据变化。循环8次即可发送一个字节。

接收一个字节:

在接收数据前,主机要先释放SDA,交由从机控制。然后与发送字节单元类似,SCL低电平时(前一个下降沿后),从机确定好要发送的数据。在SCL高电平期间主机开始接收数据

虚线为从机电平变化。

发送应对单元:

在接收一个字节之后,接着发送了一位应答位,SCL高电平时读取,SDA高电平1为非应答,低电平0为应答。发送应答成功后主机把SDA拉为低电平

接收应答单元

在发送一个字节之后,接着会接收一位应答位,SCL高电平时读取,SDA高电平为1非应答,低电平0为应答。主机接收前需释放SDA,从机机把SDA拉为低电平,主机收到0则应答成功

二、demo例子U8g2

 1.Cube配置

RCC配置:

SYS配置:

TIM1配置:

I2C2配置:

GPIO配置:

clock配置:

项目配置:

 2.U8g2移植

(1)去掉无用文件

这次实验,我们主要使用csrc文件。ssd1306驱动芯片的OLED,使用u8x8_d_ssd1306_128x64_noname.c这个文件,其它的屏幕驱动和分辨率的文件可以删掉。

其次我们可以对u8g2_d_setup.c文件进行精简,只留一个本次要用到u8g2_Setup_ssd1306_i2c_128x64_noname_f,其它的可以删掉。

精简u8g2_d_memory.c

用到的u8g2_Setup_ssd1306_i2c_128x64_noname_f函数中,只调用了u8g2_m_16_8_f这个函数,所以留下这个函数,其余删掉

3.KEil代码配置

将我们刚精简的u8g2文件添加进项目组U8g2中。

将下面代码文件添加至hardware组中。

 stm32_u8g2.c:

  1. #include "stm32_u8g2.h"
  2. #include "tim.h"
  3. #include "i2c.h"
  4. uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  5. {
  6. /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */
  7. static uint8_t buffer[128];
  8. static uint8_t buf_idx;
  9. uint8_t *data;
  10. switch (msg)
  11. {
  12. case U8X8_MSG_BYTE_INIT:
  13. {
  14. /* add your custom code to init i2c subsystem */
  15. MX_I2C2_Init(); //I2C初始化
  16. }
  17. break;
  18. case U8X8_MSG_BYTE_START_TRANSFER:
  19. {
  20. buf_idx = 0;
  21. }
  22. break;
  23. case U8X8_MSG_BYTE_SEND:
  24. {
  25. data = (uint8_t *)arg_ptr;
  26. while (arg_int > 0)
  27. {
  28. buffer[buf_idx++] = *data;
  29. data++;
  30. arg_int--;
  31. }
  32. }
  33. break;
  34. case U8X8_MSG_BYTE_END_TRANSFER:
  35. {
  36. if (HAL_I2C_Master_Transmit(&hi2c2, OLED_ADDRESS, buffer, buf_idx, 1000) != HAL_OK)
  37. return 0;
  38. }
  39. break;
  40. case U8X8_MSG_BYTE_SET_DC:
  41. break;
  42. default:
  43. return 0;
  44. }
  45. return 1;
  46. }
  47. uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  48. {
  49. switch (msg)
  50. {
  51. case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
  52. __NOP();
  53. break;
  54. case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
  55. for (uint16_t n = 0; n < 320; n++)
  56. {
  57. __NOP();
  58. }
  59. break;
  60. case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
  61. HAL_Delay(1);
  62. break;
  63. case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
  64. Tims_delay_us(5);
  65. break; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
  66. case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
  67. break; // arg_int=1: Input dir with pullup high for I2C clock pin
  68. case U8X8_MSG_GPIO_I2C_DATA: // arg_int=0: Output low at I2C data pin
  69. break; // arg_int=1: Input dir with pullup high for I2C data pin
  70. case U8X8_MSG_GPIO_MENU_SELECT:
  71. u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);
  72. break;
  73. case U8X8_MSG_GPIO_MENU_NEXT:
  74. u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);
  75. break;
  76. case U8X8_MSG_GPIO_MENU_PREV:
  77. u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);
  78. break;
  79. case U8X8_MSG_GPIO_MENU_HOME:
  80. u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);
  81. break;
  82. default:
  83. u8x8_SetGPIOResult(u8x8, 1); // default return value
  84. break;
  85. }
  86. return 1;
  87. }
  88. //U8g2的初始化,需要调用下面这个u8g2_Setup_ssd1306_128x64_noname_f函数,该函数的4个参数含义:
  89. //u8g2:传入的U8g2结构体
  90. //U8G2_R0:默认使用U8G2_R0即可(用于配置屏幕是否要旋转)
  91. //u8x8_byte_sw_i2c:使用软件IIC驱动,该函数由U8g2源码提供
  92. //u8x8_gpio_and_delay:就是上面我们写的配置函数
  93. void u8g2Init(u8g2_t *u8g2)
  94. {
  95. u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay); // 初始化u8g2 结构体
  96. u8g2_InitDisplay(u8g2); //
  97. u8g2_SetPowerSave(u8g2, 0); //
  98. u8g2_ClearBuffer(u8g2);
  99. }
  100. void draw(u8g2_t *u8g2)
  101. {
  102. u8g2_ClearBuffer(u8g2);
  103. u8g2_SetFontMode(u8g2, 1); /*字体模式选择*/
  104. u8g2_SetFontDirection(u8g2, 0); /*字体方向选择*/
  105. u8g2_SetFont(u8g2, u8g2_font_inb24_mf); /*字库选择*/
  106. u8g2_DrawStr(u8g2, 0, 20, "U");
  107. u8g2_SetFontDirection(u8g2, 1);
  108. u8g2_SetFont(u8g2, u8g2_font_inb30_mn);
  109. u8g2_DrawStr(u8g2, 21,8,"8");
  110. u8g2_SetFontDirection(u8g2, 0);
  111. u8g2_SetFont(u8g2, u8g2_font_inb24_mf);
  112. u8g2_DrawStr(u8g2, 51,30,"g");
  113. u8g2_DrawStr(u8g2, 67,30,"\xb2");
  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. u8g2_SetFont(u8g2, u8g2_font_4x6_tr);
  119. u8g2_DrawStr(u8g2, 1,54,"github.com/olikraus/u8g2");
  120. u8g2_SendBuffer(u8g2);
  121. HAL_Delay(1000);
  122. }
  123. //画点填充
  124. void testDrawPixelToFillScreen(u8g2_t *u8g2)
  125. {
  126. int t = 1000;
  127. u8g2_ClearBuffer(u8g2);
  128. for (int j = 0; j < 64; j++)
  129. {
  130. for (int i = 0; i < 128; i++)
  131. {
  132. u8g2_DrawPixel(u8g2,i, j);
  133. }
  134. }
  135. HAL_Delay(1000);
  136. }

将其对应生成的stm32_u8g2.h文件代码修改为:

  1. #ifndef __STM32_U8G2_H
  2. #define __STM32_U8G2_H
  3. /* Includes ------------------------------------------------------------------*/
  4. #include "main.h"
  5. #include "u8g2.h"
  6. /* USER CODE BEGIN Includes */
  7. /* USER CODE END Includes */
  8. /* USER CODE BEGIN Private defines */
  9. /* USER CODE END Private defines */
  10. #define u8 unsigned char // ?unsigned char ????
  11. #define MAX_LEN 128 //
  12. #define OLED_ADDRESS 0x78 // oled
  13. #define OLED_CMD 0x00 //
  14. #define OLED_DATA 0x40 //
  15. /* USER CODE BEGIN Prototypes */
  16. uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  17. uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
  18. void u8g2Init(u8g2_t *u8g2);
  19. void draw(u8g2_t *u8g2);
  20. void testDrawPixelToFillScreen(u8g2_t *u8g2);
  21. #endif

test.c文件:

  1. #include "test.h"
  2. //---------------U8g2测试函数
  3. #define SEND_BUFFER_DISPLAY_MS(u8g2, ms)\
  4. do {\
  5. u8g2_SendBuffer(u8g2); \
  6. HAL_Delay(ms);\
  7. }while(0);
  8. //进度条显示
  9. void testDrawProcess(u8g2_t *u8g2)
  10. {
  11. for(int i=10;i<=80;i=i+2)
  12. {
  13. u8g2_ClearBuffer(u8g2);
  14. char buff[20];
  15. sprintf(buff,"%d%%",(int)(i/80.0*100));
  16. u8g2_SetFont(u8g2,u8g2_font_ncenB12_tf);
  17. u8g2_DrawStr(u8g2,16,32,"STM32 U8g2");//字符显示
  18. u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);
  19. u8g2_DrawStr(u8g2,100,49,buff);//当前进度显示
  20. u8g2_DrawRBox(u8g2,16,40,i,10,4);//圆角填充框矩形框
  21. u8g2_DrawRFrame(u8g2,16,40,80,10,4);//圆角矩形
  22. u8g2_SendBuffer(u8g2);
  23. }
  24. HAL_Delay(500);
  25. }
  26. //字体测试 数字英文可选用 u8g2_font_ncenB..(粗) 系列字体
  27. //u8g2_font_unifont_t_symbols/u8g2_font_unifont_h_symbols(细 圆润)
  28. void testShowFont(u8g2_t *u8g2)
  29. {
  30. int t = 1000;
  31. char testStr[14] = "STM32F103C8T6";
  32. u8g2_ClearBuffer(u8g2);
  33. u8g2_SetFont(u8g2,u8g2_font_u8glib_4_tf);
  34. u8g2_DrawStr(u8g2,0,5,testStr);
  35. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  36. u8g2_SetFont(u8g2,u8g2_font_ncenB08_tf);
  37. u8g2_DrawStr(u8g2,0,30,testStr);
  38. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  39. u8g2_SetFont(u8g2,u8g2_font_ncenB10_tr);
  40. u8g2_DrawStr(u8g2,0,60,testStr);
  41. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  42. }
  43. //画空心矩形
  44. void testDrawFrame(u8g2_t *u8g2)
  45. {
  46. int t = 1000;
  47. int x = 16;
  48. int y = 32;
  49. int w = 50;
  50. int h = 20;
  51. u8g2_ClearBuffer(u8g2);
  52. u8g2_DrawStr(u8g2,0, 15, "DrawFrame");
  53. u8g2_DrawFrame(u8g2, x, y, w, h);
  54. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  55. u8g2_DrawFrame(u8g2, x+w+5, y-10, w-20, h+20);
  56. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  57. }
  58. //画实心圆角矩形
  59. void testDrawRBox(u8g2_t *u8g2)
  60. {
  61. int t = 1000;
  62. int x = 16;
  63. int y = 32;
  64. int w = 50;
  65. int h = 20;
  66. int r = 3;
  67. u8g2_ClearBuffer(u8g2);
  68. u8g2_DrawStr(u8g2,0, 15, "DrawRBox");
  69. u8g2_DrawRBox(u8g2, x, y, w, h, r);
  70. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  71. u8g2_DrawRBox(u8g2, x+w+5, y-10, w-20, h+20, r);
  72. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  73. }
  74. //画空心圆
  75. void testDrawCircle(u8g2_t *u8g2)
  76. {
  77. int t = 600;
  78. int stx = 0; //画图起始x
  79. int sty = 16; //画图起始y
  80. int with = 16;//一个图块的间隔
  81. int r = 15; //圆的半径
  82. u8g2_ClearBuffer(u8g2);
  83. u8g2_DrawStr(u8g2, 0, 15, "DrawCircle");
  84. u8g2_DrawCircle(u8g2, stx, sty - 1 + with, r, U8G2_DRAW_UPPER_RIGHT); //右上
  85. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  86. u8g2_DrawCircle(u8g2, stx + with, sty, r, U8G2_DRAW_LOWER_RIGHT); //右下
  87. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  88. u8g2_DrawCircle(u8g2, stx - 1 + with * 3, sty - 1 + with, r, U8G2_DRAW_UPPER_LEFT); //左上
  89. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  90. u8g2_DrawCircle(u8g2, stx - 1 + with * 4, sty, r, U8G2_DRAW_LOWER_LEFT); //左下
  91. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  92. u8g2_DrawCircle(u8g2, stx - 1 + with * 2, sty - 1 + with * 2, r, U8G2_DRAW_ALL);//整个圆
  93. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  94. u8g2_DrawCircle(u8g2, 32*3, 32, 31, U8G2_DRAW_ALL);//右侧整个圆
  95. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  96. }
  97. //画实心椭圆
  98. void testDrawFilledEllipse(u8g2_t *u8g2)
  99. {
  100. int t = 800;
  101. int with = 16;//一个图块的间隔
  102. int rx = 27; //椭圆x方向的半径
  103. int ry = 22; //椭圆y方向的半径
  104. u8g2_ClearBuffer(u8g2);
  105. u8g2_DrawStr(u8g2,0, 14, "DrawFilledEllipse");
  106. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  107. u8g2_DrawFilledEllipse(u8g2, 0, with, rx, ry, U8G2_DRAW_LOWER_RIGHT);//右下
  108. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  109. u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with, rx, ry, U8G2_DRAW_LOWER_LEFT); //左下
  110. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  111. u8g2_DrawFilledEllipse(u8g2, 0, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_RIGHT); //右上
  112. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  113. u8g2_DrawFilledEllipse(u8g2, with * 4 - 1, with * 4 - 1, rx, ry, U8G2_DRAW_UPPER_LEFT); //左上
  114. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  115. u8g2_DrawFilledEllipse(u8g2, with * 6, with * 2.5, rx, ry, U8G2_DRAW_ALL);//整个椭圆
  116. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  117. }
  118. //环形测试
  119. void testDrawMulti(u8g2_t *u8g2)
  120. {
  121. u8g2_ClearBuffer(u8g2);
  122. for (int j = 0; j < 64; j+=16)
  123. {
  124. for (int i = 0; i < 128; i+=16)
  125. {
  126. u8g2_DrawPixel(u8g2, i, j);
  127. u8g2_SendBuffer(u8g2);
  128. }
  129. }
  130. //实心矩形逐渐变大
  131. u8g2_ClearBuffer(u8g2);
  132. for(int i=30; i>0; i-=2)
  133. {
  134. u8g2_DrawBox(u8g2,i*2,i,128-i*4,64-2*i);
  135. u8g2_SendBuffer(u8g2);
  136. }
  137. //空心矩形逐渐变小
  138. u8g2_ClearBuffer(u8g2);
  139. for(int i=0; i<32; i+=2)
  140. {
  141. u8g2_DrawFrame(u8g2,i*2,i,128-i*4,64-2*i);
  142. u8g2_SendBuffer(u8g2);
  143. }
  144. //实心圆角矩形逐渐变大
  145. u8g2_ClearBuffer(u8g2);
  146. for(int i=30; i>0; i-=2)
  147. {
  148. u8g2_DrawRBox(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);
  149. u8g2_SendBuffer(u8g2);
  150. }
  151. //空心圆角矩形逐渐变小
  152. u8g2_ClearBuffer(u8g2);
  153. for(int i=0; i<32; i+=2)
  154. {
  155. u8g2_DrawRFrame(u8g2,i*2,i,128-i*4,64-2*i,10-i/3);
  156. u8g2_SendBuffer(u8g2);
  157. }
  158. //实心圆逐渐变大
  159. u8g2_ClearBuffer(u8g2);
  160. for(int i=2; i<64; i+=3)
  161. {
  162. u8g2_DrawDisc(u8g2,64,32,i, U8G2_DRAW_ALL);
  163. u8g2_SendBuffer(u8g2);
  164. }
  165. //空心圆逐渐变小
  166. u8g2_ClearBuffer(u8g2);
  167. for(int i=64; i>0; i-=3)
  168. {
  169. u8g2_DrawCircle(u8g2,64,32,i, U8G2_DRAW_ALL);
  170. u8g2_SendBuffer(u8g2);
  171. }
  172. //实心椭圆逐渐变大
  173. u8g2_ClearBuffer(u8g2);
  174. for(int i=2; i<32; i+=3)
  175. {
  176. u8g2_DrawFilledEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);
  177. u8g2_SendBuffer(u8g2);
  178. }
  179. //空心椭圆逐渐变小
  180. u8g2_ClearBuffer(u8g2);
  181. for(int i=32; i>0; i-=3)
  182. {
  183. u8g2_DrawEllipse(u8g2,64,32, i*2, i, U8G2_DRAW_ALL);
  184. u8g2_SendBuffer(u8g2);
  185. }
  186. }
  187. // width: 128, height: 48
  188. 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 };
  189. // width: 128, height: 48
  190. 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 };
  191. void testDrawXBM(u8g2_t *u8g2)
  192. {
  193. int t = 1000;
  194. u8g2_ClearBuffer(u8g2);
  195. u8g2_DrawStr(u8g2,0, 14, "DrawXBM");
  196. u8g2_DrawXBM(u8g2,0, 16, 128, 48, bilibili);
  197. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  198. u8g2_ClearBuffer(u8g2);
  199. u8g2_DrawStr(u8g2,0, 14, "bilibili");
  200. u8g2_DrawXBM(u8g2,0, 16, 128, 48, three_support);
  201. SEND_BUFFER_DISPLAY_MS(u8g2,t);
  202. }
  203. void u8g2DrawTest(u8g2_t *u8g2)
  204. {
  205. testDrawProcess(u8g2);
  206. testDrawMulti(u8g2);
  207. //testDrawFrame(u8g2);
  208. //testDrawRBox(u8g2);
  209. //testDrawCircle(u8g2);
  210. //testDrawFilledEllipse(u8g2);
  211. testShowFont(u8g2);
  212. testDrawXBM(u8g2);
  213. }

将其对应生成的test.h文件修改为

  1. #ifndef __TEST_H
  2. #define __TEST_H
  3. #include "main.h"
  4. #include "u8g2.h"
  5. void testDrawProcess(u8g2_t *u8g2);
  6. void testShowFont(u8g2_t *u8g2);
  7. void testDrawFrame(u8g2_t *u8g2);
  8. void testDrawRBox(u8g2_t *u8g2);
  9. void testDrawCircle(u8g2_t *u8g2);
  10. void testDrawFilledEllipse(u8g2_t *u8g2);
  11. void testDrawMulti(u8g2_t *u8g2);
  12. void testDrawXBM(u8g2_t *u8g2);
  13. void u8g2DrawTest(u8g2_t *u8g2);
  14. #endif

main.c代码编写:

  1. int main(void)
  2. {
  3. /* USER CODE BEGIN 1 */
  4. /* USER CODE END 1 */
  5. /* MCU Configuration--------------------------------------------------------*/
  6. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  7. HAL_Init();
  8. /* USER CODE BEGIN Init */
  9. /* USER CODE END Init */
  10. /* Configure the system clock */
  11. SystemClock_Config();
  12. /* USER CODE BEGIN SysInit */
  13. /* USER CODE END SysInit */
  14. /* Initialize all configured peripherals */
  15. MX_GPIO_Init();
  16. MX_I2C2_Init();
  17. MX_TIM1_Init();
  18. /* USER CODE BEGIN 2 */
  19. u8g2_t u8g2;
  20. u8g2Init(&u8g2);
  21. /* USER CODE END 2 */
  22. /* Infinite loop */
  23. /* USER CODE BEGIN WHILE */
  24. while (1)
  25. {
  26. /* USER CODE END WHILE */
  27. /* USER CODE BEGIN 3 */
  28. u8g2_FirstPage(&u8g2);
  29. do
  30. {
  31. draw(&u8g2);
  32. u8g2DrawTest(&u8g2);
  33. } while (u8g2_NextPage(&u8g2));
  34. }
  35. /* USER CODE END 3 */
  36. }

4.实验效果

三、OLED屏显示昵称

Cube配置和u8g2配置同上

1.KEil代码配置

将我们刚精简的u8g2文件添加进项目组U8g2中。

将下面代码文件添加至hardware组中

oled.c:

  1. #include "oled.h"
  2. #include "asc.h"
  3. #include "main.h"
  4. void WriteCmd(unsigned char I2C_Command)//???
  5. {
  6. HAL_I2C_Mem_Write(&hi2c2,OLED0561_ADD,COM,I2C_MEMADD_SIZE_8BIT,&I2C_Command,1,100);
  7. }
  8. void WriteDat(unsigned char I2C_Data)//???
  9. {
  10. HAL_I2C_Mem_Write(&hi2c2,OLED0561_ADD,DAT,I2C_MEMADD_SIZE_8BIT,&I2C_Data,1,100);
  11. }
  12. void OLED_Init(void)
  13. {
  14. HAL_Delay(100); //????????
  15. WriteCmd(0xAE); //display off
  16. WriteCmd(0x20); //Set Memory Addressing Mode
  17. WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
  18. WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
  19. WriteCmd(0xc8); //Set COM Output Scan Direction
  20. WriteCmd(0x00); //---set low column address
  21. WriteCmd(0x10); //---set high column address
  22. WriteCmd(0x40); //--set start line address
  23. WriteCmd(0x81); //--set contrast control register
  24. WriteCmd(0xff); //???? 0x00~0xff
  25. WriteCmd(0xa1); //--set segment re-map 0 to 127
  26. WriteCmd(0xa6); //--set normal display
  27. WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
  28. WriteCmd(0x3F); //
  29. WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
  30. WriteCmd(0xd3); //-set display offset
  31. WriteCmd(0x00); //-not offset
  32. WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
  33. WriteCmd(0xf0); //--set divide ratio
  34. WriteCmd(0xd9); //--set pre-charge period
  35. WriteCmd(0x22); //
  36. WriteCmd(0xda); //--set com pins hardware configuration
  37. WriteCmd(0x12);
  38. WriteCmd(0xdb); //--set vcomh
  39. WriteCmd(0x20); //0x20,0.77xVcc
  40. WriteCmd(0x8d); //--set DC-DC enable
  41. WriteCmd(0x14); //
  42. WriteCmd(0xaf); //--turn on oled panel
  43. }
  44. void OLED_SetPos(unsigned char x, unsigned char y) //???????
  45. {
  46. WriteCmd(0xb0+y);
  47. WriteCmd(((x&0xf0)>>4)|0x10);
  48. WriteCmd((x&0x0f)|0x01);
  49. }
  50. void OLED_Fill(unsigned char fill_Data)//????
  51. {
  52. unsigned char m,n;
  53. for(m=0;m<8;m++)
  54. {
  55. WriteCmd(0xb0+m); //page0-page1
  56. WriteCmd(0x00); //low column start address
  57. WriteCmd(0x10); //high column start address
  58. for(n=0;n<128;n++)
  59. {
  60. WriteDat(fill_Data);
  61. }
  62. }
  63. }
  64. void OLED_CLS(void)//??
  65. {
  66. OLED_Fill(0x00);
  67. }
  68. void OLED_ON(void)
  69. {
  70. WriteCmd(0X8D); //?????
  71. WriteCmd(0X14); //?????
  72. WriteCmd(0XAF); //OLED??
  73. }
  74. void OLED_OFF(void)
  75. {
  76. WriteCmd(0X8D); //?????
  77. WriteCmd(0X10); //?????
  78. WriteCmd(0XAE); //OLED??
  79. }
  80. // Parameters : x,y -- ?????(x:0~127, y:0~7); ch[] -- ???????; TextSize -- ????(1:6*8 ; 2:8*16)
  81. // Description : ??codetab.h??ASCII??,?6*8?8*16???
  82. void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
  83. {
  84. unsigned char c = 0,i = 0,j = 0;
  85. switch(TextSize)
  86. {
  87. case 1:
  88. {
  89. while(ch[j] != '\0')
  90. {
  91. c = ch[j] - 32;
  92. if(x > 126)
  93. {
  94. x = 0;
  95. y++;
  96. }
  97. OLED_SetPos(x,y);
  98. for(i=0;i<6;i++)
  99. WriteDat(F6x8[c][i]);
  100. x += 6;
  101. j++;
  102. }
  103. }break;
  104. case 2:
  105. {
  106. while(ch[j] != '\0')
  107. {
  108. c = ch[j] - 32;
  109. if(x > 120)
  110. {
  111. x = 0;
  112. y++;
  113. }
  114. OLED_SetPos(x,y);
  115. for(i=0;i<8;i++)
  116. WriteDat(F8X16[c*16+i]);
  117. OLED_SetPos(x,y+1);
  118. for(i=0;i<8;i++)
  119. WriteDat(F8X16[c*16+i+8]);
  120. x += 8;
  121. j++;
  122. }
  123. }break;
  124. }
  125. }
  126. // Parameters : x,y -- ?????(x:0~127, y:0~7); N:???.h????
  127. // Description : ??ASCII_8x16.h????,16*16??
  128. void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
  129. {
  130. unsigned char wm=0;
  131. unsigned int adder=32*N;
  132. OLED_SetPos(x , y);
  133. for(wm = 0;wm < 16;wm++)
  134. {
  135. WriteDat(F16x16[adder]);
  136. adder += 1;
  137. }
  138. OLED_SetPos(x,y + 1);
  139. for(wm = 0;wm < 16;wm++)
  140. {
  141. WriteDat(F16x16[adder]);
  142. adder += 1;
  143. }
  144. }
  145. // ????????????????,?????????????????????????ascll.h?????(????)
  146. //???????:x:?????
  147. // y:???(??0-7)
  148. // begin:????????????????ascll.c???????
  149. // num:????????
  150. // ?????????????????????????0,1,???0,??????,??:x:0,y:2,begin:0,num:2
  151. void OLED_ShowCN_STR(u8 x , u8 y , u8 begin , u8 num)
  152. {
  153. u8 i;
  154. for(i=0;i<num;i++){OLED_ShowCN(i*16+x,y,i+begin);} //OLED????
  155. }
  156. // Parameters : x0,y0 -- ?????(x0:0~127, y0:0~7); x1,y1 -- ?????(???)???(x1:1~128,y1:1~8)
  157. // Description : ??BMP??
  158. void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
  159. {
  160. unsigned int j=0;
  161. unsigned char x,y;
  162. if(y1%8==0)
  163. y = y1/8;
  164. else
  165. y = y1/8 + 1;
  166. for(y=y0;y<y1;y++)
  167. {
  168. OLED_SetPos(x0,y);
  169. for(x=x0;x<x1;x++)
  170. {
  171. WriteDat(BMP[j++]);
  172. }
  173. }
  174. }
  175. void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size)
  176. {
  177. unsigned char c=0,i=0;
  178. c=chr-' ';//???????
  179. if(x>128-1){x=0;y=y+2;}
  180. if(Char_Size ==16)
  181. {
  182. OLED_SetPos(x,y);
  183. for(i=0;i<8;i++)
  184. WriteDat(F8X16[c*16+i]);
  185. OLED_SetPos(x,y+1);
  186. for(i=0;i<8;i++)
  187. WriteDat(F8X16[c*16+i+8]);
  188. }
  189. else {
  190. OLED_SetPos(x,y);
  191. for(i=0;i<6;i++)
  192. WriteDat(F6x8[c][i]);
  193. }
  194. }
  195. u32 oled_pow(u8 m,u8 n)
  196. {
  197. u32 result=1;
  198. while(n--)result*=m;
  199. return result;
  200. }
  201. //??2???
  202. //x,y :????
  203. //len :?????
  204. //size:????
  205. //mode:?? 0,????;1,????
  206. //num:??(0~4294967295);
  207. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
  208. {
  209. u8 t,temp;
  210. u8 enshow=0;
  211. for(t=0;t<len;t++)
  212. {
  213. temp=(num/oled_pow(10,len-t-1))%10;
  214. if(enshow==0&&t<(len-1))
  215. {
  216. if(temp==0)
  217. {
  218. OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
  219. continue;
  220. }else enshow=1;
  221. }
  222. OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
  223. }
  224. }
  225. /*
  226. @brief ????
  227. @param x0:?????
  228. y0:?????
  229. x1:?????
  230. y1:?????
  231. k: ???
  232. m: ??????
  233. BMP[][m]:?????????
  234. @retval ?
  235. */
  236. void OLED_DrawGIF(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1, unsigned char k, int m, unsigned char GIF[][m])
  237. {
  238. unsigned int j=0; //????
  239. unsigned char x,y,i; //????
  240. if(y1%8==0) y=y1/8; //????????8????
  241. else y=y1/8+1;
  242. for (i=0;i<k;i++) //???????
  243. {
  244. j = 0;
  245. for(y=y0;y<y1;y++) //??????,?????
  246. {
  247. OLED_SetPos(x0,y); //?????????
  248. for(x=x0;x<x1;x++) //?x1 - x0 ?
  249. {
  250. WriteDat(GIF[i][j++]); //?????
  251. }
  252. }
  253. //delay_ms(80);//?????????
  254. }
  255. }

oled.h:

  1. #ifndef __OLED_H
  2. #define __OLED_H
  3. #include "i2c.h"
  4. #define u8 uint8_t
  5. #define u32 uint32_t
  6. // ??????
  7. // OLED_ShowStr(0, 0, "hello world", 2);//?????
  8. // OLED_ShowStr(0, 2, "hello world", 1);//?????
  9. // OLED_ShowCN_STR(0, 4 , 0 , 8);
  10. // sprintf(num_temp_buffer,"show num:%0.2f",num);
  11. // OLED_ShowStr(0, 6, num_temp_buffer, 2);//?????
  12. // OLED_CLS();
  13. // OLED_DrawBMP(0,0,128,7,BMP2);
  14. #define OLED0561_ADD 0x78 // OLED?I2C??(????)
  15. #define COM 0x00 // OLED ??(????)
  16. #define DAT 0x40 // OLED ??(????)
  17. void WriteCmd(unsigned char I2C_Command);//???
  18. void WriteDat(unsigned char I2C_Data);//???
  19. void OLED_Init(void);//???
  20. void OLED_SetPos(unsigned char x, unsigned char y);
  21. void OLED_Fill(unsigned char fill_Data);//????
  22. void OLED_CLS(void);
  23. void OLED_ON(void);
  24. void OLED_OFF(void);
  25. void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize);//?????
  26. void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N);//????
  27. void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]);//??????(??????)
  28. void OLED_ShowCN_STR(u8 x , u8 y , u8 begin , u8 num); //???????????
  29. void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size);
  30. u32 oled_pow(u8 m,u8 n);
  31. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2);//size2(16|12)
  32. void OLED_DrawGIF(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1, unsigned char k, int m, unsigned char GIF[][m]);
  33. #endif

main.c代码:

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "i2c.h"
  22. #include "tim.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "oled.h"
  27. /* USER CODE END Includes */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */
  36. /* USER CODE END PM */
  37. /* Private variables ---------------------------------------------------------*/
  38. /* USER CODE BEGIN PV */
  39. unsigned char BMP1[] =
  40. {0x20,0x20,0x20,0xBC,0xA0,0xA0,0xA0,0xBF,0xA4,0xA4,0xA4,0xA4,0x24,0x20,0x20,0x00,/*"?",0*/
  41. 0x00,0x00,0x00,0xFF,0x0A,0x0A,0x0A,0x0A,0x0A,0x4A,0x8A,0x7F,0x00,0x00,0x00,0x00,/*"?",0*/
  42. 0x10,0x88,0xC4,0x33,0x04,0xF4,0x94,0x94,0xF4,0x9F,0xF4,0x94,0x94,0xF4,0x04,0x00,
  43. 0x01,0x00,0xFF,0x00,0x42,0x32,0x02,0x72,0x82,0x86,0x9A,0x82,0xE2,0x0A,0x32,0x00,/*"?",1*/
  44. 0x00,0x04,0x04,0x04,0xFF,0x54,0x54,0x54,0x54,0x54,0xFF,0x04,0x04,0x04,0x00,0x00,
  45. 0x11,0x11,0x89,0x85,0x93,0x91,0x91,0xFD,0x91,0x91,0x93,0x85,0x89,0x11,0x11,0x00,/*"?",2*/
  46. 0x10,0x60,0x00,0xFC,0x04,0x04,0xE4,0xA4,0x25,0x26,0xA4,0x24,0xE4,0x04,0x04,0x00,
  47. 0x84,0x42,0x31,0x8F,0x40,0x30,0x0F,0x10,0x09,0x06,0x19,0x00,0x3F,0x40,0xF0,0x00,/*"?",3*/
  48. 0x40,0x22,0x14,0x08,0xF4,0x02,0x00,0x04,0x04,0x04,0xFC,0x04,0x04,0x04,0x00,0x00,
  49. 0x08,0x44,0x82,0x41,0x3F,0x00,0x40,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x40,0x00,/*"?",4*/
  50. 0x00,0x00,0x00,0xBE,0x2A,0x2A,0x2A,0xEA,0x2A,0x2A,0x2A,0x3E,0x00,0x00,0x00,0x00,
  51. 0x00,0x44,0x42,0x49,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x49,0x41,0x40,0x00,0x00,/*"?",5*/
  52. 0x00,0x04,0xFF,0x24,0x24,0x24,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00,
  53. 0x88,0x48,0x2F,0x09,0x09,0x19,0xAF,0x48,0x30,0x0F,0x02,0x42,0x82,0x7F,0x00,0x00,/*"?",6*/
  54. 0x00,0xFC,0x04,0x04,0x04,0xFC,0x04,0x04,0x04,0xFC,0x04,0x04,0x04,0xFC,0x00,0x00,
  55. 0x00,0x7F,0x28,0x24,0x23,0x20,0x20,0x20,0x20,0x21,0x22,0x22,0x22,0x7F,0x00,0x00};/*"?",7*/
  56. /* USER CODE END PV */
  57. /* Private function prototypes -----------------------------------------------*/
  58. void SystemClock_Config(void);
  59. /* USER CODE BEGIN PFP */
  60. /* USER CODE END PFP */
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63. /* USER CODE END 0 */
  64. /**
  65. * @brief The application entry point.
  66. * @retval int
  67. */
  68. int main(void)
  69. {
  70. /* USER CODE BEGIN 1 */
  71. /* USER CODE END 1 */
  72. /* MCU Configuration--------------------------------------------------------*/
  73. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  74. HAL_Init();
  75. /* USER CODE BEGIN Init */
  76. /* USER CODE END Init */
  77. /* Configure the system clock */
  78. SystemClock_Config();
  79. /* USER CODE BEGIN SysInit */
  80. /* USER CODE END SysInit */
  81. /* Initialize all configured peripherals */
  82. MX_GPIO_Init();
  83. MX_I2C2_Init();
  84. MX_TIM1_Init();
  85. /* USER CODE BEGIN 2 */
  86. OLED_Init();
  87. OLED_CLS();
  88. /* USER CODE END 2 */
  89. /* Infinite loop */
  90. /* USER CODE BEGIN WHILE */
  91. while (1)
  92. {
  93. /* USER CODE END WHILE */
  94. //OLED_ShowStr(20,3,"hello world",2);
  95. OLED_ShowCN_STR(0,1,0,8);
  96. OLED_ShowStr(30,5,"632207060313",1);
  97. //OLED_DrawBMP(30,0,89,8,BMP1);
  98. //OLED_DrawGIF(30,2,78,8,12,294,BMP2);
  99. /* USER CODE BEGIN 3 */
  100. }
  101. /* USER CODE END 3 */
  102. }
  103. /**
  104. * @brief System Clock Configuration
  105. * @retval None
  106. */
  107. void SystemClock_Config(void)
  108. {
  109. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  110. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  111. /** Initializes the RCC Oscillators according to the specified parameters
  112. * in the RCC_OscInitTypeDef structure.
  113. */
  114. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  115. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  116. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  117. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  118. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  119. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  120. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  121. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  122. {
  123. Error_Handler();
  124. }
  125. /** Initializes the CPU, AHB and APB buses clocks
  126. */
  127. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  128. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  129. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  130. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  131. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  132. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  133. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. }
  138. /* USER CODE BEGIN 4 */
  139. /* USER CODE END 4 */
  140. /**
  141. * @brief This function is executed in case of error occurrence.
  142. * @retval None
  143. */
  144. void Error_Handler(void)
  145. {
  146. /* USER CODE BEGIN Error_Handler_Debug */
  147. /* User can add his own implementation to report the HAL error return state */
  148. __disable_irq();
  149. while (1)
  150. {
  151. }
  152. /* USER CODE END Error_Handler_Debug */
  153. }
  154. #ifdef USE_FULL_ASSERT
  155. /**
  156. * @brief Reports the name of the source file and the source line number
  157. * where the assert_param error has occurred.
  158. * @param file: pointer to the source file name
  159. * @param line: assert_param error line source number
  160. * @retval None
  161. */
  162. void assert_failed(uint8_t *file, uint32_t line)
  163. {
  164. /* USER CODE BEGIN 6 */
  165. /* User can add his own implementation to report the file name and line number,
  166. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  167. /* USER CODE END 6 */
  168. }
  169. #endif /* USE_FULL_ASSERT */

其中char BMP1[]为我们生成的中文汉字。生成中文汉字我们使用到工具PCtoLCD2002。

在选项中将其定义为C51格式。然后生成字模并保存,将其改为字符串

并且我们需要将oled.c文件下生成的asr.h文件中的部分代码修改为我们的汉字字模,如图

2.实验效果

参考博客:

IIC通讯协议详解(电路和时序图)_iic通信协议-CSDN博客

基于STM32移植U8g2图形库——OLED显示(HAL库)_stm32 u8g2-CSDN博客

【强烈推荐】基于stm32的OLED各种显示实现(含动态图)_stm32oled以十六进制显示-CSDN博客

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/726831
推荐阅读
相关标签
  

闽ICP备14008679号