找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 589|回复: 0
打印 上一主题 下一主题
收起左侧

stm32f1_IIC_OLED显示屏驱动

[复制链接]
跳转到指定楼层
楼主
ID:193626 发表于 2023-11-17 19:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "OLED_Font.h"
  2. #include "OLED.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. }
复制代码
原理图: 无
仿真: 无
代码: 程序.7z (281.07 KB, 下载次数: 19)

评分

参与人数 1黑币 +40 收起 理由
admin + 40 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表