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

IIC通信时,有时候会卡住,

[复制链接]
CC4 提问时间:2018-3-13 11:50 /
用STM32F103C8T6 做了个核心板,使用软件模拟IIC和12864的OLED屏幕通信,项目中使用CubeMX构建,添加了FreeRTOS。
配置有三个任务,其中,display 任务负责把CACHE中的数据不停的发送给屏幕,
其他二个任务负责给CACHE中写入数据 。

初始化以后,程序运行正确,画线条成功,大约画了40左右条线的时候,就会很慢,感觉象是很卡的样子。

DEBUG发现,这个时候,系统会卡在IIC_Stop 方法上面。。。

#define SCL_L HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_RESET)
#define SDA_L HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET)


大约是这二行上面,就不再继续 运行了。。。。

还请大神帮分析问题所在
收藏 评论13 发布时间:2018-3-13 11:50

举报

13个回答
wenyangzeng 回答时间:2018-3-13 12:41:30
IIC器件发送应答信号时如果被其他中断打断,就可能出现上述现象。建议进行IIC通讯时禁止其他中断发生。

评分

参与人数 1蝴蝶豆 +3 收起 理由
zero99 + 3

查看全部评分

废鱼 回答时间:2018-3-13 13:48:59
HAL库,建议用硬件I2C通讯。

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

CC4 回答时间:2018-3-13 14:01:50
安 发表于 2018-3-13 13:48
HAL库,建议用硬件I2C通讯。

使用硬件IIC的话,只怕是需要修改引脚了,但是,板子是别人做好的,引脚 不能修改,可有办法使用硬件IIC ?
CC4 回答时间:2018-3-13 14:03:08
wenyangzeng 发表于 2018-3-13 12:41
IIC器件发送应答信号时如果被其他中断打断,就可能出现上述现象。建议进行IIC通讯时禁止其他中断发生。 ...

IIC通信是会一直持续进行的,这个是由FreeRTOS调度的。我如何来做到禁止中断发生?
wenyangzeng 回答时间:2018-3-13 14:06:37
select326 发表于 2018-3-13 14:03
IIC通信是会一直持续进行的,这个是由FreeRTOS调度的。我如何来做到禁止中断发生? ...

至少中断优先级改变一下
废鱼 回答时间:2018-3-13 14:13:07
不行,只能用模拟的I2C了。看一下驱动,STOP的时候是不是有挂起等待时间的操作?可能是其他的任务影响的。
Tcreat 回答时间:2018-3-13 14:18:27
最近怎么那么多涉及到I2C的
七哥 回答时间:2018-3-13 14:20:48
把其余任务去掉,只开I2C的,看看还卡死不?排除多任务造成的影响。
至于I2C问题,论坛问的人也不少了,可以搜索参考一下别人回复。同一问题,多次回答比较浪费资源。

评分

参与人数 1蝴蝶豆 +3 收起 理由
zero99 + 3

查看全部评分

熊猫火锅 回答时间:2018-3-13 14:36:50
关闭中断吧,实在没办法,就修改优先级别。最好是用硬件I2C.

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

MrJiu 回答时间:2018-3-13 23:40:30
使用模拟完全没问题,个人觉得ST的I2C就应该用模拟的,因为硬件实在是操蛋(曾经的受害者,唉。。。一言难尽,我配置的I2C,使用查询模式,完全没问题,但是,比如用手摸一下总线,就会卡死,卡死的地方是标志位冲突,举个例子,比如标志位显示总线忙,但是实际用示波器测量,完全没问题,这时候,只能重启I2C,即使解决这个问题,但是量产依然出现莫名奇妙的问题)。。。至于你的问题,我觉得是优先级切换的问题,当然了和整个程序设计也有关系,比如刷屏,但是被另外一个任务切换了,但是这个任务需要很长实际才执行完,这时候,有些从机会默认退出通信。。。不过,我觉得还是模拟写的不太好导致的。。。反正我用模拟的,从来不出问题!!!

评分

参与人数 1蝴蝶豆 +4 收起 理由
zero99 + 4

查看全部评分

xmstudio 回答时间:2018-3-13 23:58:19
我有一个写好的OLED程序,楼主可以参考一下。这个程序的功能是ADC采样,并且利用ADC噪声产生一个随机数显示在屏幕上,屏幕接的是I2C1。
里面有一些我自己写的函数你应该可以直接拿去用

HAL_I2C_Master_Transmit(&hi2c1, 0x78, OledInitCodes, sizeof(OledInitCodes), 10);是OLED初始化,初始化的代码存在了OledInitCodes这个数组里,0x80代表发送的是指令。

OLED_Clear(void);是清屏

OLED_Print(char *c, uint8_t line);是显示任意ascii字符,这个程序写的是按行显示的,第一个参数是字符指针,可以直接用双引号那种字符串,也可以把要显示的字符先用sprintf写进变量再调用,这两种用法我这个程序都用到了。line是要显示字符的行,一般是从0到6,每行显示16个字符,每行字符占用OLED的两行PAGE
  1. /**
  2.   ******************************************************************************
  3.   * @file           : main.c
  4.   * @brief          : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f1xx_hal.h"

  41. /* USER CODE BEGIN Includes */

  42. /* USER CODE END Includes */

  43. /* Private variables ---------------------------------------------------------*/
  44. ADC_HandleTypeDef hadc1;

  45. I2C_HandleTypeDef hi2c1;

  46. TIM_HandleTypeDef htim2;

  47. /* USER CODE BEGIN PV */
  48. /* Private variables ---------------------------------------------------------*/
  49. uint8_t ASCII[] =
  50. {
  51.                  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",0*/
  52.                  0x00,0x00,0x00,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x17,0x17,0x00,0x00,0x00,/*"!",1*/
  53.                  0x00,0x00,0x78,0x78,0x00,0x78,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*""",2*/
  54.                  0x00,0x40,0xC0,0xF8,0x78,0xC0,0xF8,0x78,0x02,0x1E,0x1F,0x03,0x1E,0x1F,0x03,0x02,/*"#",3*/
  55.                  0xE0,0xF0,0x10,0x18,0x18,0x10,0x30,0x20,0x08,0x19,0x11,0x71,0x71,0x11,0x1F,0x0E,/*"$",4*/
  56.                  0x10,0x38,0x28,0x38,0x90,0xC0,0x60,0x30,0x08,0x0C,0x06,0x03,0x09,0x1C,0x14,0x1C,/*"%",5*/
  57.                  0x00,0x70,0xF8,0x88,0x08,0x18,0x10,0x00,0x0E,0x1F,0x11,0x11,0x13,0x1E,0x0C,0x1E,/*"&",6*/
  58.                  0x00,0x00,0x00,0x78,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"'",7*/
  59.                  0x00,0x00,0x00,0xC0,0xF0,0x38,0x08,0x00,0x00,0x00,0x00,0x1F,0x7F,0xE0,0x80,0x00,/*"(",8*/
  60.                  0x00,0x00,0x08,0x38,0xF0,0xC0,0x00,0x00,0x00,0x00,0x80,0xE0,0x7F,0x1F,0x00,0x00,/*")",9*/
  61.                  0x80,0xA0,0xE0,0xE0,0xE0,0xE0,0xA0,0x80,0x00,0x02,0x03,0x03,0x03,0x03,0x02,0x00,/*"*",10*/
  62.                  0x00,0x00,0x00,0xE0,0xE0,0x00,0x00,0x00,0x01,0x01,0x01,0x0F,0x0F,0x01,0x01,0x01,/*"+",11*/
  63.                  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x38,0x38,0x18,0x08,0x00,/*",",12*/
  64.                  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,/*"-",13*/
  65.                  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x00,0x00,/*".",14*/
  66.                  0x00,0x00,0x00,0x00,0x80,0xC0,0x60,0x30,0x08,0x0C,0x06,0x03,0x01,0x00,0x00,0x00,/*"/",15*/
  67.                  0x00,0xF0,0xF8,0x08,0x08,0x08,0xF8,0xF0,0x00,0x0F,0x1F,0x10,0x10,0x10,0x1F,0x0F,/*"0",16*/
  68.                  0x00,0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,/*"1",17*/
  69.                  0x00,0x10,0x18,0x08,0x08,0x88,0xF8,0x70,0x00,0x18,0x1C,0x16,0x13,0x11,0x10,0x10,/*"2",18*/
  70.                  0x00,0x10,0x18,0x88,0x88,0x88,0xF8,0x70,0x00,0x08,0x18,0x10,0x10,0x10,0x1F,0x0F,/*"3",19*/
  71.                  0x00,0x00,0x80,0xE0,0x70,0xF8,0xF8,0x00,0x00,0x03,0x03,0x02,0x12,0x1F,0x1F,0x12,/*"4",20*/
  72.                  0x00,0xF8,0xF8,0x88,0x88,0x88,0x88,0x08,0x00,0x08,0x18,0x10,0x10,0x10,0x1F,0x0F,/*"5",21*/
  73.                  0x00,0xE0,0xF0,0x98,0x88,0x88,0x88,0x00,0x00,0x0F,0x1F,0x11,0x10,0x10,0x1F,0x0F,/*"6",22*/
  74.                  0x00,0x18,0x18,0x08,0x88,0xE8,0x78,0x18,0x00,0x00,0x18,0x1E,0x07,0x01,0x00,0x00,/*"7",23*/
  75.                  0x00,0x70,0xF8,0x88,0x88,0x88,0xF8,0x70,0x00,0x0F,0x1F,0x10,0x10,0x10,0x1F,0x0F,/*"8",24*/
  76.                  0x00,0xF0,0xF8,0x08,0x08,0x88,0xF8,0xF0,0x00,0x00,0x11,0x11,0x11,0x19,0x0F,0x07,/*"9",25*/
  77.                  0x00,0x00,0x00,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x00,0x00,/*":",26*/
  78.                  0x00,0x00,0x00,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x20,0x38,0x38,0x18,0x08,0x00,/*";",27*/
  79.                  0x00,0x00,0x80,0xC0,0x60,0x30,0x10,0x00,0x00,0x01,0x03,0x06,0x0C,0x18,0x10,0x00,/*"<",28*/
  80.                  0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,/*"=",29*/
  81.                  0x00,0x10,0x30,0x60,0xC0,0x80,0x00,0x00,0x00,0x10,0x18,0x0C,0x06,0x03,0x01,0x00,/*">",30*/
  82.                  0x00,0x10,0x18,0x08,0x08,0x88,0xF8,0x70,0x00,0x00,0x00,0x16,0x17,0x01,0x00,0x00,/*"?",31*/
  83.                  0xE0,0xF0,0x98,0xC8,0x48,0xC8,0xD8,0xF0,0x0F,0x1F,0x33,0x27,0x24,0x27,0x27,0x27,/*"@",32*/
  84.                  0x00,0x88,0xE8,0x78,0x78,0xE0,0x80,0x00,0x1C,0x1F,0x13,0x02,0x02,0x13,0x1F,0x1C,/*"A",33*/
  85.                  0x08,0xF8,0xF8,0x88,0x88,0x88,0xF8,0x70,0x10,0x1F,0x1F,0x10,0x10,0x10,0x1F,0x0F,/*"B",34*/
  86.                  0xE0,0xF0,0x18,0x08,0x08,0x18,0x78,0x78,0x07,0x0F,0x18,0x10,0x10,0x10,0x18,0x08,/*"C",35*/
  87.                  0x08,0xF8,0xF8,0x08,0x08,0x18,0xF0,0xE0,0x10,0x1F,0x1F,0x10,0x10,0x18,0x0F,0x07,/*"D",36*/
  88.                  0x08,0xF8,0xF8,0x88,0xC8,0xC8,0x18,0x18,0x10,0x1F,0x1F,0x10,0x11,0x11,0x18,0x18,/*"E",37*/
  89.                  0x08,0xF8,0xF8,0x88,0xC8,0xC8,0x18,0x18,0x10,0x1F,0x1F,0x10,0x11,0x01,0x00,0x00,/*"F",38*/
  90.                  0xE0,0xF0,0x18,0x08,0x08,0x18,0x78,0x78,0x07,0x0F,0x18,0x10,0x11,0x11,0x1F,0x0F,/*"G",39*/
  91.                  0xF8,0xF8,0x88,0x80,0x80,0x88,0xF8,0xF8,0x1F,0x1F,0x10,0x00,0x00,0x10,0x1F,0x1F,/*"H",40*/
  92.                  0x00,0x08,0x08,0xF8,0xF8,0x08,0x08,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,/*"I",41*/
  93.                  0x00,0x00,0x00,0x08,0x08,0xF8,0xF8,0x08,0x0C,0x1C,0x10,0x10,0x10,0x1F,0x0F,0x00,/*"J",42*/
  94.                  0x08,0xF8,0xF8,0x88,0xE8,0x78,0x18,0x08,0x10,0x1F,0x1F,0x11,0x03,0x06,0x1C,0x18,/*"K",43*/
  95.                  0x08,0xF8,0xF8,0x08,0x08,0x00,0x00,0x00,0x10,0x1F,0x1F,0x10,0x10,0x10,0x18,0x18,/*"L",44*/
  96.                  0xF8,0xF8,0xE0,0x80,0x80,0xE0,0xF8,0xF8,0x1F,0x1F,0x11,0x07,0x07,0x11,0x1F,0x1F,/*"M",45*/
  97.                  0xF8,0xF8,0xE0,0xC0,0x08,0x08,0xF8,0xF8,0x1F,0x1F,0x10,0x11,0x07,0x0E,0x1F,0x1F,/*"N",46*/
  98.                  0xE0,0xF0,0x18,0x08,0x08,0x18,0xF0,0xE0,0x07,0x0F,0x18,0x10,0x10,0x18,0x0F,0x07,/*"O",47*/
  99.                  0x08,0xF8,0xF8,0x08,0x08,0x08,0xF8,0xF0,0x10,0x1F,0x1F,0x11,0x11,0x01,0x01,0x00,/*"P",48*/
  100.                  0xE0,0xF0,0x18,0x08,0x08,0x18,0xF0,0xE0,0x07,0x4F,0x58,0x70,0x30,0x78,0x4F,0x47,/*"Q",49*/
  101.                  0x08,0xF8,0xF8,0x08,0x08,0x08,0xF8,0xF0,0x10,0x1F,0x1F,0x11,0x03,0x07,0x1D,0x18,/*"R",50*/
  102.                  0x70,0xF8,0x88,0x88,0x88,0x18,0x38,0x38,0x1C,0x1C,0x18,0x10,0x11,0x11,0x1F,0x0E,/*"S",51*/
  103.                  0x18,0x08,0x08,0xF8,0xF8,0x08,0x08,0x18,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,/*"T",52*/
  104.                  0xF8,0xF8,0x08,0x00,0x00,0x08,0xF8,0xF8,0x0F,0x0F,0x18,0x10,0x10,0x18,0x0F,0x0F,/*"U",53*/
  105.                  0x38,0xF8,0xC8,0x00,0x00,0xC8,0xF8,0x38,0x00,0x01,0x07,0x1E,0x1E,0x07,0x01,0x00,/*"V",54*/
  106.                  0xF8,0x88,0x80,0xE0,0xE0,0x80,0x88,0xF8,0x03,0x1F,0x1F,0x03,0x03,0x1F,0x1F,0x03,/*"W",55*/
  107.                  0x18,0x38,0x68,0xC0,0xC0,0x68,0x38,0x18,0x18,0x1C,0x16,0x03,0x03,0x16,0x1C,0x18,/*"X",56*/
  108.                  0x18,0x78,0xE8,0x80,0x80,0xE8,0x78,0x18,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,/*"Y",57*/
  109.                  0x18,0x18,0x08,0x88,0xC8,0x68,0x38,0x18,0x18,0x1C,0x16,0x13,0x11,0x10,0x18,0x18,/*"Z",58*/
  110.                  0x00,0x00,0x00,0xF8,0xF8,0x08,0x08,0x00,0x00,0x00,0x00,0xFF,0xFF,0x80,0x80,0x00,/*"[",59*/
  111.                  0x10,0x30,0x60,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x06,0x0C,/*"",60*/
  112.                  0x00,0x00,0x08,0x08,0xF8,0xF8,0x00,0x00,0x00,0x00,0x80,0x80,0xFF,0xFF,0x00,0x00,/*"]",61*/
  113.                  0x40,0x60,0x30,0x18,0x18,0x30,0x60,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"^",62*/
  114.                  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,/*"_",63*/
  115.                  0x00,0x00,0x08,0x18,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"`",64*/
  116.                  0x00,0x40,0x40,0x40,0x40,0xC0,0x80,0x00,0x0E,0x1F,0x11,0x11,0x11,0x1F,0x1F,0x10,/*"a",65*/
  117.                  0x08,0xF8,0xF8,0xC0,0x40,0xC0,0x80,0x80,0x10,0x1F,0x1F,0x18,0x10,0x18,0x0F,0x0F,/*"b",66*/
  118.                  0x80,0x80,0xC0,0x40,0x40,0xC0,0xC0,0xC0,0x0F,0x0F,0x18,0x10,0x10,0x18,0x08,0x08,/*"c",67*/
  119.                  0x80,0x80,0xC0,0x40,0xC8,0xF8,0xF8,0x00,0x0F,0x0F,0x18,0x10,0x18,0x1F,0x1F,0x10,/*"d",68*/
  120.                  0x80,0x80,0xC0,0x40,0x40,0xC0,0x80,0x80,0x0F,0x0F,0x1A,0x12,0x12,0x1A,0x0B,0x0B,/*"e",69*/
  121.                  0x00,0x80,0xF0,0xF8,0x88,0x88,0x18,0x10,0x00,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,/*"f",70*/
  122.                  0x80,0x80,0xC0,0x40,0xC0,0xC0,0xC0,0x40,0x67,0xE7,0x8C,0x88,0x8C,0xFF,0x7F,0x00,/*"g",71*/
  123.                  0x08,0xF8,0xF8,0xC0,0x40,0xC0,0x80,0x00,0x10,0x1F,0x1F,0x10,0x10,0x1F,0x1F,0x10,/*"h",72*/
  124.                  0x00,0x40,0x40,0xC8,0xC8,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,/*"i",73*/
  125.                  0x00,0x00,0x40,0x40,0xC8,0xC8,0x00,0x00,0x40,0xC0,0x80,0x80,0xFF,0x7F,0x00,0x00,/*"j",74*/
  126.                  0x08,0xF8,0xF8,0x00,0xC0,0xC0,0x40,0x40,0x10,0x1F,0x1F,0x13,0x17,0x1C,0x18,0x10,/*"k",75*/
  127.                  0x00,0x08,0x08,0xF8,0xF8,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,/*"l",76*/
  128.                  0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x1F,0x1F,0x10,0x1F,0x1F,0x10,0x1F,0x1F,/*"m",77*/
  129.                  0x40,0xC0,0xC0,0xC0,0x40,0xC0,0x80,0x00,0x10,0x1F,0x1F,0x10,0x10,0x1F,0x1F,0x10,/*"n",78*/
  130.                  0x80,0x80,0xC0,0x40,0x40,0xC0,0x80,0x80,0x0F,0x0F,0x18,0x10,0x10,0x18,0x0F,0x0F,/*"o",79*/
  131.                  0x40,0xC0,0xC0,0xC0,0x40,0xC0,0x80,0x80,0x80,0xFF,0xFF,0x98,0x90,0x18,0x0F,0x0F,/*"p",80*/
  132.                  0x80,0x80,0xC0,0x40,0xC0,0xC0,0xC0,0x40,0x0F,0x0F,0x18,0x90,0x98,0xFF,0xFF,0x80,/*"q",81*/
  133.                  0x40,0x40,0xC0,0xC0,0xC0,0x40,0x40,0xC0,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,/*"r",82*/
  134.                  0x80,0xC0,0x40,0x40,0x40,0x40,0xC0,0x80,0x09,0x1B,0x12,0x12,0x12,0x12,0x1E,0x0C,/*"s",83*/
  135.                  0x40,0x40,0xF0,0xF0,0x40,0x40,0x40,0x00,0x00,0x00,0x0F,0x1F,0x10,0x10,0x18,0x08,/*"t",84*/
  136.                  0x40,0xC0,0xC0,0x00,0x40,0xC0,0xC0,0x00,0x00,0x0F,0x1F,0x10,0x18,0x1F,0x1F,0x10,/*"u",85*/
  137.                  0xC0,0xC0,0x40,0x00,0x00,0x40,0xC0,0xC0,0x00,0x03,0x0F,0x1C,0x1C,0x0F,0x03,0x00,/*"v",86*/
  138.                  0xC0,0xC0,0x40,0xC0,0xC0,0x40,0xC0,0xC0,0x07,0x1F,0x1F,0x07,0x07,0x1F,0x1F,0x07,/*"w",87*/
  139.                  0x40,0xC0,0xC0,0x40,0x40,0xC0,0xC0,0x40,0x10,0x18,0x1D,0x17,0x17,0x1D,0x18,0x10,/*"x",88*/
  140.                  0xC0,0xC0,0x40,0x00,0x00,0x40,0xC0,0xC0,0x80,0x83,0xCF,0x7C,0x3C,0x0F,0x03,0x00,/*"y",89*/
  141.                  0xC0,0xC0,0x40,0x40,0x40,0xC0,0xC0,0x40,0x10,0x18,0x1C,0x16,0x13,0x11,0x18,0x18,/*"z",90*/
  142.                  0x00,0x00,0x00,0xF0,0xF8,0x08,0x08,0x00,0x00,0x02,0x02,0x7F,0xFD,0x80,0x80,0x00,/*"{",91*/
  143.                  0x00,0x00,0x00,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,/*"|",92*/
  144.                  0x00,0x00,0x08,0x08,0xF8,0xF0,0x00,0x00,0x00,0x00,0x80,0x80,0xFD,0x7F,0x02,0x02,/*"}",93*/
  145.                  0x30,0x38,0x08,0x18,0x30,0x20,0x38,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"~",94*/
  146. };

  147. uint8_t OLED_Buffer[] =
  148. {
  149.                 0x80,0xB0,0x80,0x00,0x80,0x10,
  150.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,
  151.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,
  152.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,
  153.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,
  154.                 0x80,0xB1,0x80,0x00,0x80,0x10,
  155.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,
  156.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,
  157.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,
  158.                 0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x00,0x40,0x00,
  159. };

  160. uint8_t OledInitCodes[] =
  161. {
  162.                  0x80,0x81,//亮度设置
  163.                  0x80,0x00,//00~FF
  164.                  0x80,0xA4,//A4??,A5??
  165.                  0x80,0xA6,//A6??,A7??
  166.                  0x80,0xAF,//AE??,AF??
  167.                  0x80,0xA8,
  168.                  0x80,0x3F,
  169.                  0x80,0xC8,//C0????,C8??
  170.                  0x80,0xD3,
  171.                  0x80,0x00,
  172.                  0x80,0x40,
  173.                  0x80,0xA1,//A0????,A1??
  174.                  0x80,0xDA,
  175.                  0x80,0x12,
  176.                  0x80,0xD5,
  177.                  0x80,0xF0,
  178.                  0x80,0x8D,
  179.                  0x80,0x14,
  180.                  0x80,0x20,
  181.                  0x80,0x02,
  182.                  0x80,0xb0,
  183.                  0x80,0x00,
  184.                  0x00,0x10
  185. };

  186. uint8_t *pOLED_Buffer;
  187. uint8_t *pASCII;
  188. char cBuffer[16];
  189. uint16_t ADC_Result;
  190. uint16_t ADC_Result_Sum;
  191. float ADC_Final_Result;
  192. uint16_t ADC_Final_Result_d;
  193. uint16_t ADC_Final_Result_f;
  194. uint8_t Main_Status = 0;
  195. uint8_t Random_Result;
  196. /* USER CODE END PV */

  197. /* Private function prototypes -----------------------------------------------*/
  198. void SystemClock_Config(void);
  199. static void MX_GPIO_Init(void);
  200. static void MX_I2C1_Init(void);
  201. static void MX_TIM2_Init(void);
  202. static void MX_ADC1_Init(void);

  203. /* USER CODE BEGIN PFP */
  204. /* Private function prototypes -----------------------------------------------*/
  205. void OLED_Clear(void);
  206. void OLED_Print(char *c, uint8_t line);
  207. /* USER CODE END PFP */

  208. /* USER CODE BEGIN 0 */

  209. /* USER CODE END 0 */

  210. /**
  211.   * @brief  The application entry point.
  212.   *
  213.   * @retval None
  214.   */
  215. int main(void)
  216. {
  217.   /* USER CODE BEGIN 1 */

  218.   /* USER CODE END 1 */

  219.   /* MCU Configuration----------------------------------------------------------*/

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

  222.   /* USER CODE BEGIN Init */

  223.   /* USER CODE END Init */

  224.   /* Configure the system clock */
  225.   SystemClock_Config();

  226.   /* USER CODE BEGIN SysInit */

  227.   /* USER CODE END SysInit */

  228.   /* Initialize all configured peripherals */
  229.   MX_GPIO_Init();
  230.   MX_I2C1_Init();
  231.   MX_TIM2_Init();
  232.   MX_ADC1_Init();
  233.   /* USER CODE BEGIN 2 */
  234.   /* 等待OLED上电 */
  235.   HAL_Delay(100);

  236.   HAL_I2C_Master_Transmit(&hi2c1, 0x78, OledInitCodes, sizeof(OledInitCodes), 10);
  237.   OLED_Clear();
  238.   OLED_Print("Welcome!", 6);

  239.   /* ADC1自校准 */
  240.   HAL_ADCEx_Calibration_Start(&hadc1);

  241.   HAL_TIM_Base_Start_IT(&htim2);
  242.   /* USER CODE END 2 */

  243.   /* Infinite loop */
  244.   /* USER CODE BEGIN WHILE */
  245.   while (1)
  246.   {
  247.           HAL_SuspendTick();
  248.           HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  249.           HAL_ResumeTick();
  250.   /* USER CODE END WHILE */

  251.   /* USER CODE BEGIN 3 */
  252.           switch(Main_Status++)
  253.           {
  254.           case 0:
  255.                   HAL_ADC_Start(&hadc1);
  256.                   ADC_Result_Sum = 0;
  257.                   Random_Result = 0;
  258.                   break;
  259.           case 1:
  260.           case 2:
  261.           case 3:
  262.           case 4:
  263.           case 5:
  264.           case 6:
  265.           case 7:
  266.                   ADC_Result = HAL_ADC_GetValue(&hadc1);
  267.                   ADC_Result_Sum += ADC_Result;
  268.                   Random_Result <<= 1;
  269.                   Random_Result |= (ADC_Result & 0x01);
  270.                   HAL_ADC_Start(&hadc1);
  271.                   break;
  272.           case 8:
  273.                   ADC_Result = HAL_ADC_GetValue(&hadc1);
  274.                   ADC_Result_Sum += ADC_Result;
  275.                   Random_Result <<= 1;
  276.                   Random_Result |= (ADC_Result & 0x01);
  277.                   sprintf(cBuffer, "Random = %d", Random_Result);
  278.                   OLED_Print(cBuffer, 0);
  279.                   sprintf(cBuffer, "ADC Value = %d", ADC_Result_Sum >> 3);        //8次采样求平均
  280.                   OLED_Print(cBuffer, 2);
  281.                   ADC_Final_Result = ADC_Result_Sum * 3.358 / 4095 / 8;        //电源电压3.358V

  282.                   /* 由于sprintf不支持输出浮点变量,拆成两个整形分别输出 */
  283.                   ADC_Final_Result_d = (uint16_t)ADC_Final_Result;        //??????
  284.                   ADC_Final_Result -= ADC_Final_Result_d;        //??????
  285.                   ADC_Final_Result_f = (uint16_t)(ADC_Final_Result * 1e03);        //??????
  286.                   sprintf(cBuffer, "Voltage = %d.%03dV", ADC_Final_Result_d, ADC_Final_Result_f);
  287.                   OLED_Print(cBuffer, 4);
  288.                   break;
  289.           default:
  290.                   Main_Status = 0;

  291.                   /* 翻转LED */
  292.                   HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  293.                   break;
  294.           }
  295.   }
  296.   /* USER CODE END 3 */

  297. }

  298. /**
  299.   * @brief System Clock Configuration
  300.   * @retval None
  301.   */
  302. void SystemClock_Config(void)
  303. {

  304.   RCC_OscInitTypeDef RCC_OscInitStruct;
  305.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  306.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  307.     /**Initializes the CPU, AHB and APB busses clocks
  308.     */
  309.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  310.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  311.   RCC_OscInitStruct.HSICalibrationValue = 16;
  312.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  313.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  314.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  315.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  316.   {
  317.     _Error_Handler(__FILE__, __LINE__);
  318.   }

  319.     /**Initializes the CPU, AHB and APB busses clocks
  320.     */
  321.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  322.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  323.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  324.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  325.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  326.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  327.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  328.   {
  329.     _Error_Handler(__FILE__, __LINE__);
  330.   }

  331.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  332.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  333.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  334.   {
  335.     _Error_Handler(__FILE__, __LINE__);
  336.   }

  337.     /**Configure the Systick interrupt time
  338.     */
  339.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  340.     /**Configure the Systick
  341.     */
  342.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  343.   /* SysTick_IRQn interrupt configuration */
  344.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  345. }

  346. /* ADC1 init function */
  347. static void MX_ADC1_Init(void)
  348. {

  349.   ADC_ChannelConfTypeDef sConfig;

  350.     /**Common config
  351.     */
  352.   hadc1.Instance = ADC1;
  353.   hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  354.   hadc1.Init.ContinuousConvMode = DISABLE;
  355.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  356.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  357.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  358.   hadc1.Init.NbrOfConversion = 1;
  359.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  360.   {
  361.     _Error_Handler(__FILE__, __LINE__);
  362.   }

  363.     /**Configure Regular Channel
  364.     */
  365.   sConfig.Channel = ADC_CHANNEL_1;
  366.   sConfig.Rank = ADC_REGULAR_RANK_1;
  367.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  368.   if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  369.   {
  370.     _Error_Handler(__FILE__, __LINE__);
  371.   }

  372. }

  373. /* I2C1 init function */
  374. static void MX_I2C1_Init(void)
  375. {

  376.   hi2c1.Instance = I2C1;
  377.   hi2c1.Init.ClockSpeed = 400000;
  378.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  379.   hi2c1.Init.OwnAddress1 = 0;
  380.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  381.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  382.   hi2c1.Init.OwnAddress2 = 0;
  383.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  384.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  385.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  386.   {
  387.     _Error_Handler(__FILE__, __LINE__);
  388.   }

  389. }

  390. /* TIM2 init function */
  391. static void MX_TIM2_Init(void)
  392. {

  393.   TIM_ClockConfigTypeDef sClockSourceConfig;
  394.   TIM_MasterConfigTypeDef sMasterConfig;

  395.   htim2.Instance = TIM2;
  396.   htim2.Init.Prescaler = 159;
  397.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  398.   htim2.Init.Period = 4999;
  399.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  400.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  401.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  402.   {
  403.     _Error_Handler(__FILE__, __LINE__);
  404.   }

  405.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  406.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  407.   {
  408.     _Error_Handler(__FILE__, __LINE__);
  409.   }

  410.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  411.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  412.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  413.   {
  414.     _Error_Handler(__FILE__, __LINE__);
  415.   }

  416. }

  417. /** Configure pins as
  418.         * Analog
  419.         * Input
  420.         * Output
  421.         * EVENT_OUT
  422.         * EXTI
  423. */
  424. static void MX_GPIO_Init(void)
  425. {

  426.   GPIO_InitTypeDef GPIO_InitStruct;

  427.   /* GPIO Ports Clock Enable */
  428.   __HAL_RCC_GPIOC_CLK_ENABLE();
  429.   __HAL_RCC_GPIOA_CLK_ENABLE();
  430.   __HAL_RCC_GPIOB_CLK_ENABLE();

  431.   /*Configure GPIO pin Output Level */
  432.   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

  433.   /*Configure GPIO pin : PC13 */
  434.   GPIO_InitStruct.Pin = GPIO_PIN_13;
  435.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  436.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  437.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  438. }

  439. /* USER CODE BEGIN 4 */
  440. void OLED_Clear(void)
  441. {
  442.           for(uint16_t i = 7; i < 262; i+=2)
  443.                   OLED_Buffer[i] = 0x00;
  444.           for(uint16_t i = 269; i < 524; i+=2)
  445.                   OLED_Buffer[i] = 0x00;
  446.           OLED_Buffer[1] = 0xB0;
  447.           OLED_Buffer[263] = 0xB4;
  448.           HAL_I2C_Master_Transmit(&hi2c1, 0x78, OLED_Buffer, sizeof(OLED_Buffer), 50);
  449.           OLED_Buffer[1]++;
  450.           OLED_Buffer[263]++;
  451.           HAL_I2C_Master_Transmit(&hi2c1, 0x78, OLED_Buffer, sizeof(OLED_Buffer), 50);
  452.           OLED_Buffer[1]++;
  453.           OLED_Buffer[263]++;
  454.           HAL_I2C_Master_Transmit(&hi2c1, 0x78, OLED_Buffer, sizeof(OLED_Buffer), 50);
  455.           OLED_Buffer[1]++;
  456.           OLED_Buffer[263]++;
  457.           HAL_I2C_Master_Transmit(&hi2c1, 0x78, OLED_Buffer, sizeof(OLED_Buffer), 50);
  458. }

  459. void OLED_Print(char *c, uint8_t line)
  460. {
  461.         uint8_t i, j;
  462.         OLED_Buffer[1] = 0xB0 | (line & 0x07);
  463.         OLED_Buffer[263] = 0xB0 | ((line + 1) & 0x07);
  464.     for(i = 0; i < 16; i++){
  465.         if(*c)
  466.         {
  467.             pASCII = ASCII + ((*c - 0x20) << 4);
  468.             pOLED_Buffer = OLED_Buffer + (i << 4) + 7;
  469.             for(j = 8; j; j--){
  470.                 *pOLED_Buffer = *pASCII++;
  471.                 pOLED_Buffer += 2;
  472.             }
  473.             pOLED_Buffer = OLED_Buffer + (i << 4) + 269;
  474.             for(j = 8; j; j--){
  475.                 *pOLED_Buffer = *pASCII++;
  476.                 pOLED_Buffer += 2;
  477.             }
  478.             c++;
  479.         }
  480.         else
  481.         {
  482.             pASCII = ASCII;
  483.             pOLED_Buffer = OLED_Buffer + (i << 4) + 7;
  484.             for(j = 8; j; j--){
  485.                 *pOLED_Buffer = *pASCII++;
  486.                 pOLED_Buffer += 2;
  487.             }
  488.             pOLED_Buffer = OLED_Buffer + (i << 4) + 269;
  489.             for(j = 8; j; j--){
  490.                 *pOLED_Buffer = *pASCII++;
  491.                 pOLED_Buffer += 2;
  492.             }
  493.         }
  494.     }
  495.     HAL_I2C_Master_Transmit(&hi2c1, 0x78, OLED_Buffer, sizeof(OLED_Buffer), 50);
  496. }

  497. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  498. {
  499.     if (htim->Instance == htim2.Instance)
  500.     {

  501.     }
  502. }
  503. /* USER CODE END 4 */

  504. /**
  505.   * @brief  This function is executed in case of error occurrence.
  506.   * @param  file: The file name as string.
  507.   * @param  line: The line in file as a number.
  508.   * @retval None
  509.   */
  510. void _Error_Handler(char *file, int line)
  511. {
  512.   /* USER CODE BEGIN Error_Handler_Debug */
  513.   /* User can add his own implementation to report the HAL error return state */
  514.   while(1)
  515.   {
  516.   }
  517.   /* USER CODE END Error_Handler_Debug */
  518. }

  519. #ifdef  USE_FULL_ASSERT
  520. /**
  521.   * @brief  Reports the name of the source file and the source line number
  522.   *         where the assert_param error has occurred.
  523.   * @param  file: pointer to the source file name
  524.   * @param  line: assert_param error line source number
  525.   * @retval None
  526.   */
  527. void assert_failed(uint8_t* file, uint32_t line)
  528. {
  529.   /* USER CODE BEGIN 6 */
  530.   /* User can add his own implementation to report the file name and line number,
  531.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  532.   /* USER CODE END 6 */
  533. }
  534. #endif /* USE_FULL_ASSERT */

  535. /**
  536.   * @}
  537.   */

  538. /**
  539.   * @}
  540.   */

  541. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码



评分

参与人数 1蝴蝶豆 +4 收起 理由
zero99 + 4

查看全部评分

CC4 回答时间:2018-3-14 22:17:04
xmstudio 发表于 2018-3-13 23:58
我有一个写好的OLED程序,楼主可以参考一下。这个程序的功能是ADC采样,并且利用ADC噪声产生一个随机数显示 ...

感谢指点,我研究下这个办法,谢谢
埃斯提爱慕 回答时间:2018-3-15 15:51:25
提示: 作者被禁止或删除 内容自动屏蔽

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版