STM32 OLED

[复制链接]
发表于 2026-1-12 21:52:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

前言

提示:这里可以添加本文要记载的大概内容:
项目必要:

提示:以下是本篇文章正文内容,下面案例可供参考
一、OLED是什么?

示例:
OLED(Organic Light Emitting Diode):有机发光二极管
OLED表现屏:性能良好的新型表现屏,具有功耗低、相应速率快、宽视角、浮滑柔韧等特点
0.96寸OLED模块:小巧玲珑、占用接口少、简单易用,是电子计划中非常常见的表现屏模块
供电:3~5.5V,通讯协议:I2C/SPI,分辨率:128*64

二、利用步调

2.1复制 OLED.C .H文件



添加到工程中

2.1.1 遇到题目

…\HARDWER\OLED.c(13): error: #268: declaration may not appear after executable statement in block

2.2同一风格

2.2.1 OLED.H

代码如下(示例):
  1. #ifndef __OLED_H
  2. #define __OLED_H
  3. #include "stm32f10x.h"
  4. #include "OLED_Font.h"
  5. void OLED_Init(void);
  6. void OLED_Clear(void);
  7. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
  8. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
  9. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  10. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
  11. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  12. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  13. #endif
复制代码
2.2.2 OLED.C

  1. #include <OLED.h>
  2. /*引脚配置*/
  3. #define OLED_W_SCL(x)                GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
  4. #define OLED_W_SDA(x)                GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
  5. /*引脚初始化*/
  6. void OLED_I2C_Init(void)
  7. {
  8.            GPIO_InitTypeDef GPIO_InitStructure;
  9.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  10.        
  11.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  12.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  14.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  15.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  16.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  17.        
  18.         OLED_W_SCL(1);
  19.         OLED_W_SDA(1);
  20. }
  21. /**
  22.   * @brief  I2C开始
  23.   * @param  无
  24.   * @retval 无
  25.   */
  26. void OLED_I2C_Start(void)
  27. {
  28.         OLED_W_SDA(1);
  29.         OLED_W_SCL(1);
  30.         OLED_W_SDA(0);
  31.         OLED_W_SCL(0);
  32. }
  33. /**
  34.   * @brief  I2C停止
  35.   * @param  无
  36.   * @retval 无
  37.   */
  38. void OLED_I2C_Stop(void)
  39. {
  40.         OLED_W_SDA(0);
  41.         OLED_W_SCL(1);
  42.         OLED_W_SDA(1);
  43. }
  44. /**
  45.   * @brief  I2C发送一个字节
  46.   * @param  Byte 要发送的一个字节
  47.   * @retval 无
  48.   */
  49. void OLED_I2C_SendByte(uint8_t Byte)
  50. {
  51.         uint8_t i;
  52.         for (i = 0; i < 8; i++)
  53.         {
  54.                 OLED_W_SDA(Byte & (0x80 >> i));
  55.                 OLED_W_SCL(1);
  56.                 OLED_W_SCL(0);
  57.         }
  58.         OLED_W_SCL(1);        //额外的一个时钟,不处理应答信号
  59.         OLED_W_SCL(0);
  60. }
  61. /**
  62.   * @brief  OLED写命令
  63.   * @param  Command 要写入的命令
  64.   * @retval 无
  65.   */
  66. void OLED_WriteCommand(uint8_t Command)
  67. {
  68.         OLED_I2C_Start();
  69.         OLED_I2C_SendByte(0x78);                //从机地址
  70.         OLED_I2C_SendByte(0x00);                //写命令
  71.         OLED_I2C_SendByte(Command);
  72.         OLED_I2C_Stop();
  73. }
  74. /**
  75.   * @brief  OLED写数据
  76.   * @param  Data 要写入的数据
  77.   * @retval 无
  78.   */
  79. void OLED_WriteData(uint8_t Data)
  80. {
  81.         OLED_I2C_Start();
  82.         OLED_I2C_SendByte(0x78);                //从机地址
  83.         OLED_I2C_SendByte(0x40);                //写数据
  84.         OLED_I2C_SendByte(Data);
  85.         OLED_I2C_Stop();
  86. }
  87. /**
  88.   * @brief  OLED设置光标位置
  89.   * @param  Y 以左上角为原点,向下方向的坐标,范围:0~7
  90.   * @param  X 以左上角为原点,向右方向的坐标,范围:0~127
  91.   * @retval 无
  92.   */
  93. void OLED_SetCursor(uint8_t Y, uint8_t X)
  94. {
  95.         OLED_WriteCommand(0xB0 | Y);                                        //设置Y位置
  96.         OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4));        //设置X位置高4位
  97.         OLED_WriteCommand(0x00 | (X & 0x0F));                        //设置X位置低4位
  98. }
  99. /**
  100.   * @brief  OLED清屏
  101.   * @param  无
  102.   * @retval 无
  103.   */
  104. void OLED_Clear(void)
  105. {  
  106.         uint8_t i, j;
  107.         for (j = 0; j < 8; j++)
  108.         {
  109.                 OLED_SetCursor(j, 0);
  110.                 for(i = 0; i < 128; i++)
  111.                 {
  112.                         OLED_WriteData(0x00);
  113.                 }
  114.         }
  115. }
  116. /**
  117.   * @brief  OLED显示一个字符
  118.   * @param  Line 行位置,范围:1~4
  119.   * @param  Column 列位置,范围:1~16
  120.   * @param  Char 要显示的一个字符,范围:ASCII可见字符
  121.   * @retval 无
  122.   */
  123. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
  124. {             
  125.         uint8_t i;
  126.         OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8);                //设置光标位置在上半部分
  127.         for (i = 0; i < 8; i++)
  128.         {
  129.                 OLED_WriteData(OLED_F8x16[Char - ' '][i]);                        //显示上半部分内容
  130.         }
  131.         OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8);        //设置光标位置在下半部分
  132.         for (i = 0; i < 8; i++)
  133.         {
  134.                 OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]);                //显示下半部分内容
  135.         }
  136. }
  137. /**
  138.   * @brief  OLED显示字符串
  139.   * @param  Line 起始行位置,范围:1~4
  140.   * @param  Column 起始列位置,范围:1~16
  141.   * @param  String 要显示的字符串,范围:ASCII可见字符
  142.   * @retval 无
  143.   */
  144. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
  145. {
  146.         uint8_t i;
  147.         for (i = 0; String[i] != '\0'; i++)
  148.         {
  149.                 OLED_ShowChar(Line, Column + i, String[i]);
  150.         }
  151. }
  152. /**
  153.   * @brief  OLED次方函数
  154.   * @retval 返回值等于X的Y次方
  155.   */
  156. uint32_t OLED_Pow(uint32_t X, uint32_t Y)
  157. {
  158.         uint32_t Result = 1;
  159.         while (Y--)
  160.         {
  161.                 Result *= X;
  162.         }
  163.         return Result;
  164. }
  165. /**
  166.   * @brief  OLED显示数字(十进制,正数)
  167.   * @param  Line 起始行位置,范围:1~4
  168.   * @param  Column 起始列位置,范围:1~16
  169.   * @param  Number 要显示的数字,范围:0~4294967295
  170.   * @param  Length 要显示数字的长度,范围:1~10
  171.   * @retval 无
  172.   */
  173. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  174. {
  175.         uint8_t i;
  176.         for (i = 0; i < Length; i++)                                                       
  177.         {
  178.                 OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
  179.         }
  180. }
  181. /**
  182.   * @brief  OLED显示数字(十进制,带符号数)
  183.   * @param  Line 起始行位置,范围:1~4
  184.   * @param  Column 起始列位置,范围:1~16
  185.   * @param  Number 要显示的数字,范围:-2147483648~2147483647
  186.   * @param  Length 要显示数字的长度,范围:1~10
  187.   * @retval 无
  188.   */
  189. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
  190. {
  191.         uint8_t i;
  192.         uint32_t Number1;
  193.         if (Number >= 0)
  194.         {
  195.                 OLED_ShowChar(Line, Column, '+');
  196.                 Number1 = Number;
  197.         }
  198.         else
  199.         {
  200.                 OLED_ShowChar(Line, Column, '-');
  201.                 Number1 = -Number;
  202.         }
  203.         for (i = 0; i < Length; i++)                                                       
  204.         {
  205.                 OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
  206.         }
  207. }
  208. /**
  209.   * @brief  OLED显示数字(十六进制,正数)
  210.   * @param  Line 起始行位置,范围:1~4
  211.   * @param  Column 起始列位置,范围:1~16
  212.   * @param  Number 要显示的数字,范围:0~0xFFFFFFFF
  213.   * @param  Length 要显示数字的长度,范围:1~8
  214.   * @retval 无
  215.   */
  216. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  217. {
  218.         uint8_t i, SingleNumber;
  219.         for (i = 0; i < Length; i++)                                                       
  220.         {
  221.                 SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
  222.                 if (SingleNumber < 10)
  223.                 {
  224.                         OLED_ShowChar(Line, Column + i, SingleNumber + '0');
  225.                 }
  226.                 else
  227.                 {
  228.                         OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
  229.                 }
  230.         }
  231. }
  232. /**
  233.   * @brief  OLED显示数字(二进制,正数)
  234.   * @param  Line 起始行位置,范围:1~4
  235.   * @param  Column 起始列位置,范围:1~16
  236.   * @param  Number 要显示的数字,范围:0~1111 1111 1111 1111
  237.   * @param  Length 要显示数字的长度,范围:1~16
  238.   * @retval 无
  239.   */
  240. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  241. {
  242.         uint8_t i;
  243.         for (i = 0; i < Length; i++)                                                       
  244.         {
  245.                 OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
  246.         }
  247. }
  248. /**
  249.   * @brief  OLED初始化
  250.   * @param  无
  251.   * @retval 无
  252.   */
  253. void OLED_Init(void)
  254. {
  255.         uint32_t i, j;
  256.        
  257.         for (i = 0; i < 1000; i++)                        //上电延时
  258.         {
  259.                 for (j = 0; j < 1000; j++);
  260.         }
  261.        
  262.         OLED_I2C_Init();                        //端口初始化
  263.        
  264.         OLED_WriteCommand(0xAE);        //关闭显示
  265.        
  266.         OLED_WriteCommand(0xD5);        //设置显示时钟分频比/振荡器频率
  267.         OLED_WriteCommand(0x80);
  268.        
  269.         OLED_WriteCommand(0xA8);        //设置多路复用率
  270.         OLED_WriteCommand(0x3F);
  271.        
  272.         OLED_WriteCommand(0xD3);        //设置显示偏移
  273.         OLED_WriteCommand(0x00);
  274.        
  275.         OLED_WriteCommand(0x40);        //设置显示开始行
  276.        
  277.         OLED_WriteCommand(0xA1);        //设置左右方向,0xA1正常 0xA0左右反置
  278.        
  279.         OLED_WriteCommand(0xC8);        //设置上下方向,0xC8正常 0xC0上下反置
  280.         OLED_WriteCommand(0xDA);        //设置COM引脚硬件配置
  281.         OLED_WriteCommand(0x12);
  282.        
  283.         OLED_WriteCommand(0x81);        //设置对比度控制
  284.         OLED_WriteCommand(0xCF);
  285.         OLED_WriteCommand(0xD9);        //设置预充电周期
  286.         OLED_WriteCommand(0xF1);
  287.         OLED_WriteCommand(0xDB);        //设置VCOMH取消选择级别
  288.         OLED_WriteCommand(0x30);
  289.         OLED_WriteCommand(0xA4);        //设置整个显示打开/关闭
  290.         OLED_WriteCommand(0xA6);        //设置正常/倒转显示
  291.         OLED_WriteCommand(0x8D);        //设置充电泵
  292.         OLED_WriteCommand(0x14);
  293.         OLED_WriteCommand(0xAF);        //开启显示
  294.                
  295.         OLED_Clear();                                //OLED清屏
  296. }
复制代码
2. 3.主函数引用头文件


2.3.1 oled.h 提供了什么函数

.\Objects\text1.axf: Error: L6200E: Symbol OLED_F8x16 multiply defined (by oled.o and main.o).
2.4 修改OLED.c

  1. #include <OLED.h>
  2. #include "OLED_Font.h"
  3. /*引脚配置*/
  4. #define OLED_W_SCL(x)                GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
  5. #define OLED_W_SDA(x)                GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
  6. /*引脚初始化*/
  7. void OLED_I2C_Init(void)
  8. {
  9.            GPIO_InitTypeDef GPIO_InitStructure;
  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  11.        
  12.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  13.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  15.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  16.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  17.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  18.        
  19.         OLED_W_SCL(1);
  20.         OLED_W_SDA(1);
  21. }
  22. /**
  23.   * @brief  I2C开始
  24.   * @param  无
  25.   * @retval 无
  26.   */
  27. void OLED_I2C_Start(void)
  28. {
  29.         OLED_W_SDA(1);
  30.         OLED_W_SCL(1);
  31.         OLED_W_SDA(0);
  32.         OLED_W_SCL(0);
  33. }
  34. /**
  35.   * @brief  I2C停止
  36.   * @param  无
  37.   * @retval 无
  38.   */
  39. void OLED_I2C_Stop(void)
  40. {
  41.         OLED_W_SDA(0);
  42.         OLED_W_SCL(1);
  43.         OLED_W_SDA(1);
  44. }
  45. /**
  46.   * @brief  I2C发送一个字节
  47.   * @param  Byte 要发送的一个字节
  48.   * @retval 无
  49.   */
  50. void OLED_I2C_SendByte(uint8_t Byte)
  51. {
  52.         uint8_t i;
  53.         for (i = 0; i < 8; i++)
  54.         {
  55.                 OLED_W_SDA(Byte & (0x80 >> i));
  56.                 OLED_W_SCL(1);
  57.                 OLED_W_SCL(0);
  58.         }
  59.         OLED_W_SCL(1);        //额外的一个时钟,不处理应答信号
  60.         OLED_W_SCL(0);
  61. }
  62. /**
  63.   * @brief  OLED写命令
  64.   * @param  Command 要写入的命令
  65.   * @retval 无
  66.   */
  67. void OLED_WriteCommand(uint8_t Command)
  68. {
  69.         OLED_I2C_Start();
  70.         OLED_I2C_SendByte(0x78);                //从机地址
  71.         OLED_I2C_SendByte(0x00);                //写命令
  72.         OLED_I2C_SendByte(Command);
  73.         OLED_I2C_Stop();
  74. }
  75. /**
  76.   * @brief  OLED写数据
  77.   * @param  Data 要写入的数据
  78.   * @retval 无
  79.   */
  80. void OLED_WriteData(uint8_t Data)
  81. {
  82.         OLED_I2C_Start();
  83.         OLED_I2C_SendByte(0x78);                //从机地址
  84.         OLED_I2C_SendByte(0x40);                //写数据
  85.         OLED_I2C_SendByte(Data);
  86.         OLED_I2C_Stop();
  87. }
  88. /**
  89.   * @brief  OLED设置光标位置
  90.   * @param  Y 以左上角为原点,向下方向的坐标,范围:0~7
  91.   * @param  X 以左上角为原点,向右方向的坐标,范围:0~127
  92.   * @retval 无
  93.   */
  94. void OLED_SetCursor(uint8_t Y, uint8_t X)
  95. {
  96.         OLED_WriteCommand(0xB0 | Y);                                        //设置Y位置
  97.         OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4));        //设置X位置高4位
  98.         OLED_WriteCommand(0x00 | (X & 0x0F));                        //设置X位置低4位
  99. }
  100. /**
  101.   * @brief  OLED清屏
  102.   * @param  无
  103.   * @retval 无
  104.   */
  105. void OLED_Clear(void)
  106. {  
  107.         uint8_t i, j;
  108.         for (j = 0; j < 8; j++)
  109.         {
  110.                 OLED_SetCursor(j, 0);
  111.                 for(i = 0; i < 128; i++)
  112.                 {
  113.                         OLED_WriteData(0x00);
  114.                 }
  115.         }
  116. }
  117. /**
  118.   * @brief  OLED显示一个字符
  119.   * @param  Line 行位置,范围:1~4
  120.   * @param  Column 列位置,范围:1~16
  121.   * @param  Char 要显示的一个字符,范围:ASCII可见字符
  122.   * @retval 无
  123.   */
  124. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
  125. {             
  126.         uint8_t i;
  127.         OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8);                //设置光标位置在上半部分
  128.         for (i = 0; i < 8; i++)
  129.         {
  130.                 OLED_WriteData(OLED_F8x16[Char - ' '][i]);                        //显示上半部分内容
  131.         }
  132.         OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8);        //设置光标位置在下半部分
  133.         for (i = 0; i < 8; i++)
  134.         {
  135.                 OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]);                //显示下半部分内容
  136.         }
  137. }
  138. /**
  139.   * @brief  OLED显示字符串
  140.   * @param  Line 起始行位置,范围:1~4
  141.   * @param  Column 起始列位置,范围:1~16
  142.   * @param  String 要显示的字符串,范围:ASCII可见字符
  143.   * @retval 无
  144.   */
  145. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
  146. {
  147.         uint8_t i;
  148.         for (i = 0; String[i] != '\0'; i++)
  149.         {
  150.                 OLED_ShowChar(Line, Column + i, String[i]);
  151.         }
  152. }
  153. /**
  154.   * @brief  OLED次方函数
  155.   * @retval 返回值等于X的Y次方
  156.   */
  157. uint32_t OLED_Pow(uint32_t X, uint32_t Y)
  158. {
  159.         uint32_t Result = 1;
  160.         while (Y--)
  161.         {
  162.                 Result *= X;
  163.         }
  164.         return Result;
  165. }
  166. /**
  167.   * @brief  OLED显示数字(十进制,正数)
  168.   * @param  Line 起始行位置,范围:1~4
  169.   * @param  Column 起始列位置,范围:1~16
  170.   * @param  Number 要显示的数字,范围:0~4294967295
  171.   * @param  Length 要显示数字的长度,范围:1~10
  172.   * @retval 无
  173.   */
  174. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  175. {
  176.         uint8_t i;
  177.         for (i = 0; i < Length; i++)                                                       
  178.         {
  179.                 OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
  180.         }
  181. }
  182. /**
  183.   * @brief  OLED显示数字(十进制,带符号数)
  184.   * @param  Line 起始行位置,范围:1~4
  185.   * @param  Column 起始列位置,范围:1~16
  186.   * @param  Number 要显示的数字,范围:-2147483648~2147483647
  187.   * @param  Length 要显示数字的长度,范围:1~10
  188.   * @retval 无
  189.   */
  190. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
  191. {
  192.         uint8_t i;
  193.         uint32_t Number1;
  194.         if (Number >= 0)
  195.         {
  196.                 OLED_ShowChar(Line, Column, '+');
  197.                 Number1 = Number;
  198.         }
  199.         else
  200.         {
  201.                 OLED_ShowChar(Line, Column, '-');
  202.                 Number1 = -Number;
  203.         }
  204.         for (i = 0; i < Length; i++)                                                       
  205.         {
  206.                 OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
  207.         }
  208. }
  209. /**
  210.   * @brief  OLED显示数字(十六进制,正数)
  211.   * @param  Line 起始行位置,范围:1~4
  212.   * @param  Column 起始列位置,范围:1~16
  213.   * @param  Number 要显示的数字,范围:0~0xFFFFFFFF
  214.   * @param  Length 要显示数字的长度,范围:1~8
  215.   * @retval 无
  216.   */
  217. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  218. {
  219.         uint8_t i, SingleNumber;
  220.         for (i = 0; i < Length; i++)                                                       
  221.         {
  222.                 SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
  223.                 if (SingleNumber < 10)
  224.                 {
  225.                         OLED_ShowChar(Line, Column + i, SingleNumber + '0');
  226.                 }
  227.                 else
  228.                 {
  229.                         OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
  230.                 }
  231.         }
  232. }
  233. /**
  234.   * @brief  OLED显示数字(二进制,正数)
  235.   * @param  Line 起始行位置,范围:1~4
  236.   * @param  Column 起始列位置,范围:1~16
  237.   * @param  Number 要显示的数字,范围:0~1111 1111 1111 1111
  238.   * @param  Length 要显示数字的长度,范围:1~16
  239.   * @retval 无
  240.   */
  241. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
  242. {
  243.         uint8_t i;
  244.         for (i = 0; i < Length; i++)                                                       
  245.         {
  246.                 OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
  247.         }
  248. }
  249. /**
  250.   * @brief  OLED初始化
  251.   * @param  无
  252.   * @retval 无
  253.   */
  254. void OLED_Init(void)
  255. {
  256.         uint32_t i, j;
  257.        
  258.         for (i = 0; i < 1000; i++)                        //上电延时
  259.         {
  260.                 for (j = 0; j < 1000; j++);
  261.         }
  262.        
  263.         OLED_I2C_Init();                        //端口初始化
  264.        
  265.         OLED_WriteCommand(0xAE);        //关闭显示
  266.        
  267.         OLED_WriteCommand(0xD5);        //设置显示时钟分频比/振荡器频率
  268.         OLED_WriteCommand(0x80);
  269.        
  270.         OLED_WriteCommand(0xA8);        //设置多路复用率
  271.         OLED_WriteCommand(0x3F);
  272.        
  273.         OLED_WriteCommand(0xD3);        //设置显示偏移
  274.         OLED_WriteCommand(0x00);
  275.        
  276.         OLED_WriteCommand(0x40);        //设置显示开始行
  277.        
  278.         OLED_WriteCommand(0xA1);        //设置左右方向,0xA1正常 0xA0左右反置
  279.        
  280.         OLED_WriteCommand(0xC8);        //设置上下方向,0xC8正常 0xC0上下反置
  281.         OLED_WriteCommand(0xDA);        //设置COM引脚硬件配置
  282.         OLED_WriteCommand(0x12);
  283.        
  284.         OLED_WriteCommand(0x81);        //设置对比度控制
  285.         OLED_WriteCommand(0xCF);
  286.         OLED_WriteCommand(0xD9);        //设置预充电周期
  287.         OLED_WriteCommand(0xF1);
  288.         OLED_WriteCommand(0xDB);        //设置VCOMH取消选择级别
  289.         OLED_WriteCommand(0x30);
  290.         OLED_WriteCommand(0xA4);        //设置整个显示打开/关闭
  291.         OLED_WriteCommand(0xA6);        //设置正常/倒转显示
  292.         OLED_WriteCommand(0x8D);        //设置充电泵
  293.         OLED_WriteCommand(0x14);
  294.         OLED_WriteCommand(0xAF);        //开启显示
  295.                
  296.         OLED_Clear();                                //OLED清屏
  297. }
复制代码
2.5 修改OLED.h

  1. #ifndef __OLED_H
  2. #define __OLED_H
  3. #include "stm32f10x.h"
  4. void OLED_Init(void);
  5. void OLED_Clear(void);
  6. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
  7. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
  8. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  9. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
  10. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  11. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  12. #endif
复制代码
2.6 字库头文件 OLED_Font.h

  1. #ifndef __OLED_FONT_H
  2. #define __OLED_FONT_H
  3. /*OLED瀛楁ā搴擄紝瀹?鍍忕礌锛岄珮16鍍忕礌*/
  4. const uint8_t OLED_F8x16[][16]=
  5. {
  6.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//  0
  8.        
  9.         0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
  10.         0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  11.        
  12.         0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
  13.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  14.        
  15.         0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
  16.         0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  17.        
  18.         0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
  19.         0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  20.        
  21.         0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
  22.         0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  23.        
  24.         0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
  25.         0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  26.        
  27.         0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
  28.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  29.        
  30.         0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
  31.         0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  32.        
  33.         0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
  34.         0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  35.        
  36.         0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
  37.         0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  38.        
  39.         0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
  40.         0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  41.        
  42.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43.         0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  44.        
  45.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46.         0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  47.        
  48.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49.         0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  50.        
  51.         0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
  52.         0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  53.        
  54.         0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  55.         0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  56.        
  57.         0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
  58.         0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  59.        
  60.         0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
  61.         0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  62.        
  63.         0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
  64.         0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  65.        
  66.         0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
  67.         0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  68.        
  69.         0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
  70.         0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  71.        
  72.         0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
  73.         0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  74.        
  75.         0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
  76.         0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  77.        
  78.         0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
  79.         0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  80.        
  81.         0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  82.         0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  83.        
  84.         0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
  85.         0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  86.        
  87.         0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
  88.         0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  89.        
  90.         0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
  91.         0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  92.        
  93.         0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
  94.         0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  95.        
  96.         0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
  97.         0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  98.        
  99.         0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
  100.         0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  101.        
  102.         0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
  103.         0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  104.        
  105.         0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
  106.         0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  107.        
  108.         0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
  109.         0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  110.        
  111.         0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
  112.         0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  113.        
  114.         0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
  115.         0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  116.        
  117.         0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  118.         0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  119.        
  120.         0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  121.         0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  122.        
  123.         0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
  124.         0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  125.        
  126.         0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  127.         0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  128.        
  129.         0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
  130.         0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  131.        
  132.         0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
  133.         0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  134.        
  135.         0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
  136.         0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  137.        
  138.         0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
  139.         0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  140.        
  141.         0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
  142.         0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  143.        
  144.         0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
  145.         0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  146.        
  147.         0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  148.         0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  149.        
  150.         0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
  151.         0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  152.        
  153.         0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  154.         0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  155.        
  156.         0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
  157.         0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  158.        
  159.         0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
  160.         0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  161.        
  162.         0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
  163.         0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  164.        
  165.         0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  166.         0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  167.        
  168.         0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
  169.         0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  170.        
  171.         0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
  172.         0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  173.        
  174.         0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
  175.         0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  176.        
  177.         0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
  178.         0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  179.        
  180.         0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
  181.         0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  182.        
  183.         0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
  184.         0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  185.        
  186.         0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
  187.         0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  188.        
  189.         0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
  190.         0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  191.        
  192.         0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
  193.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  194.        
  195.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  196.         0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  197.        
  198.         0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
  199.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  200.        
  201.         0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  202.         0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  203.        
  204.         0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
  205.         0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  206.        
  207.         0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
  208.         0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  209.        
  210.         0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
  211.         0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  212.        
  213.         0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  214.         0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  215.        
  216.         0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
  217.         0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  218.        
  219.         0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  220.         0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  221.        
  222.         0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
  223.         0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  224.        
  225.         0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
  226.         0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  227.        
  228.         0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
  229.         0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  230.        
  231.         0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
  232.         0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  233.        
  234.         0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
  235.         0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  236.        
  237.         0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  238.         0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  239.        
  240.         0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
  241.         0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  242.        
  243.         0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  244.         0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  245.        
  246.         0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
  247.         0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  248.        
  249.         0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
  250.         0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  251.        
  252.         0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  253.         0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  254.        
  255.         0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  256.         0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  257.        
  258.         0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
  259.         0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  260.        
  261.         0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
  262.         0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  263.        
  264.         0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  265.         0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  266.        
  267.         0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
  268.         0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  269.        
  270.         0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  271.         0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  272.        
  273.         0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  274.         0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  275.        
  276.         0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  277.         0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  278.        
  279.         0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
  280.         0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  281.        
  282.         0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
  283.         0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  284.        
  285.         0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
  286.         0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  287.        
  288.         0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
  289.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
  290. };
  291. #endif
复制代码
三、先容表现一个字符的函数


调用
3.1 main.c调用

  1. #include <stm32f10x.h>
  2. #include <key.h>
  3. #include <OLED.h>
  4. void led_init()
  5. {
  6. //LED 指示用
  7.   GPIO_InitTypeDef GPIO_InitStruct;
  8.        
  9.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  10.        
  11.         GPIO_InitStruct.GPIO_Pin= GPIO_Pin_1;
  12.         GPIO_InitStruct.GPIO_Mode= GPIO_Mode_Out_PP;
  13.         GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;
  14.         GPIO_Init(GPIOA, &GPIO_InitStruct);
  15. }
  16. int main()
  17. {
  18.         u8 temp;
  19.        
  20.         led_init();
  21.         key_init();
  22.        
  23.         OLED_Init();
  24. //        GPIO_InitTypeDef GPIO_InitStruct;
  25. //        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE);
  26. //        //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  27. //        GPIO_InitStruct.GPIO_Pin= GPIO_Pin_5;
  28. //        GPIO_InitStruct.GPIO_Mode= GPIO_Mode_IPU;
  29. //        GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;
  30. //        GPIO_Init(GPIOC, &GPIO_InitStruct);
  31. //       
  32. ////LED 指示用
  33. //       
  34. //        GPIO_InitStruct.GPIO_Pin= GPIO_Pin_1;
  35. //        GPIO_InitStruct.GPIO_Mode= GPIO_Mode_Out_PP;
  36. //        GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;
  37. //        GPIO_Init(GPIOA, &GPIO_InitStruct);
  38. //        OLED_Clear();
  39.        
  40.          OLED_ShowChar(4, 4, '0');
  41.          OLED_ShowString(1, 1, "nihaoma?");
  42.          OLED_ShowNum(2, 1, 54321, 5);
  43.        
  44.        
  45.         while(1)
  46.         {
  47.                        
  48.        
  49. //                temp=GPIO_ReadInputDataBit( GPIOC, GPIO_Pin_5);
  50. //               
  51. //                if(temp==0) {
  52. //               
  53. //                  //led  亮
  54. //                        GPIO_SetBits( GPIOA, GPIO_Pin_1);
  55. //                       
  56. //                }
  57. //                else{
  58. //                       
  59. //                        //led  灭
  60. //                       
  61. //                       
  62. //                }
  63.                
  64.                 temp=KEY_Scan(1);
  65.                 if(temp==0)
  66.                 {
  67.                                 GPIO_SetBits( GPIOA, GPIO_Pin_1);
  68.                 }
  69.                
  70.                
  71.         }
  72. }
  73. //#include <stm32f10x.h>
  74. //#define BIT(n)     (1<<n)
  75. //int main()
  76. //{
  77. //     //PC1  配置为推挽输出
  78. //        *((unsigned int*)(0x40021000+0x18)) |=  0X00000010;
  79. //       
  80. ////        RCC->APB2ENR |=  0X00000010;
  81. //        RCC->APB2ENR |=  0X00000020;
  82. //       
  83. ////        GPIOC->CRL &= 1111 1111 1111 1111 1111 1111 1111 0000 1111   0X FFFF FF0F
  84. //        GPIOC->CRL &=  0XFFFFFF0F;
  85. ////        GPIOC->CRL &= 0000 0000 0000 0000 0000 0000 0011 0000   0X 0000 0030       
  86. //        GPIOC->CRL |= 0X00000030;
  87. //       
  88. //        GPIOD->CRL &=  0XFF0FFFFF;       
  89. //        GPIOD->CRL |=  0X00300000;
  90. //       
  91. //       
  92. //       
  93. //        while(1)
  94. //        {
  95. //               
  96. ////                GPIOC->ODR  |= 0X00000002;  //给高电平的
  97. ////                GPIOC->ODR  &=~ 0X00000002;  //给低电平的
  98. ////                GPIOC->ODR  &= 0XFFFFFFFD;  //给低电平的
  99. //               
  100. //               
  101. ////                GPIOC->ODR  |= 1<<1;  //给高电平的   PC1
  102. ////        GPIOC->ODR  &= ~(1<<1);  //给高电平的   PC1
  103. ////               
  104. //               
  105. //                GPIOC->ODR  |=  BIT(1);  //给高电平的   PC1
  106. //        GPIOC->ODR  &= ~BIT(1);  //给高电平的   PC1
  107. //               
  108. //                GPIOD->ODR  |=  BIT(5);  //给高电平的   PD1
  109. //        GPIOD->ODR  &= ~BIT(5);  //给高电平的   PD1
  110. //               
  111. //               
  112. //        }
  113. //       
  114. //}
复制代码

3.2. 表现十进制函数的解说



四、利用留意事项

4.1 设置符合本身的引脚



4.2 花屏和不表现 OLED 步伐驱动精确才表现否则黑屏

方案一: IIC总线添加上拉电阻
方案二:将引脚设置为推挽输出,能办理花屏和不表现题目

4.3 表现效果

表现单个字符
表现字符串
表现十进制数

4.4 怎样获取字模



汉字


五、手把手操纵视频

5.1 OLED 移植过程


   OLED移植操纵演示

5.2 OLED快速入手 2025.12.26


   20251226_162041_OLED快速入手

总结

先容了 OLED的移植和利用
作业:
1.实现中文表现,
2.实现多张图片表现,
3.实现按键触发表现,
4.实现按键菜单图形界面
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表