• STM32 OLED屏幕显示详解


    目录

    1.OLED介绍

    2.OLED如何显示一个点?

    内存管理​编辑​编辑

    页地址模式

    水平地址模式​编辑

    垂直地址模式

    ​编辑

    3.OLED显示图片

    用到的库函数:

    向OLED写命令的封装:

    显示图片代码示例:


    1.OLED介绍

    OLED是有机发光管(Organic Light-Emitting Diode)的缩写,是一种新兴的平面显示屏技术。与传统的LCD显示屏相比,OLED显示屏具有自发光、广视角、高对比度、低功耗、快速响应和可用于绕曲性面板等优点。OLED屏幕可以自发光,所以不需要背光灯,使得屏幕更薄且显示效果更优。常见的OLED屏幕有蓝色、黄色、白色等几种。一个常见的OLED屏幕的大小为0.96寸,像素点为128*64,所以被称为0.96 OLED屏或者12864屏。

    2.OLED如何显示一个点?

    有三种,分别为页地址模式,水平地址模式和垂直地址模式,可以通过一下表格进行配置

    内存管理

    页地址模式

    水平地址模式

    垂直地址模式

    列地址选择

    如果写入0x08(b00001000)会显示什么呢 一个字节负责一个Page的一列显示

    3.OLED显示图片

     用取模软件:

    用到的库函数:

    1. HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c,
    2. uint16_t DevAddress,
    3. uint16_t MemAddress,
    4. uint16_t MemAddSize,
    5. uint8_t *pData,
    6. uint16_t Size,
    7. uint32_t Timeout)

    • 参数一:I2C_HandleTypeDef *hi2c,I2C设备句柄
    • 参数二:uint16_t DevAddress,目标器件的地址,七位地址必须左对齐
    • 参数三:uint16_t MemAddress,目标器件的目标寄存器地址
    • 参数四:uint16_t MemAddSize,目标器件内部寄存器地址数据长度
    • 参数五:uint8_t *pData,待写的数据首地址
    • 参数六:uint16_t Size,待写的数据长度
    • 参数七:uint32_t Timeout,超时时间
    • 返回值:HAL_StatusTypeDef,HAL状态(OK,busy,ERROR,TIMEOUT)

    向OLED写命令的封装:

    1. void Oled_Write_Cmd(uint8_t dataCmd)
    2. {
    3. HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,&dataCmd, 1, 0xff);
    4. }

    向OLED写数据的封装:

    1. void Oled_Write_Data(uint8_t dataData)
    2. {
    3. HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,&dataData, 1, 0xff);
    4. }

    接线:

    •  SCL -- PB6
    •  SDA -- PB7

    显示图片代码示例:

    1. #include "main.h"
    2. #include "i2c.h"
    3. #include "gpio.h"
    4. void Oled_Write_Cmd(uint8_t dataCmd)
    5. {
    6. HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT,
    7. &dataCmd, 1, 0xff);
    8. }
    9. void Oled_Write_Data(uint8_t dataData)
    10. {
    11. HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT,
    12. &dataData, 1, 0xff);
    13. }
    14. void Oled_Init(void){
    15. Oled_Write_Cmd(0xAE);//--display off
    16. Oled_Write_Cmd(0x00);//---set low column address
    17. Oled_Write_Cmd(0x10);//---set high column address
    18. Oled_Write_Cmd(0x40);//--set start line address
    19. Oled_Write_Cmd(0xB0);//--set page address
    20. Oled_Write_Cmd(0x81); // contract control
    21. Oled_Write_Cmd(0xFF);//--128
    22. Oled_Write_Cmd(0xA1);//set segment remap
    23. Oled_Write_Cmd(0xA6);//--normal / reverse
    24. Oled_Write_Cmd(0xA8);//--set multiplex ratio(1 to 64)
    25. Oled_Write_Cmd(0x3F);//--1/32 duty
    26. Oled_Write_Cmd(0xC8);//Com scan direction
    27. Oled_Write_Cmd(0xD3);//-set display offset
    28. Oled_Write_Cmd(0x00);//
    29. Oled_Write_Cmd(0xD5);//set osc division
    30. Oled_Write_Cmd(0x80);//
    31. Oled_Write_Cmd(0xD8);//set area color mode off
    32. Oled_Write_Cmd(0x05);//
    33. Oled_Write_Cmd(0xD9);//Set Pre-Charge Period
    34. Oled_Write_Cmd(0xF1);//
    35. Oled_Write_Cmd(0xDA);//set com pin configuartion
    36. Oled_Write_Cmd(0x12);//
    37. Oled_Write_Cmd(0xDB);//set Vcomh
    38. Oled_Write_Cmd(0x30);//
    39. Oled_Write_Cmd(0x8D);//set charge pump enable
    40. Oled_Write_Cmd(0x14);//
    41. Oled_Write_Cmd(0xAF);//--turn on oled panel
    42. }
    43. void Oled_Screen_Clear(void){
    44. char i,n;
    45. Oled_Write_Cmd (0x20); //set memory addressing mode
    46. Oled_Write_Cmd (0x02); //page addressing mode
    47. for(i=0;i<8;i++){
    48. Oled_Write_Cmd(0xb0+i);
    49. Oled_Write_Cmd(0x00);
    50. Oled_Write_Cmd(0x10);
    51. for(n=0;n<128;n++)Oled_Write_Data(0x00);
    52. }
    53. }
    54. //用取模软件生成
    55. unsigned char bmpImager[] = {
    56. /*-- 调入了一幅图像:D:\无标题.bmp --*/
    57. /*-- 宽度x高度=128x64 --128x8x8*/
    58. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    59. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    60. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    61. 0x00,0x00,0xF0,0x08,0x0C,0x04,0x06,0x06,0x0C,0x04,0x0C,0xFC,0x1C,0x74,0xFC,0xF8,
    62. 0xF0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    63. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    64. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    65. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    66. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    67. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    68. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    69. 0x00,0x00,0x01,0x07,0x04,0x88,0xF8,0x08,0x08,0x0C,0x06,0x01,0x00,0x00,0x01,0x1F,
    70. 0x7F,0xFF,0xDC,0xF8,0xE0,0xC0,0x40,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    71. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    72. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    73. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    74. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    75. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    76. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x10,0x18,0x08,0x0C,
    77. 0x04,0x04,0x06,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,
    78. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    79. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    80. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    81. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    82. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    83. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    84. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x60,0xC0,0x80,0x80,
    85. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xE0,0x00,0x00,0x00,0x00,
    86. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    87. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    88. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    89. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    90. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    91. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    92. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    93. 0x03,0x06,0x1C,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0xFC,0x00,0x00,
    94. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x88,0xE8,0x38,0x0E,0x09,0x08,
    95. 0x08,0x88,0xE8,0x18,0x08,0x08,0x08,0x00,0x00,0xFF,0x89,0x89,0x89,0xFF,0x00,0xFF,
    96. 0x89,0x89,0x89,0x89,0xFF,0x00,0x00,0x04,0x04,0x84,0x74,0x6F,0xA4,0x24,0x24,0x24,
    97. 0x24,0xA4,0x64,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    98. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    99. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    100. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    101. 0x00,0x80,0xF0,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x7F,0x00,0x00,
    102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x08,0x09,0x09,0x06,0x06,
    103. 0x06,0x05,0x08,0x08,0x10,0x10,0x00,0x00,0x0C,0x03,0x10,0x10,0x10,0x1F,0x18,0x07,
    104. 0x00,0x00,0x10,0x10,0x1F,0x00,0x10,0x08,0x06,0x11,0x10,0x08,0x09,0x0A,0x06,0x06,
    105. 0x0B,0x08,0x10,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    106. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    107. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    108. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,
    109. 0x1E,0x03,0x00,0x00,0xC0,0x60,0x30,0x0C,0x04,0x06,0x02,0x01,0x01,0x00,0x00,0x00,
    110. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    111. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    113. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    115. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    116. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
    117. 0x1E,0x60,0x78,0x0F,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    118. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    119. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    120. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    121. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    122. };
    123. void Oled_Show_Image(unsigned char *image)
    124. {
    125. unsigned char i;
    126. unsigned int j;
    127. for(i=0;i<8;i++){
    128. Oled_Write_Cmd(0xB0 + i);//page0--page7
    129. //每个page从0列
    130. Oled_Write_Cmd(0x00);
    131. Oled_Write_Cmd(0x10);
    132. //0到127列,依次写入0,每写入数据,列地址自动偏移
    133. for(j = 128 * i; j<(128 * (i+1));j++){
    134. Oled_Write_Data(image[j]);
    135. }
    136. }
    137. }
    138. int main(void)
    139. {
    140. /* USER CODE BEGIN 1 */
    141. /* USER CODE END 1 */
    142. /* MCU Configuration--------------------------------------------------------*/
    143. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    144. HAL_Init();
    145. /* USER CODE BEGIN Init */
    146. /* USER CODE END Init */
    147. /* Configure the system clock */
    148. SystemClock_Config();
    149. /* USER CODE BEGIN SysInit */
    150. /* USER CODE END SysInit */
    151. /* Initialize all configured peripherals */
    152. MX_GPIO_Init();
    153. MX_I2C1_Init();
    154. /* USER CODE BEGIN 2 */
    155. //1. OLED初始化
    156. Oled_Init();
    157. //2. 选择一个位置
    158. //2.1 确认页寻址模式
    159. Oled_Write_Cmd(0x20);
    160. Oled_Write_Cmd(0x02);
    161. Oled_Screen_Clear();
    162. Oled_Show_Image(bmpImager);
    163. /* USER CODE END 2 */
    164. /* Infinite loop */
    165. /* USER CODE BEGIN WHILE */
    166. while (1)
    167. {
    168. /* USER CODE END WHILE */
    169. /* USER CODE BEGIN 3 */
    170. }
    171. /* USER CODE END 3 */
    172. }

  • 相关阅读:
    新版Testwell CTC++带来哪些新变化?
    【Linux】第十三站:进程状态
    11. 字符串
    【uniapp】微信小程序canvas签名旋转生成图片
    看板任务管理,让你分分钟提高工作效率!
    lombok插件各个注解介绍
    JimuReport积木报表 v1.7.2 版本发布,低代码报表工具
    操作系统:进程与线程(二)同步与互斥B
    详解大数据、数据存储和边缘计算技术在元宇宙中的应用
    Jmeter对服务器资源的监控
  • 原文地址:https://blog.csdn.net/m0_74712453/article/details/132891942