当前位置:   article > 正文

基于江科大的stm32单片机学习笔记_江科大stm32

江科大stm32

视频链接:[1-1] 课程简介_哔哩哔哩_bilibili

STM32F103c8t6引脚定义表:(橙色是电源相关的引脚,蓝色是最小系统相关的引脚。绿色的是IO口和功能引脚,FT意为能容忍5V电压,主功能是上电默认的功能,默认复用功能是IO口上连接的外设功能引脚,加粗的引脚是能直接用的,没加粗的引脚可能要进行其他配置才能使用)

3-1 GPIO输出

GPIO叫通用输入输出端口,有八种配置模式

这里解释一下各种模式(原文章:STM32 GPIO八种输入输出模式 - 知乎 ):

1. GPIO_MODE_AIN 模拟输入

输入信号不经施密特触发器直接接入,输入信号为模拟量而非数字量,其余输入方式输入数字量。

2. GPIO_MODE_IN_FLOATING 浮空输入

输入信号经过施密特触发器接入输入数据存储器。当无信号输入时,电压不确定。因为浮空输入既高阻输入,可以认为输入端口阻抗无穷大,这样可以检测到微弱的信号。(相当于电压表测电压,如果电压表内阻不够大而外部阻抗比较大,则电压表分压会比较小)。此时输入高电平即高电平,输入低电平即低电平。但是外界没有输入时输入电平却容易受到外界电磁以及各种玄学干扰的影响。如按键采用浮空输入,则在按键按下时输入电平为低,但是当松开按键时输入端口悬空,外界有微弱的干扰都会被端口检测到。此时端口可能高,也可能低。

3. GPIO_MODE_IPD 下拉输入

浮空输入在外界没有输入时状态不确定,可能对电路造成干扰。为了使得电路更加稳定,不出现没有输入时端口的输入数据被干扰 (比如手碰一下电压就发生变化)。这时就需要下拉电阻(或上拉电阻),此电阻与端口输入阻抗相比仍然较小。有输入信号时端口读取输入信号,无输入信号时端口电平被拉到低电平(高电平)

4. GPIO_MODE_IPU 上拉输入

上拉输入与下拉输入类似,只是无输入信号时端口电平被拉到高电平。例如按键信号,当按下时输入低电平,松开时电平被拉到高电平。这样就不会出现按键松开时端口电平不确定的情况。即不知道时按下还是松开。

5. GPIO-MODE_OUT_OD 开漏输出

开漏输出即漏极开路输出。这种输出方式指场效应管漏极开路输出。需要接上拉电阻才能输出1。漏极经上拉电阻接到电源,栅极输出0时,场效应管截止(阻抗无线大),电压被分到场效应管上,此时输出为1。当栅极输出1时,场效应管导通,输出端口相当于接地,此时输出0。开漏输出高电平时是由外接电源输出的,因此可以实现高于输出端口电压的输出。可以实现电平的转换。开漏输出可以实现线与功能,方法是多个输出共接一个上拉电阻。但是漏极开路输出上升沿慢,因为上升沿是外接电源对上拉电阻以及外接负载充电。当上拉电阻较大或者负载容性较大时时间常数较大,充电较慢。需要较快反映时可以采用下降沿触发,此时没有电阻接入,电路的时间常数较小,充电较快。

6. GPIO_MODE_OUT_PP 推挽输出

推挽输出既可以输出1,又可以输出0。但是无法调节输出电压,因为输出高低电平均为三极管输入端电压,此电压在由芯片内部供电,无法改变。推挽输出任意时刻只有一路工作。上图为输出高电平时电路工作状态。只有三极管导通电阻,无外接电阻。因此推挽输出损耗小、速度快

7. GPIO_MODE_AF_OD 复用开漏输出

STM32单片机内部有其他的外设,比如定时器、DAC等。复用开漏输出与普通开漏输出区别在于,开漏输出输出的是输出数据寄存器中的数据,复用开漏输出输出的是来自外设的数据

8. GOIO_MODE_AF_PP 复用推挽输出

复用推挽输出原理与复用开漏输出原理相同

 官方GPIO相关库函数说明:

 下面是标准库有关GPIO的所有函数,可在"stm32f10x_gpio.h"中查看。

  1. //重置函数,将GPIO的所有配置重置,恢复为上电状态
  2. void GPIO_DeInit(GPIO_TypeDef* GPIOx);
  3. //重置函数,将AFIO的所有配置重置,恢复为上电状态
  4. void GPIO_AFIODeInit(void);
  5. //GPIO的初始化函数,在GPIO相关时钟使能后,通过这个函数配置GPIO的各种参数
  6. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
  7. //将传入的结构体的所有变量重置,以方便再配置其他端口
  8. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
  9. //位读取GPIO的输入信号,直接读取指定的端口的信号,返回高SET或低RESET
  10. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  11. //读取GPIOx的全部端口,返回寄存器中的16位数据
  12. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
  13. //位读取GPIO输出信号,直接读取指定的GPIO的输出信号,返回高SET或低RESET
  14. //当我们不知道当前GPIO输出什么信号,可以用这个函数读取
  15. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  16. //读取GPIOx的全部输出信号,返回寄存器中的16位数据
  17. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
  18. //直接拉高指定端口的电平状态
  19. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  20. //直接拉低指定端口的电平状态
  21. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  22. //直接对指定端口写入高或低电平
  23. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
  24. //对GPIOx写入16位的数据来控制全部端口,参数PortVal以十六进制输入
  25. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
  26. //锁定指定端口的配置,防止再被修改
  27. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  28. //用于配置 GPIO 引脚作为事件输出,以便与其他外设进行事件触发或通知
  29. void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
  30. //用于启用或禁用GPIO事件输出功能
  31. void GPIO_EventOutputCmd(FunctionalState NewState);
  32. //用来进行引脚重映射的函数,当引脚的默认功能与我们的规划有冲突时,可以将一些引脚重映射
  33. //参数1是重映射的方式,参数2是新的状态
  34. void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
  35. //配置AFIO的数据选择器,以此来选择我们想要的中断引脚
  36. void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
  37. //以太网相关
  38. void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);

3-2 LED闪烁&LED流水灯&蜂鸣器

想要驱动GPIO外设,分三个步骤。使能时钟RCC,初始化GPIO,调函数置高低电平

  1. //任务1 LED闪烁 接PA0低电平点亮
  2. void task01()
  3. {
  4. //初始化APB2总线时钟
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  6. //初始化GPIO,初始化GPIOA的PA0
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOA,&GPIO_InitStructure);
  12. while(1)
  13. {
  14. GPIO_ResetBits(GPIOA,GPIO_Pin_0);
  15. Delay_ms(500);
  16. GPIO_SetBits(GPIOA,GPIO_Pin_0);
  17. Delay_ms(500);
  18. }
  19. }

  1. //任务2 LED流水灯 接A0~A7八颗灯珠,低电平点亮
  2. void task02()
  3. {
  4. //使能PB2总线时钟
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  6. //初始化GPIOA A0~A7
  7. GPIO_InitTypeDef GPIO_initstructure;
  8. GPIO_initstructure.GPIO_Pin = GPIO_Pin_All;
  9. GPIO_initstructure.GPIO_Mode = GPIO_Mode_Out_PP;
  10. GPIO_initstructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOA,&GPIO_initstructure);
  12. while(1)
  13. {
  14. GPIO_Write(GPIOA,~0x0001); //0000 0000 0000 0001
  15. Delay_ms(500);
  16. GPIO_Write(GPIOA,~0x0001); //0000 0000 0000 0010
  17. Delay_ms(500);
  18. GPIO_Write(GPIOA,~0x0001); //0000 0000 0000 0100
  19. Delay_ms(500);
  20. GPIO_Write(GPIOA,~0x0001); //0000 0000 0000 1000
  21. Delay_ms(500);
  22. GPIO_Write(GPIOA,~0x0001); //0000 0000 0001 0000
  23. Delay_ms(500);
  24. GPIO_Write(GPIOA,~0x0001); //0000 0000 0010 0000
  25. Delay_ms(500);
  26. GPIO_Write(GPIOA,~0x0001); //0000 0000 0100 0000
  27. Delay_ms(500);
  28. GPIO_Write(GPIOA,~0x0001); //0000 0000 1000 0000
  29. Delay_ms(500);
  30. }
  31. }

  1. //任务3 蜂鸣器叫唤 接PB12,低电平触发,有源蜂鸣器
  2. void task03()
  3. {
  4. //使能PB2时钟总线
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  6. //初始化GPIOB12
  7. GPIO_InitTypeDef GPIO_Initstructure;
  8. GPIO_Initstructure.GPIO_Pin = GPIO_Pin_12;
  9. GPIO_Initstructure.GPIO_Mode = GPIO_Mode_Out_PP;
  10. GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOB,&GPIO_Initstructure);
  12. while(1)
  13. {
  14. GPIO_ResetBits(GPIOB,GPIO_Pin_12);
  15. Delay_ms(500);
  16. GPIO_SetBits(GPIOB,GPIO_Pin_12);
  17. Delay_ms(500);
  18. }
  19. }

3-3 GPIO输入

3-4 按键控制LED&光敏传感器控制蜂鸣器

使用模块化编程,当遇到多外设时,将每个设备的代码分成.c和.h分开来写,有利于使代码结构清晰,也便于移植。

mian.c

  1. #include "stm32f10x.h" // Device header
  2. #include "key.h"
  3. #include "led.h"
  4. #include "buzz.h"
  5. #include "light_sensor.h"
  6. //按键控制LED亮灭&光敏传感器控制蜂鸣器buzz叫唤,模块化编写
  7. //任务1,按键控制LED亮灭
  8. //按键为B1 B11,按下低电平 灯为A1 A2低电平点亮
  9. void task01()
  10. {
  11. LED_Init();
  12. KEY_Init();
  13. while(1)
  14. {
  15. uint8_t key_temp = Read_Key();
  16. if(key_temp == 1)
  17. {
  18. LED_Turn(LED1_Pin);
  19. }
  20. else if(key_temp == 2)
  21. {
  22. LED_Turn(LED2_Pin);
  23. }
  24. }
  25. }
  26. //任务2,光敏传感器控制蜂鸣器buzz交换,有光不叫,无光叫唤
  27. //蜂鸣器为B12,低电平叫 光敏传感器二值化,接B13
  28. void task02()
  29. {
  30. Buzz_Init();
  31. Init_Light_Sensor();
  32. uint8_t temp = 0;
  33. while(1)
  34. {
  35. temp = Read_LightSensor();//无光回0,有光回1
  36. if(temp == 0)//无光,buzz启动
  37. {
  38. Buzz_ON();
  39. }
  40. else if(temp == 1)//有光,buzz关闭
  41. {
  42. Buzz_OFF();
  43. }
  44. else
  45. {
  46. }
  47. }
  48. }

 KEY.c

  1. #include "Device/Include/stm32f10x.h" // Device header
  2. #include "delay.h"
  3. //初始化按键IO口
  4. void KEY_Init(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
  9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOB,&GPIO_InitStructure);
  12. }
  13. //读取按键,按键1按下就返回1,按键2按下就返回2
  14. uint8_t Read_Key(void)
  15. {
  16. uint8_t temp = 0;
  17. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
  18. {
  19. Delay_ms(10);
  20. while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0);
  21. temp = 1;
  22. return temp;
  23. }
  24. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0)
  25. {
  26. Delay_ms(10);
  27. while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0);
  28. temp = 2;
  29. return temp;
  30. }
  31. }

KEY.h

  1. #ifndef _KEY_H_
  2. #define _KEY_H_
  3. void KEY_Init(void);
  4. uint8_t Read_Key(void);
  5. #endif

Delay.c

  1. #include "stm32f10x.h"
  2. /**
  3. * @brief 微秒级延时
  4. * @param xus 延时时长,范围:0~233015
  5. * @retval 无
  6. */
  7. void Delay_us(uint32_t xus)
  8. {
  9. SysTick->LOAD = 72 * xus; //设置定时器重装值
  10. SysTick->VAL = 0x00; //清空当前计数值
  11. SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器
  12. while(!(SysTick->CTRL & 0x00010000)); //等待计数到0
  13. SysTick->CTRL = 0x00000004; //关闭定时器
  14. }
  15. /**
  16. * @brief 毫秒级延时
  17. * @param xms 延时时长,范围:0~4294967295
  18. * @retval 无
  19. */
  20. void Delay_ms(uint32_t xms)
  21. {
  22. while(xms--)
  23. {
  24. Delay_us(1000);
  25. }
  26. }
  27. /**
  28. * @brief 秒级延时
  29. * @param xs 延时时长,范围:0~4294967295
  30. * @retval 无
  31. */
  32. void Delay_s(uint32_t xs)
  33. {
  34. while(xs--)
  35. {
  36. Delay_ms(1000);
  37. }
  38. }

Delay.h

  1. #ifndef __DELAY_H
  2. #define __DELAY_H
  3. void Delay_us(uint32_t us);
  4. void Delay_ms(uint32_t ms);
  5. void Delay_s(uint32_t s);
  6. #endif

 LED.c

  1. #include "Device/Include/stm32f10x.h" // Device header
  2. #include "led.h"
  3. //初始化LED的io口,A1和A2设置为推挽输出
  4. void LED_Init(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
  9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOA,&GPIO_InitStructure);
  12. }
  13. //设定LED亮灭,参数1填入选中LED,参数2填入设定值
  14. void LED_Set(uint8_t LEDx_Pin,uint8_t RESET_OR_SET)
  15. {
  16. if(RESET_OR_SET == SET)
  17. {
  18. GPIO_SetBits(GPIOA,LEDx_Pin);
  19. }
  20. else if(RESET_OR_SET == RESET)
  21. {
  22. GPIO_ResetBits(GPIOA,LEDx_Pin);
  23. }
  24. }
  25. //设置LED翻转
  26. void LED_Turn(uint8_t LEDx_Pin)
  27. {
  28. if(GPIO_ReadOutputDataBit(GPIOA,LEDx_Pin) == 0)
  29. {
  30. GPIO_SetBits(GPIOA,LEDx_Pin);
  31. }
  32. else
  33. {
  34. GPIO_ResetBits(GPIOA,LEDx_Pin);
  35. }
  36. }

LED.h

  1. #ifndef _LED_H_
  2. #define _LED_H_
  3. #define LED1_Pin GPIO_Pin_1
  4. #define LED2_Pin GPIO_Pin_2
  5. #define SET 1
  6. #define RESET 0
  7. void LED_Init(void);
  8. void LED_Set(uint8_t LEDx_Pin,uint8_t RESET_OR_SET);
  9. void LED_Turn(uint8_t LEDx_Pin);
  10. #endif

 buzz.c

  1. #include "Device/Include/stm32f10x.h" // Device header
  2. #include "buzz.h"
  3. //初始化蜂鸣器buzz
  4. void Buzz_Init(void)
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOB,&GPIO_InitStructure);
  12. GPIO_SetBits(GPIOB,GPIO_Pin_12);//默认设为高电平,防止蜂鸣器叫唤
  13. }
  14. //开启蜂鸣器buzz
  15. void Buzz_ON(void)
  16. {
  17. GPIO_ResetBits(GPIOB,GPIO_Pin_12);
  18. }
  19. //关闭蜂鸣器buzz
  20. void Buzz_OFF(void)
  21. {
  22. GPIO_SetBits(GPIOB,GPIO_Pin_12);
  23. }

 buzz.h

  1. #ifndef _BUZZ_H_
  2. #define _BUZZ_H_
  3. void Buzz_Init(void);
  4. void Buzz_ON(void);
  5. void Buzz_OFF(void);
  6. #endif

 light_sensor.c

  1. #include "Device/Include/stm32f10x.h" // Device header
  2. #include "light_sensor.h"
  3. //初始化传感器的io口
  4. void Init_Light_Sensor()
  5. {
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_Init(GPIOB,&GPIO_InitStructure);
  12. }
  13. //读取光敏传感器的值,无光返回0,有光返回1
  14. uint8_t Read_LightSensor()
  15. {
  16. uint8_t temp = 0;
  17. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)//有光
  18. {
  19. temp = 1;
  20. return temp;
  21. }
  22. else if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 1)//无光
  23. {
  24. temp = 0;
  25. return temp;
  26. }
  27. return 3;
  28. }

light_sensor.h

  1. #ifndef _LIGHT_SENSOR_H_
  2. #define _LIGHT_SENSOR_H_
  3. void Init_Light_Sensor(void);
  4. uint8_t Read_LightSensor(void);
  5. #endif

4-1 OLED调试工具

OLED是有机发光二极管的简称,下面提供的OLED驱动函数是江科大自己编写的软件IIC,字库未搭载汉字,不能显示汉字。

相比于硬件IIC,软件IIC更为灵活,任意两个引脚就能驱动OLED,只需在驱动函数稍作修改即可正常使用,但其通信速度相比于硬件IIC来说就较慢了,并且软件IIC会占用CPU的运算资源,硬件IIC和软件IIC各有优劣,按需选择。

4-2 OLED显示屏

  1. void main()
  2. {
  3. OLED_Init();
  4. OLED_ShowChar(1,1,'A'); //显示单个字符
  5. OLED_ShowString(1,3,"hello world!"); //显示字符串
  6. OLED_ShowNum(2,1,12345,5); //显示无符号数字
  7. OLED_ShowSignedNum(2,6,-66,3); //显示有符号数字
  8. OLED_ShowHexNum(3,1,0xA0,8); //显示十六进制数
  9. OLED_ShowBinNum(4,1,0xAA55,16); //显示二进制,填入的参数依然为十六进制
  10. while(1)
  11. {
  12. }
  13. }

 OLED.c

更换不同的引脚使用此驱动库时,只要把.c文件内的宏定义和初始化的引脚配置改成自己想要的即可。

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

OLED.h

  1. #ifndef __OLED_H
  2. #define __OLED_H
  3. void OLED_Init(void);
  4. void OLED_Clear(void);
  5. void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
  6. void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
  7. void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  8. void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
  9. void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  10. void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
  11. #endif

 OLED_Font.h  即字库文件

  1. #ifndef __OLED_FONT_H
  2. #define __OLED_FONT_H
  3. /*OLED字模库,宽8像素,高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. 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
  9. 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  10. 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
  11. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  12. 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
  13. 0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  14. 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
  15. 0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  16. 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
  17. 0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  18. 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
  19. 0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  20. 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
  21. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  22. 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
  23. 0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  24. 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
  25. 0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  26. 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
  27. 0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  28. 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
  29. 0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  30. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31. 0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  32. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33. 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  34. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  36. 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
  37. 0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  38. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  39. 0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  40. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
  41. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  42. 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
  43. 0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  44. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
  45. 0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  46. 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
  47. 0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  48. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
  49. 0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  50. 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
  51. 0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  52. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
  53. 0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  54. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
  55. 0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  56. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
  57. 0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  58. 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
  59. 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  60. 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
  61. 0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  62. 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
  63. 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  64. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
  65. 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  66. 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
  67. 0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  68. 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
  69. 0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  70. 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
  71. 0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  72. 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
  73. 0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  74. 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
  75. 0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  76. 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
  77. 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  78. 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
  79. 0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  80. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  81. 0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  82. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
  83. 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  84. 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
  85. 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  86. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  87. 0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  88. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
  89. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  90. 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
  91. 0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  92. 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
  93. 0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  94. 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
  95. 0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  96. 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
  97. 0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  98. 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
  99. 0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  100. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  101. 0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  102. 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
  103. 0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  104. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
  105. 0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  106. 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
  107. 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  108. 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
  109. 0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  110. 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
  111. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  112. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
  113. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  114. 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
  115. 0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  116. 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
  117. 0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  118. 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
  119. 0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  120. 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
  121. 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  122. 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
  123. 0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  124. 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
  125. 0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  126. 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
  127. 0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  128. 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
  129. 0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  130. 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
  131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  133. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  134. 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
  135. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  136. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  137. 0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  138. 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
  139. 0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  140. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
  141. 0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  142. 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
  143. 0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  144. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  145. 0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  146. 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
  147. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  148. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  149. 0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  150. 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
  151. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  152. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
  153. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  154. 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
  155. 0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  156. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
  157. 0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  158. 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
  159. 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  160. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  161. 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  162. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
  163. 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  164. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
  165. 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  166. 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
  167. 0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  168. 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
  169. 0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  170. 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  171. 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  172. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
  173. 0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  174. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
  175. 0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  176. 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
  177. 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  178. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  179. 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  180. 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
  181. 0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  182. 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
  183. 0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  184. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
  185. 0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  186. 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
  187. 0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  188. 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
  189. 0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  190. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
  191. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  192. 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
  193. 0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  194. 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
  195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
  196. };
  197. #endif

5-1EXTI外部中断

使用外部中断的步骤:

1.配置RCC,相关时钟都打开

2.配置GPIO,选择端口为输入模式

3.配置AFIO,链接到EXTI

4.配置EXTI,选择边缘触发方式

5.配置NVIC,给中断配置优先级

官方的EXTI外设库函数解释:

下面是EXTI外部中断库函数的调用函数,位置在stm32f10x_exti.h"内

  1. //将EXTI外设配置恢复位上电时的默认状态
  2. void EXTI_DeInit(void);
  3. //初始化指定的EXTI寄存器
  4. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
  5. //将EXTI配置的结构体重置,以便重新输入参数来配置其他EXTI
  6. void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct);
  7. //产生一个软件中断
  8. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line);
  9. //查看指定指定线路的EXTI中断标志位的寄存器数值
  10. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line);
  11. //清除指定线路的EXTI中断标志位
  12. //当中断要执行的操作完成后,一定要清除此中断的标志位,否则程序会一直卡在中断里面
  13. void EXTI_ClearFlag(uint32_t EXTI_Line);
  14. //检查指定EXTI线路是否触发中断请求
  15. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);
  16. //清除EXTI线路挂起位
  17. void EXTI_ClearITPendingBit(uint32_t EXTI_Line);

"misc.h"中的关于NVIC中断控制器的函数

  1. //设置中断分组的方式
  2. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
  3. //初始化NVIC中断控制器
  4. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
  5. //设置中断向量表
  6. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset);
  7. //系统低功耗配置
  8. void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState);
  9. //配置SysTick 系统定时器的时钟源
  10. void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource);

5-2 对射使红外传感器计次&旋转编码器计次

这里仅贴核心代码

  1. #include "stm32f10x.h" // Device header
  2. #include "delay.h"
  3. #include "OLED.h"
  4. #include "conter_sensor.h"//二极管计数传感器
  5. #include "encoder_sonser.h"//编码器传感器
  6. /*
  7. 1.配置RCC,相关时钟都打开
  8. 2.配置GPIO,选择端口为输入模式
  9. 3.配置AFIO,链接到EXTI
  10. 4.配置EXTI,选择边缘触发方式
  11. 5.配置NVIC,给中断配置优先级
  12. */
  13. //任务1,对射式红外传感器中断计数并将计次显示到OLED上
  14. //OLEDSCL接B8,SDA接B9,红外传感器接B14
  15. void task01()
  16. {
  17. OLED_Init();
  18. ConterSensor_Init();
  19. OLED_ShowString(1,1,"count:");
  20. while(1)
  21. {
  22. OLED_ShowSignedNum(1,6,Get_ConterNum() ,6);
  23. }
  24. }
  25. //任务2,用中断旋转编码器计数并显示到OLED上
  26. //OLEDSCL接B8,SDA接B9,旋转编码器A相接B0,B相接B1
  27. int16_t num;
  28. void task02()
  29. {
  30. OLED_Init();
  31. EncoderSensor_Init();
  32. OLED_ShowString(1,1,"count:");
  33. while(1)
  34. {
  35. num += Get_ConderNum();
  36. OLED_ShowSignedNum(1,6, num,6);
  37. }
  38. }
  39. int main(void)
  40. {
  41. while (1)
  42. {
  43. }
  44. }
  1. #include "Device/Include/stm32f10x.h" // Device header
  2. #include "conter_sensor.h"
  3. int16_t num = 0;
  4. //初始化红外对射式计数器
  5. //中断信号: GPIO->AFIO->EXTI->NVIC->CPU
  6. void ConterSensor_Init()
  7. {
  8. //开启GPIOB的时钟
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  10. //开启AFIO的时钟
  11. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  12. //配置GPIO B14
  13. GPIO_InitTypeDef My_GPIO_Initstruct;
  14. My_GPIO_Initstruct.GPIO_Pin = GPIO_Pin_14;
  15. My_GPIO_Initstruct.GPIO_Mode = GPIO_Mode_IPU;
  16. My_GPIO_Initstruct.GPIO_Speed = GPIO_Speed_50MHz;
  17. GPIO_Init(GPIOB,&My_GPIO_Initstruct);
  18. //配置AFIO数据选择器
  19. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
  20. //配置EXTI外部中断
  21. EXTI_InitTypeDef My_EXTI_InitStructure;
  22. My_EXTI_InitStructure.EXTI_Line = EXTI_Line14;//指定要配置的中断线
  23. My_EXTI_InitStructure.EXTI_LineCmd = ENABLE;//指定中断是否使能
  24. My_EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//配置中断模式,是中断模式还是事件模式
  25. My_EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//指定触发方式,是上升沿触发还是下降沿触发还是上下均触发,rising上升触发,falling下降沿触发
  26. EXTI_Init(&My_EXTI_InitStructure);
  27. //配置NVIC中断管理器,配置中断优先级
  28. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  29. //初始化NVIC,每个中断通道对应一个中断函数,在触发中断时系统会自动调用
  30. NVIC_InitTypeDef My_NVIC_Initstructure;
  31. My_NVIC_Initstructure.NVIC_IRQChannel = EXTI15_10_IRQn;//选中要配置的中断通道
  32. My_NVIC_Initstructure.NVIC_IRQChannelCmd = ENABLE;//指定通道使能还是失能
  33. My_NVIC_Initstructure.NVIC_IRQChannelPreemptionPriority = 1;//配置抢占优先级
  34. My_NVIC_Initstructure.NVIC_IRQChannelSubPriority = 1;//配置响应优先级
  35. NVIC_Init(&My_NVIC_Initstructure);
  36. }
  37. int16_t Get_ConterNum()
  38. {
  39. return num;
  40. }
  41. //中断函数在启动文件里面查找
  42. void EXTI15_10_IRQHandler(void)
  43. {
  44. if(EXTI_GetITStatus(EXTI_Line14) == SET)
  45. {
  46. num++;
  47. EXTI_ClearITPendingBit(EXTI_Line14);
  48. }
  49. }
  1. #include "encoder_sonser.h"
  2. int16_t encoder_num = 0;
  3. //初始化旋转编码器
  4. void EncoderSensor_Init()
  5. {
  6. //开启GPIOB时钟
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  8. //开启AFIO时钟
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  10. //配置B0和B1端口
  11. GPIO_InitTypeDef My_GPIO_InitStructure;
  12. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  13. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  14. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  15. GPIO_Init(GPIOB,&My_GPIO_InitStructure);
  16. //配置AFIO数据选择器
  17. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
  18. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
  19. //配置外部中断
  20. EXTI_InitTypeDef My_EXIT_InitStructure;
  21. My_EXIT_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;//选择AFIO的通道
  22. My_EXIT_InitStructure.EXTI_LineCmd = ENABLE;//使能中断
  23. My_EXIT_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//配置为中断模式,并非事件模式
  24. My_EXIT_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//配置为上下沿均触发
  25. EXTI_Init(&My_EXIT_InitStructure);
  26. //配置NVIC中断管理器,配置中断优先级
  27. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  28. //初始化NVIC,每个中断通道对应一个中断函数,在触发中断时系统会自动调用
  29. NVIC_InitTypeDef My_NVIC_InitStructure;
  30. My_NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//选中要配置的通道
  31. My_NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//选择使能还是失能
  32. My_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  33. My_NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  34. NVIC_Init(&My_NVIC_InitStructure);
  35. My_NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;//选中要配置的通道
  36. My_NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//选择使能还是失能
  37. My_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  38. My_NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  39. NVIC_Init(&My_NVIC_InitStructure);
  40. }
  41. //获取编码器的变化值
  42. int16_t Get_ConderNum()
  43. {
  44. uint16_t temp;
  45. temp = encoder_num;
  46. encoder_num = 0;
  47. return temp;
  48. }
  49. //中断函数在启动文件里面查找
  50. void EXTI0_IRQHandler(void)
  51. {
  52. if(EXTI_GetFlagStatus(EXTI_Line0) == SET)
  53. {
  54. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
  55. {
  56. encoder_num--;
  57. }
  58. EXTI_ClearITPendingBit(EXTI_Line0);//清除标志位
  59. }
  60. }
  61. void EXTI1_IRQHandler(void)
  62. {
  63. if(EXTI_GetFlagStatus(EXTI_Line1) == SET)
  64. {
  65. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0)
  66. {
  67. encoder_num++;
  68. }
  69. EXTI_ClearITPendingBit(EXTI_Line1);
  70. }
  71. }

6-1TIM定时器中断

 

 

 

 

有关定时器的库函数就更多了,先将"stm32f10x_tim.h"内的函数都列出来大致看一下,用到再详细说明

  1. //将时钟配置恢复到上电时的默认状态
  2. void TIM_DeInit(TIM_TypeDef* TIMx);
  3. //时基单元初始化
  4. void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);
  5. //这四个函数就是配置输出比较模块的,OC(Output Compare)的意思是输出比较
  6. void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
  7. void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
  8. void TIM_OC3Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
  9. void TIM_OC4Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
  10. //输入通道的初始化
  11. void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct);
  12. //PWM输入捕获的设置
  13. void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct);
  14. void TIM_BDTRConfig(TIM_TypeDef* TIMx, TIM_BDTRInitTypeDef *TIM_BDTRInitStruct);
  15. //将之前配置的结构体内的变量重置成默认值,以便重新进行配置
  16. void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);
  17. //给结构体赋默认值的,当我们要重新配置结构体或有些参数用不上时,就使用此函数把结构体赋默认值
  18. void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct);
  19. //初始化输入捕获配置结构体,以便于赋默认值或重新配置
  20. void TIM_ICStructInit(TIM_ICInitTypeDef* TIM_ICInitStruct);
  21. void TIM_BDTRStructInit(TIM_BDTRInitTypeDef* TIM_BDTRInitStruct);
  22. //用来使能定时器
  23. void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState);
  24. //仅高级定时器使用,在使用高级定时器输出PWM时,要调用此函数,否则无法正常输出PWM
  25. void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState);
  26. //使能定时器产生的中断信号
  27. void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState);
  28. void TIM_GenerateEvent(TIM_TypeDef* TIMx, uint16_t TIM_EventSource);
  29. void TIM_DMAConfig(TIM_TypeDef* TIMx, uint16_t TIM_DMABase, uint16_t TIM_DMABurstLength);
  30. void TIM_DMACmd(TIM_TypeDef* TIMx, uint16_t TIM_DMASource, FunctionalState NewState);
  31. //选择内部时钟,如果不用此函数,芯片默认也是使用内部时钟,追求严谨可加上此函数
  32. void TIM_InternalClockConfig(TIM_TypeDef* TIMx);
  33. //选择ITRx其他定时器的时钟
  34. void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource);
  35. //选择TIx捕获通道的时钟
  36. void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_TIxExternalCLKSource,
  37. uint16_t TIM_ICPolarity, uint16_t ICFilter);
  38. //选择ETR通过外部时钟模式1输入的时钟
  39. void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  40. uint16_t ExtTRGFilter);
  41. //选择ETR通过外部时钟模式2输入的时钟
  42. void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  43. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter);
  44. //单独用来配置ETR引脚的预分频器、极性、滤波器这些参数
  45. void TIM_ETRConfig(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  46. uint16_t ExtTRGFilter);
  47. //直接单独配置预分配值PSC,参数2是写入模式,可选择是听从安排,在更新事件生效;或者是在写入后手动
  48. //产生一个更新事件,让这个值立刻生效
  49. void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode);
  50. //改变计数器的计数模式
  51. void TIM_CounterModeConfig(TIM_TypeDef* TIMx, uint16_t TIM_CounterMode);
  52. void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource);
  53. void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,
  54. uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity);
  55. //配置强制输出模式,运行中想要强制改变输出的波形,直接输出高或低电平就用这个
  56. //不过其效果和改变PWM占空比100%或0%的效果是一样的
  57. void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
  58. void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
  59. void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
  60. void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
  61. //自动重装器预装功能配置
  62. void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState);
  63. void TIM_SelectCOM(TIM_TypeDef* TIMx, FunctionalState NewState);
  64. void TIM_SelectCCDMA(TIM_TypeDef* TIMx, FunctionalState NewState);
  65. void TIM_CCPreloadControl(TIM_TypeDef* TIMx, FunctionalState NewState);
  66. //配置CCR寄存器的预装功能,也就是影子寄存器(写入的值不会立即生效,而是在事件更新后才生效)
  67. void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
  68. void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
  69. void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
  70. void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
  71. //配置快速使能
  72. void TIM_OC1FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
  73. void TIM_OC2FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
  74. void TIM_OC3FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
  75. void TIM_OC4FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
  76. //外部事件时清除REF信号
  77. void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
  78. void TIM_ClearOC2Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
  79. void TIM_ClearOC3Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
  80. void TIM_ClearOC4Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
  81. //用来单独设置输出比较极性的,带N的就是高级定时器的互补通道的配置,OC4无互补通道
  82. void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
  83. void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
  84. void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
  85. void TIM_OC2NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
  86. void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
  87. void TIM_OC3NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
  88. void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
  89. //单独修改输出使能参数的
  90. void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx);
  91. void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN);
  92. //单独更改输出比较模式
  93. void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode);
  94. void TIM_UpdateDisableConfig(TIM_TypeDef* TIMx, FunctionalState NewState);
  95. void TIM_UpdateRequestConfig(TIM_TypeDef* TIMx, uint16_t TIM_UpdateSource);
  96. void TIM_SelectHallSensor(TIM_TypeDef* TIMx, FunctionalState NewState);
  97. void TIM_SelectOnePulseMode(TIM_TypeDef* TIMx, uint16_t TIM_OPMode);
  98. void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_TRGOSource);
  99. void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode);
  100. void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_MasterSlaveMode);
  101. //给计数器直接写入一个值
  102. void TIM_SetCounter(TIM_TypeDef* TIMx, uint16_t Counter);
  103. //给自动重装值直接写入一个值
  104. void TIM_SetAutoreload(TIM_TypeDef* TIMx, uint16_t Autoreload);
  105. //单独用来更改CCR寄存器值的,PWM输出更改占空比就用这个函数
  106. void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1);
  107. void TIM_SetCompare2(TIM_TypeDef* TIMx, uint16_t Compare2);
  108. void TIM_SetCompare3(TIM_TypeDef* TIMx, uint16_t Compare3);
  109. void TIM_SetCompare4(TIM_TypeDef* TIMx, uint16_t Compare4);
  110. void TIM_SetIC1Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);
  111. void TIM_SetIC2Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);
  112. void TIM_SetIC3Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);
  113. void TIM_SetIC4Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);
  114. void TIM_SetClockDivision(TIM_TypeDef* TIMx, uint16_t TIM_CKD);
  115. uint16_t TIM_GetCapture1(TIM_TypeDef* TIMx);
  116. uint16_t TIM_GetCapture2(TIM_TypeDef* TIMx);
  117. uint16_t TIM_GetCapture3(TIM_TypeDef* TIMx);
  118. uint16_t TIM_GetCapture4(TIM_TypeDef* TIMx);
  119. //获取当前计数器的值,返回值就是当前计数器的值
  120. uint16_t TIM_GetCounter(TIM_TypeDef* TIMx);
  121. //获取当前预分频器的值,返回值就是当前预分频器的值
  122. uint16_t TIM_GetPrescaler(TIM_TypeDef* TIMx);
  123. //获取TIM标志位
  124. FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
  125. //清除TIM标志位
  126. void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
  127. //获取定时器中断标志位
  128. ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT);
  129. //清除定时器中断标志位
  130. void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT);

6-2 定时器定时中断&定时器外部时钟

main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "delay.h"
  3. #include "timer.h"
  4. #include "oled.h"
  5. int16_t num = 0;
  6. //任务1 定时器定时中断
  7. //OLED的SCL连B8,SDA连B9,显示屏上显示时间秒
  8. void task01()
  9. {
  10. OLED_Init();
  11. Task01_Timer_Init();
  12. OLED_ShowString(1,1,"Num:");
  13. while(1)
  14. {
  15. OLED_ShowNum(1,5,num,6);
  16. }
  17. }
  18. //任务2 定时器外部时钟
  19. //对射式红外传感器接A0,遮挡传感器计数值count+1,满十次使num+1
  20. void task02()
  21. {
  22. OLED_Init();
  23. Task02_Timer_Init();
  24. OLED_ShowString(1,1,"Count:");
  25. while(1)
  26. {
  27. OLED_ShowNum(1,6,Timer_GetCounter(),6);
  28. }
  29. }
  30. int main(void)
  31. {
  32. while (1)
  33. {
  34. }
  35. }
  36. //TIM2中断回调函数
  37. void TIM2_IRQHandler(void)
  38. {
  39. if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
  40. {
  41. num++;
  42. TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
  43. }
  44. }

time.c

  1. #include "Device/Include/stm32f10x.h" // Device header
  2. /*
  3. 开启时钟步骤
  4. 1.RCC开启时钟
  5. 2.选择时基单元的时钟源,对于定时中断,选择内部时钟源
  6. 3.配置时基单元,包括预分频器PSC,自动重装器ARR,计数器CNT等
  7. 4.配置输出中断控制,允许更新中断输出到NVIC
  8. 5.配置NVIC,在NVIC中打开定时器中断的通道,并分配优先级
  9. 6.运行控制,使能定时器,编写中断函数
  10. */
  11. void Task01_Timer_Init()//初始化TIM2通用定时器
  12. {
  13. //开启时钟
  14. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  15. //选择时基单元的时钟
  16. TIM_InternalClockConfig(TIM2);
  17. //初始化时基单元 定时频率 = 72M / (PSC + 1) / (ARR + 1)
  18. TIM_TimeBaseInitTypeDef My_TimerBaseInitstructure;
  19. My_TimerBaseInitstructure.TIM_ClockDivision = TIM_CKD_DIV1; //预分频值
  20. My_TimerBaseInitstructure.TIM_CounterMode = TIM_CounterMode_Up; //计数模式
  21. My_TimerBaseInitstructure.TIM_Period = 10000 - 1; //ARR自动重装器的值
  22. My_TimerBaseInitstructure.TIM_Prescaler = 7200 - 1; //PCS预分频器的值
  23. My_TimerBaseInitstructure.TIM_RepetitionCounter = 0; //重复计数器的值
  24. TIM_TimeBaseInit(TIM2,&My_TimerBaseInitstructure);
  25. TIM_ClearFlag(TIM2,TIM_CounterMode_Up);//避免刚初始化完就进行一次中断
  26. //使能定时器中断,TIM_IT_Update为更新中断
  27. TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
  28. //配置中断控制器NVIC
  29. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  30. //配置中断通道以及优先级相关,开启中断
  31. NVIC_InitTypeDef My_NVICStructure;
  32. My_NVICStructure.NVIC_IRQChannel = TIM2_IRQn;
  33. My_NVICStructure.NVIC_IRQChannelCmd = ENABLE;
  34. My_NVICStructure.NVIC_IRQChannelPreemptionPriority = 2;
  35. My_NVICStructure.NVIC_IRQChannelSubPriority = 1;
  36. NVIC_Init(&My_NVICStructure);
  37. //启动定时器
  38. TIM_Cmd(TIM2,ENABLE);
  39. }
  40. //编写TIM2的中断函数模板
  41. //void TIM2_IRQHandler(void)
  42. //{
  43. // if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
  44. // {
  45. //
  46. // TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
  47. // }
  48. //}
  49. void Task02_Timer_Init()//初始化TIM2通用定时器
  50. {
  51. //开启TIM2时钟
  52. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  53. //通过ETR引脚的外部时钟模式2配置
  54. TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x00);
  55. //初始化时基单元 定时频率 = 72M / (PSC + 1) / (ARR + 1)
  56. TIM_TimeBaseInitTypeDef My_TimerBaseInitstructure;
  57. My_TimerBaseInitstructure.TIM_ClockDivision = TIM_CKD_DIV1; //预分频值
  58. My_TimerBaseInitstructure.TIM_CounterMode = TIM_CounterMode_Up; //计数模式
  59. My_TimerBaseInitstructure.TIM_Period = 10000 - 1; //ARR自动重装器的值
  60. My_TimerBaseInitstructure.TIM_Prescaler = 7200 - 1; //PCS预分频器的值
  61. My_TimerBaseInitstructure.TIM_RepetitionCounter = 0; //重复计数器的值
  62. TIM_TimeBaseInit(TIM2,&My_TimerBaseInitstructure);
  63. TIM_ClearFlag(TIM2,TIM_CounterMode_Up);//避免刚初始化完就进行一次中断
  64. //使能定时器中断,TIM_IT_Update为更新中断
  65. TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
  66. //配置中断控制器NVIC
  67. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  68. //配置中断通道以及优先级相关,开启中断
  69. NVIC_InitTypeDef My_NVICStructure;
  70. My_NVICStructure.NVIC_IRQChannel = TIM2_IRQn;
  71. My_NVICStructure.NVIC_IRQChannelCmd = ENABLE;
  72. My_NVICStructure.NVIC_IRQChannelPreemptionPriority = 2;
  73. My_NVICStructure.NVIC_IRQChannelSubPriority = 1;
  74. NVIC_Init(&My_NVICStructure);
  75. //启动定时器
  76. TIM_Cmd(TIM2,ENABLE);
  77. //配置GPIO口
  78. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  79. GPIO_InitTypeDef My_GPIO_InitStructure;
  80. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  81. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  82. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  83. GPIO_Init(GPIOA,&My_GPIO_InitStructure);
  84. }
  85. uint16_t Timer_GetCounter(void)
  86. {
  87. return TIM_GetCounter(TIM2);
  88. }

6-3TIM输出捕获

PWM配置步骤:

1.开启RCC时钟,将要使用的定时器和GPIO外设时钟打开

2.配置时基单元,时钟源选择

3.配置输出比较单元,设置CCR的值,输出比较模式,极性选择,输出使能这些参数

4.配置GPIO,初始化为复用推挽输出的配置

5.运行控制,启动计数器

6-4 PWM驱动LED呼吸灯&PWM驱动直流电机

一个定时器TIM有四个通道,每个通道均可输出PWM波,但当我们用一个定时器输出4路PWM时,因为共用一个计数器,频率必须是一样的,占空比由各自通道的CCR决定,四路通道的相位也是一样的。

main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "delay.h"
  3. #include "OLED.h"
  4. #include "PWM.h"
  5. #include "servo.h"
  6. #include "motor.h"
  7. //任务1 PWM驱动LED呼吸灯
  8. //A0接LED
  9. void task01()
  10. {
  11. uint8_t i;
  12. OLED_Init();
  13. Task01_PWM_Init();
  14. while(1)
  15. {
  16. for(i = 0;i < 100;i++)
  17. {
  18. Task01_PWM_SetComparel(i);
  19. Delay_ms(10);
  20. }
  21. for(i = 0;i < 100;i++)
  22. {
  23. Task01_PWM_SetComparel(100-i);
  24. Delay_ms(10);
  25. }
  26. }
  27. }
  28. //任务2 PWM驱动舵机
  29. //A1接舵机的信号线 PA1是TIM2的CH2 B1接一个按键控制PWM改变,即把io口配置为上拉输入
  30. void Task02()
  31. {
  32. OLED_Init();
  33. Servo_Init();
  34. Servo_SetAngle(0);//初始把舵机设置为0度
  35. uint16_t Angle = 0;
  36. while(1)
  37. {
  38. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == RESET)//检测按键是否按下
  39. {
  40. Delay_ms(10);
  41. while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == RESET);
  42. Angle += 20;
  43. }
  44. // for(Angle = 0;Angle <= 180; Angle+=20)
  45. // {
  46. // Servo_SetAngle(Angle);
  47. // Delay_ms(50);
  48. // }
  49. // for(Angle = 0;Angle <= 180; Angle+=20)
  50. // {
  51. // Servo_SetAngle(180-Angle);
  52. // Delay_ms(50);
  53. // }
  54. if(Angle > 180)
  55. {
  56. Angle = 0;
  57. }
  58. }
  59. }
  60. //任务3 PWM驱动直流电机
  61. //用TB6612来驱动直流电机
  62. /*
  63. AO1接电机正极,AO2接电机负极 VM接5V VCC接3.3V GND共地 STBY接3.3V
  64. PWMA接PA2,AIN1接PA4,AIN2接PA5 PA2是TIM2的CH3
  65. 按键接B1,端口为上拉输入
  66. */
  67. void Task03()
  68. {
  69. OLED_Init();
  70. Task03_PWM_Init();
  71. int8_t temp = 0;//Speed的范围是0到100
  72. while(1)
  73. {
  74. if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == RESET)
  75. {
  76. while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == RESET){}
  77. temp+=20;
  78. if(temp > 100)
  79. {
  80. temp = -100;
  81. }
  82. Motor_SetSpeed(temp);
  83. }
  84. }
  85. }
  86. int main(void)
  87. {
  88. Task03();
  89. while (1)
  90. {
  91. }
  92. }

 PWM.c

  1. #include "PWM.h"
  2. /*
  3. PWM配置步骤:
  4. 1.开启RCC时钟,将要使用的定时器和GPIO外设时钟打开
  5. 2.配置时基单元,时钟源选择
  6. 3.配置输出比较单元,设置CCR的值,输出比较模式,极性选择,输出使能这些参数
  7. 4.配置GPIO,初始化为复用推挽输出的配置
  8. 5.运行控制,启动计数器
  9. PWM频率: Freq = CK_PSC / (PSC + 1) / (ARR + 1)
  10. PWM占空比: Duty = CCR / (ARR + 1)
  11. PWM分辨率: Reso = 1 / (ARR + 1)
  12. */
  13. void Task01_PWM_Init()
  14. {
  15. //RCC开启
  16. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  17. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  18. //重映射将TIM2的CH1的PWM输出转到复用引脚
  19. // RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  20. // GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);
  21. // GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
  22. //选择内部时钟
  23. TIM_InternalClockConfig(TIM2);
  24. //初始化时基单元
  25. TIM_TimeBaseInitTypeDef My_TimeBaceInitStructure;
  26. My_TimeBaceInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //选择预分配倍数
  27. My_TimeBaceInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //选择向上计数模式
  28. My_TimeBaceInitStructure.TIM_Period = 100 - 1; //ARR
  29. My_TimeBaceInitStructure.TIM_Prescaler = 720 - 1; //PSC
  30. My_TimeBaceInitStructure.TIM_RepetitionCounter = 0;
  31. TIM_TimeBaseInit(TIM2,&My_TimeBaceInitStructure);
  32. //配置输出比较单元
  33. TIM_OCInitTypeDef My_TIM_OCInitStructure;
  34. TIM_OCStructInit(&My_TIM_OCInitStructure);
  35. My_TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  36. My_TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  37. My_TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  38. My_TIM_OCInitStructure.TIM_Pulse = 30;//CCR
  39. TIM_OC1Init(TIM2,&My_TIM_OCInitStructure);
  40. //配置GPIO输出,A0为TIM2_CH1
  41. GPIO_InitTypeDef My_GPIO_InitStructure;
  42. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIO_Pin_15 TIM2CH1复用时使用
  43. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
  44. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  45. GPIO_Init(GPIOA,&My_GPIO_InitStructure);
  46. //启动定时器
  47. TIM_Cmd(TIM2,ENABLE);
  48. }
  49. void Task01_PWM_SetComparel(uint16_t Compare)//输入值改变占空比
  50. {
  51. TIM_SetCompare1(TIM2,Compare);
  52. }
  53. void Task02_PWM_Init()
  54. {
  55. //开启RCC
  56. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //开启TIM2的时钟
  57. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //A1是PWM的CH2输出口
  58. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //开启GPIOB的时钟,B1要接一个按键控制pwm值改变,这样就可以靠按键实现舵机转动
  59. //配置GPIO输出,A1为TIM2_CH2
  60. GPIO_InitTypeDef My_GPIOInitStructure;
  61. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_1;
  62. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
  63. My_GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  64. GPIO_Init(GPIOA,&My_GPIOInitStructure);
  65. //再配置按键的B1
  66. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_1;
  67. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
  68. My_GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  69. GPIO_Init(GPIOB,&My_GPIOInitStructure);
  70. //选择TIM的时钟源,选内部时钟
  71. TIM_InternalClockConfig(TIM2);
  72. //初始化时基单元
  73. TIM_TimeBaseInitTypeDef My_TimeBaceInitStructure;
  74. My_TimeBaceInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  75. My_TimeBaceInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  76. My_TimeBaceInitStructure.TIM_Period = 20000 - 1;//ARR
  77. My_TimeBaceInitStructure.TIM_Prescaler = 72 - 1;//PSC
  78. My_TimeBaceInitStructure.TIM_RepetitionCounter = 30;
  79. TIM_TimeBaseInit(TIM2,&My_TimeBaceInitStructure);
  80. //配置输出比较单元
  81. TIM_OCInitTypeDef My_OCInitStructure;
  82. TIM_OCStructInit(&My_OCInitStructure);
  83. My_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  84. My_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  85. My_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  86. My_OCInitStructure.TIM_Pulse = 0;//CCR 500到2500
  87. TIM_OC2Init(TIM2,&My_OCInitStructure);
  88. //启动定时器
  89. TIM_Cmd(TIM2,ENABLE);
  90. }
  91. void Task02_PWM_SetComparel(uint16_t Compare)//输入数值改变PWM占空比
  92. {
  93. TIM_SetCompare2(TIM2,Compare);
  94. }
  95. void Task03_PWM_Init()
  96. {
  97. //开启RCC时钟
  98. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  99. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  100. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  101. //配置时基单元,选择时钟源
  102. TIM_InternalClockConfig(TIM2);
  103. TIM_TimeBaseInitTypeDef My_TimeBaseInitStructure;
  104. My_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  105. My_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  106. My_TimeBaseInitStructure.TIM_Period = 20000 - 1;//ARR
  107. My_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;//PSC
  108. My_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  109. TIM_TimeBaseInit(TIM2,&My_TimeBaseInitStructure);
  110. //配置输出比较单元
  111. TIM_OCInitTypeDef My_TIM_OCInitStructure;
  112. TIM_OCStructInit(&My_TIM_OCInitStructure);
  113. My_TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  114. My_TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  115. My_TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  116. My_TIM_OCInitStructure.TIM_Pulse = 30;//CCR
  117. TIM_OC3Init(TIM2,&My_TIM_OCInitStructure);
  118. //配置GPIO A4和A5分别是AIN1和AIN2 按键是B1
  119. //并且IO口如果不是默认功能,要使用复用功能时,也要配置 A2是TIM2的CH3
  120. GPIO_InitTypeDef My_GPIOInitStructure;
  121. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_1;
  122. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_IPU;
  123. My_GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  124. GPIO_Init(GPIOB,&My_GPIOInitStructure);//配置PB1为按键,上拉输入
  125. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_4;
  126. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  127. GPIO_Init(GPIOA,&My_GPIOInitStructure);
  128. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_5;
  129. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  130. GPIO_Init(GPIOA,&My_GPIOInitStructure);//PA4和PA5是控制电机旋转方向的,推挽输出即可
  131. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_2;
  132. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//设置PA2为复用推挽输出,这样就可以输出PWM了
  133. GPIO_Init(GPIOA,&My_GPIOInitStructure);
  134. //启动控制器
  135. TIM_Cmd(TIM2,ENABLE);
  136. }
  137. //输入改变TIM2的CH3的PWM占空比
  138. void Task03_PWM_SetComparel(uint16_t Compare)
  139. {
  140. TIM_SetCompare3(TIM2,Compare);
  141. }

servo.c

  1. #include "Device/Include/stm32f10x.h" // Device header
  2. #include "PWM.h"
  3. void Servo_Init(void)
  4. {
  5. Task02_PWM_Init();
  6. }
  7. //0度对于500,180度对于2500
  8. void Servo_SetAngle(float Angle)
  9. {
  10. Task02_PWM_SetComparel(Angle / 180 * 2000 + 500);
  11. }

 motor.c

  1. #include "motor.h"
  2. void Motor_Init()
  3. {
  4. //初始化AIN1和AIN2两个引脚,控制正反转
  5. RCC_APB2PeriphClockCmd(RCC_APBPeriph_AIN,ENABLE);
  6. GPIO_InitTypeDef My_GPIO_InitStructure;
  7. My_GPIO_InitStructure.GPIO_Pin = AIN1 | AIN2;
  8. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  9. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  10. GPIO_Init(AIN1_Port,&My_GPIO_InitStructure);
  11. //默认将电机锁住,防止乱转
  12. GPIO_ResetBits(AIN1_Port,AIN1);
  13. GPIO_ResetBits(AIN1_Port,AIN2);
  14. }
  15. void Motor_SetSpeed(int8_t Speed)
  16. {
  17. if(Speed > 0)//正转
  18. {
  19. GPIO_SetBits(AIN1_Port,AIN1);
  20. GPIO_ResetBits(AIN1_Port,AIN2);
  21. Task03_PWM_SetComparel(Speed);
  22. }
  23. if(Speed < 0)//反转
  24. {
  25. GPIO_ResetBits(AIN1_Port,AIN1);
  26. GPIO_SetBits(AIN1_Port,AIN2);
  27. Task03_PWM_SetComparel(-Speed);
  28. }
  29. }

6-5 TIM输入捕获

高级定时器和基本定时器有输入捕获通道,但基本定时器没有。 

PWMI模式是专门测量输入的频率和占空比的。

TIM输入捕获也可配置为主从触发模式,从而实现硬件全自动测量。

测频率就是测量一秒钟内有几个周期,测频法适用于测量高频率,测周法适用于测低频率,频率是高还是低就看中介频率。

配置输入捕获的步骤:

1.RCC开启时钟,把GPIO和TIM的时钟打开

2.GPIO初始化,将GPIO配置成输入模式,上拉输入或浮空输入

3.配置时基单元,让CNT计数器在内部时钟的驱动下自增运行

4.配置输入捕获单元,包括滤波器、极性、直连通道还是交叉通道,分频器这些参数

5.选择从模式的触发源,这里选择TI1FP1

6.选择触发后执行的操作,执行Reset操作

7.调用TIM_Cmd函数,开启定时器

6-6 输入捕获模式测频率&PWMI模式测频率占空比

main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "delay.h"
  3. #include "task01.h"
  4. #include "task02.h"
  5. #include "OLED.h"
  6. //任务1 输入捕获模式测频率
  7. //PA0输出PWM信号,用PA6输入捕获来测量信号
  8. //PA0是TIM2的CH1 PA6是TIM3的CH1
  9. void Task01()
  10. {
  11. OLED_Init();
  12. Task01_Init();
  13. OLED_ShowString(1,1,"Freq:00000Hz");
  14. Task01_PWM_SetPrescaler(720 - 1); //Freq = 72M / (PSC + 1) / 100
  15. Task01_PWM_SetComparel(50); //Duty = CCR / 100
  16. while(1)
  17. {
  18. OLED_ShowNum(1,6,Task01_IC_GetFreq(),5);
  19. }
  20. }
  21. //任务2 PWMI模数测频率占空比
  22. void Task02()
  23. {
  24. OLED_Init();
  25. Task02_Init();
  26. OLED_ShowString(1,1,"Freq:00000Hz");
  27. OLED_ShowString(2,1,"Duty:00%");
  28. Task01_PWM_SetPrescaler(7200 - 1); //Freq = 72M / (PSC + 1) / 100
  29. Task01_PWM_SetComparel(50); //Duty = CCR / 100
  30. while(1)
  31. {
  32. OLED_ShowNum(1,6,Task02_IC_GetFreq(),5);
  33. OLED_ShowNum(2,6,Task02_IC_GetDuty(),2);
  34. }
  35. }
  36. int main(void)
  37. {
  38. // Task02();
  39. while (1)
  40. {
  41. }
  42. }

 task01.c

  1. #include "task01.h"
  2. //PA0和PA6
  3. void Task01_Init()
  4. {
  5. //初始化时钟
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  7. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  8. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  9. //选择内部时钟
  10. TIM_InternalClockConfig(TIM2);
  11. TIM_InternalClockConfig(TIM3);
  12. //配置TIM2时基单元
  13. TIM_TimeBaseInitTypeDef My_TIM_TimeBaseInitStructure;
  14. My_TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  15. My_TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  16. My_TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;//ARR
  17. My_TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1;//PSC
  18. My_TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  19. TIM_TimeBaseInit(TIM2,&My_TIM_TimeBaseInitStructure);//TIM2的CH1作为PWM输出
  20. //配置PA0io口作为PWM输出
  21. GPIO_InitTypeDef My_GPIOInitStructure;
  22. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_0;
  23. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  24. My_GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  25. GPIO_Init(GPIOA,&My_GPIOInitStructure);
  26. //设置输出比较单元
  27. TIM_OCInitTypeDef My_TIM_OCInitStructure;
  28. TIM_OCStructInit(&My_TIM_OCInitStructure);
  29. My_TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出模式选择
  30. My_TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//极性选择
  31. My_TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//使能
  32. My_TIM_OCInitStructure.TIM_Pulse = 20;//RCC
  33. TIM_OC1Init(TIM2,&My_TIM_OCInitStructure);
  34. //启动定时器TIM2
  35. TIM_Cmd(TIM2,ENABLE);
  36. //配置TIM3时基单元
  37. My_TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  38. My_TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  39. My_TIM_TimeBaseInitStructure.TIM_Period = 65535 - 1;//ARR
  40. My_TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;//PSC
  41. My_TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  42. TIM_TimeBaseInit(TIM3,&My_TIM_TimeBaseInitStructure);//TIM3的CH1作为输入捕获
  43. //配置TIM3输入捕获单元
  44. TIM_ICInitTypeDef My_TIM_InitStructure;
  45. My_TIM_InitStructure.TIM_Channel = TIM_Channel_1;//选择定时器通道
  46. My_TIM_InitStructure.TIM_ICFilter = 0xF;//滤波数值
  47. My_TIM_InitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//触发模式,选上升沿触发
  48. My_TIM_InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//预分频值
  49. My_TIM_InitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//选择触发信号从哪个引脚输入
  50. TIM_ICInit(TIM3,&My_TIM_InitStructure);
  51. //配置PA6GPIO口为输入模式
  52. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_6;
  53. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
  54. My_GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  55. GPIO_Init(GPIOA,&My_GPIOInitStructure);
  56. //配置从模式的触发源
  57. TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
  58. //配置从模式为Reset
  59. TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
  60. //启动定时器TIM3
  61. TIM_Cmd(TIM3,ENABLE);
  62. }
  63. void Task01_PWM_SetComparel(uint16_t Comparel)
  64. {
  65. TIM_SetCompare1(TIM2,Comparel);
  66. }
  67. void Task01_PWM_SetPrescaler(uint16_t Prescaler)
  68. {
  69. TIM_PrescalerConfig(TIM2,Prescaler,TIM_PSCReloadMode_Immediate);
  70. }
  71. uint32_t Task01_IC_GetFreq(void)
  72. {
  73. return 1000000 / (TIM_GetCapture1(TIM3) + 1);//+1消除误差
  74. }

 task02.c

  1. #include "task02.h"
  2. void Task02_Init()
  3. {
  4. //初始化时钟
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  7. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  8. //选择内部时钟
  9. TIM_InternalClockConfig(TIM2);
  10. TIM_InternalClockConfig(TIM3);
  11. //配置TIM2时基单元
  12. TIM_TimeBaseInitTypeDef My_TIM_TimeBaseInitStructure;
  13. My_TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  14. My_TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  15. My_TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;//ARR
  16. My_TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1;//PSC
  17. My_TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  18. TIM_TimeBaseInit(TIM2,&My_TIM_TimeBaseInitStructure);//TIM2的CH1作为PWM输出
  19. //配置PA0io口作为PWM输出
  20. GPIO_InitTypeDef My_GPIOInitStructure;
  21. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_0;
  22. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  23. My_GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24. GPIO_Init(GPIOA,&My_GPIOInitStructure);
  25. //设置输出比较单元
  26. TIM_OCInitTypeDef My_TIM_OCInitStructure;
  27. TIM_OCStructInit(&My_TIM_OCInitStructure);
  28. My_TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出模式选择
  29. My_TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//极性选择
  30. My_TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//使能
  31. My_TIM_OCInitStructure.TIM_Pulse = 20;//RCC
  32. TIM_OC1Init(TIM2,&My_TIM_OCInitStructure);
  33. //启动定时器TIM2
  34. TIM_Cmd(TIM2,ENABLE);
  35. //配置TIM3时基单元
  36. My_TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  37. My_TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  38. My_TIM_TimeBaseInitStructure.TIM_Period = 65535 - 1;//ARR
  39. My_TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;//PSC
  40. My_TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  41. TIM_TimeBaseInit(TIM3,&My_TIM_TimeBaseInitStructure);//TIM3的CH1作为输入捕获
  42. //配置TIM3输入捕获单元
  43. TIM_ICInitTypeDef My_TIM_InitStructure;
  44. My_TIM_InitStructure.TIM_Channel = TIM_Channel_1;//选择定时器通道
  45. My_TIM_InitStructure.TIM_ICFilter = 0xF;//滤波数值
  46. My_TIM_InitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//触发模式,选上升沿触发
  47. My_TIM_InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//预分频值
  48. My_TIM_InitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//选择触发信号从哪个引脚输入
  49. TIM_ICInit(TIM3,&My_TIM_InitStructure);
  50. //裴PWMI
  51. TIM_PWMIConfig(TIM3,&My_TIM_InitStructure);
  52. //配置PA6GPIO口为输入模式
  53. My_GPIOInitStructure.GPIO_Pin = GPIO_Pin_6;
  54. My_GPIOInitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
  55. My_GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  56. GPIO_Init(GPIOA,&My_GPIOInitStructure);
  57. //配置从模式的触发源
  58. TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
  59. //配置从模式为Reset
  60. TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
  61. //启动定时器TIM3
  62. TIM_Cmd(TIM3,ENABLE);
  63. }
  64. void Task02_PWM_SetComparel(uint16_t Comparel)
  65. {
  66. TIM_SetCompare1(TIM2,Comparel);
  67. }
  68. void Task02_PWM_SetPrescaler(uint16_t Prescaler)
  69. {
  70. TIM_PrescalerConfig(TIM2,Prescaler,TIM_PSCReloadMode_Immediate);
  71. }
  72. uint32_t Task02_IC_GetFreq(void)
  73. {
  74. return 1000000 / (TIM_GetCapture1(TIM3) + 1);//+1消除误差
  75. }
  76. uint32_t Task02_IC_GetDuty(void)
  77. {
  78. return (TIM_GetCapture2(TIM3) + 1) * 100 / (TIM_GetCapture1(TIM3) + 1);
  79. }


6-7 TIM编码器接口

配置编码器测量步骤:

1.RCC开启时钟,开启GPIO和定时器时钟

2.配置相关GPIO,将CH1和CH2的IO口设置为输入模式

3.配置时基单元,预分频器一般不分频,自动重装一般给最大65535

4.配置输入捕获单元,只需配置通道、滤波器和极性即可,其他参数用不上

5.配置编码器接口模式,直接调用库函数

6.调用TIM_Cmd启动定时器


6-8 编码器接口测速

main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "encoder.h"
  3. #include "OLED_I2C.h"
  4. #include "delay.h"
  5. #include <stdio.h>
  6. //编码器接口测速
  7. //PA6接A相 PA7接B相 分别是TIM3的CH1 和 TIM3的CH2
  8. uint16_t temp = 0;
  9. void DisplayIntegerValue(int value)
  10. {
  11. char str[16]; // 用于存储转换后的字符串
  12. snprintf(str, sizeof(str), "temp: %d", value); // 将整数转换为字符串
  13. OLED_ShowStr(1, 1, (unsigned char *)str, 1); // 在OLED上显示字符串
  14. }
  15. int main(void)
  16. {
  17. DelayInit();
  18. I2C_Configuration();
  19. OLED_Init();
  20. Encoder_Init();
  21. OLED_Fill(0xFF);
  22. DelayS(2);
  23. OLED_CLS();
  24. while (1)
  25. {
  26. temp = Encoder_Get();
  27. DisplayIntegerValue(temp);
  28. DelayS(1);
  29. }
  30. }

 encoder.c

  1. #include "encoder.h"
  2. void Encoder_Init()
  3. {
  4. //开启RCC时钟
  5. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  7. //配置GPIO,CH1和CH2都配成输入模式 PA6 PA7
  8. GPIO_InitTypeDef My_GPIO_InitStructure;
  9. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  10. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  11. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOA,&My_GPIO_InitStructure);
  13. //配置时基单元
  14. TIM_TimeBaseInitTypeDef My_TIM_TimeBaseInitStructure;
  15. My_TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  16. My_TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  17. My_TIM_TimeBaseInitStructure.TIM_Period = 65535 - 1;//ARR
  18. My_TIM_TimeBaseInitStructure.TIM_Prescaler = 1-1;//PSC
  19. My_TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  20. TIM_TimeBaseInit(TIM3,&My_TIM_TimeBaseInitStructure);
  21. //配置输入捕获单元
  22. TIM_ICInitTypeDef My_TIM_ICInitStructure;
  23. TIM_ICStructInit(&My_TIM_ICInitStructure);
  24. My_TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
  25. My_TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  26. My_TIM_ICInitStructure.TIM_ICFilter = 0xF;
  27. TIM_ICInit(TIM3,&My_TIM_ICInitStructure);
  28. My_TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  29. My_TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  30. My_TIM_ICInitStructure.TIM_ICFilter = 0xF;
  31. TIM_ICInit(TIM3,&My_TIM_ICInitStructure);
  32. //配置编码器接口
  33. TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
  34. //启动定时器
  35. TIM_Cmd(TIM2,ENABLE);
  36. }
  37. int16_t Encoder_Get(void)
  38. {
  39. int16_t temp;
  40. temp = TIM_GetCounter(TIM3);
  41. TIM_SetCounter(TIM3,0);
  42. return temp;
  43. }


7-1 ADC模数转换器

  以下是ADC的库函数:

  1. //恢复初始化
  2. void ADC_DeInit(ADC_TypeDef* ADCx);
  3. //ADC模块初始化
  4. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct);
  5. //ADC配置结构体初始化
  6. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct);
  7. //控制ADC开启或关闭
  8. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  9. //开启DMA输出信号的
  10. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  11. //中断输出控制
  12. void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState);
  13. //复位校准
  14. void ADC_ResetCalibration(ADC_TypeDef* ADCx);
  15. //获取复位校准状态
  16. FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx);
  17. //开始校准
  18. void ADC_StartCalibration(ADC_TypeDef* ADCx);
  19. //获取开始校准状态
  20. FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx);
  21. //ADC软件开始转换控制,调用就能软件触发转换
  22. void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  23. //ADC获取软件开始转换状态,一般很少用
  24. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx);
  25. //下面两个函数是配置间断模式的
  26. //每个几个通道间断一次
  27. void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number);
  28. //是否启动间断模式
  29. void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  30. //ADC规则组通道测试
  31. void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  32. //ADC外部触发转换控制,即是否允许外部触发转换
  33. void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  34. //ADC获取转换值
  35. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
  36. //ADC获取双模式转换值
  37. uint32_t ADC_GetDualModeConversionValue(void);
  38. //这部分都是对注入组进行配置的
  39. void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  40. void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  41. void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t
  42. ADC_ExternalTrigInjecConv);
  43. void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  44. void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  45. FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx);
  46. void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  47. void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length);
  48. void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset);
  49. uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel);
  50. //是否启动模拟看门狗
  51. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog);
  52. //配置高低阈值
  53. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t
  54. LowThreshold);
  55. //配置看门的通道
  56. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel);
  57. //ADC温度传感器,内部参考电压控制
  58. void ADC_TempSensorVrefintCmd(FunctionalState NewState);
  59. //获取标志位状态
  60. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  61. //清除标志位
  62. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  63. //获取中断标志位
  64. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  65. //清除中断挂起位
  66. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT);


7-2 AD单通道&AD多通道

STM32f103c8t6只有ADC1和ADC2,且无PC部分的端口

有四种转换模式

配置ADC的步骤:

1.开启RCC时钟,包括ADC和GPIO时钟,ADCCLK的分配器也需配置RCC_用ADCCLKConfig()

2.配置GPIO,将信号输入的端口配置成模拟输入

3.配置多路开关,将左边的通道接入到右边的规则组列表里

4.配置ADC转换器,结构体里面的参数有选择转换模式,是否扫描,通道个数设置,触发源设置,数据对齐方式等。

5.开启ADC,调用ADC_Cmd函数。如若需要,可加入校准步骤。

 main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "OLED.h"
  3. #include "delay.h"
  4. #include "task01.h"
  5. #include "task02.h"
  6. //任务1 AD单通道
  7. //电位器:一个可调电阻,有三个端口,其中两个连接正负极,一个连接A0
  8. uint16_t task01_AD_value;
  9. float task01_Volatge;
  10. void Task01()
  11. {
  12. OLED_Init();
  13. Task01_Init();
  14. OLED_ShowString(1,1,"ADValue:");
  15. OLED_ShowString(2,1,"Volatge:0.00V");
  16. while(1)
  17. {
  18. task01_AD_value = Task01_AD_GetValue();
  19. task01_Volatge = (float)task01_AD_value / 4095 * 3.3;
  20. OLED_ShowNum(1,9,task01_AD_value,4);
  21. OLED_ShowNum(2,9,task01_Volatge,1);
  22. OLED_ShowNum(2,11,(uint16_t)(task01_Volatge * 100) % 100,2);
  23. Delay_ms(100);
  24. }
  25. }
  26. //任务2 AD多通道
  27. //PA0接电位器 PA1接光温传感器 PA2接热敏传感器 PA3接反射式红外传感器
  28. uint16_t AD0;
  29. uint16_t AD1;
  30. uint16_t AD2;
  31. uint16_t AD3;
  32. void Task02()
  33. {
  34. OLED_Init();
  35. Task02_Init();
  36. OLED_ShowString(1,1,"AD0:0000");
  37. OLED_ShowString(2,1,"AD1:9999");
  38. OLED_ShowString(3,1,"AD2:9999");
  39. OLED_ShowString(4,1,"AD3:9999");
  40. while(1)
  41. {
  42. AD0 = Task02_AD_GetValue(ADC_Channel_0);
  43. AD1 = Task02_AD_GetValue(ADC_Channel_1);
  44. AD2 = Task02_AD_GetValue(ADC_Channel_2);
  45. AD3 = Task02_AD_GetValue(ADC_Channel_3);
  46. OLED_ShowNum(1,5,AD0,4);
  47. OLED_ShowNum(2,5,AD1,4);
  48. OLED_ShowNum(3,5,AD2,4);
  49. OLED_ShowNum(4,5,AD3,4);
  50. Delay_ms(100);
  51. }
  52. }
  53. int main(void)
  54. {
  55. // Task02();
  56. while (1)
  57. {
  58. }
  59. }

task01.c

  1. #include "task01.h"
  2. //配置单次转换,非扫描模式ADC
  3. void Task01_Init()
  4. {
  5. //开启RCC时钟
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
  8. //ADCCLK分配器配置
  9. RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  10. //配置GPIO为模拟输入模式
  11. GPIO_InitTypeDef My_GPIO_InitStructure;
  12. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  13. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式,AIN模式是ADC的专属模式
  14. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  15. GPIO_Init(GPIOA,&My_GPIO_InitStructure);
  16. //选择规则组的输入通道
  17. ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);
  18. //初始化ADC
  19. ADC_InitTypeDef My_ADC_InitStructure;
  20. My_ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//单次转换模式
  21. My_ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据右对齐
  22. My_ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//外部触发源选择,这里是无触发源,用软件触发就选这个参数
  23. My_ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
  24. My_ADC_InitStructure.ADC_NbrOfChannel = 1;//选择通道一
  25. My_ADC_InitStructure.ADC_ScanConvMode = DISABLE;//单通道模式
  26. ADC_Init(ADC1,&My_ADC_InitStructure);
  27. //开启ADC电源
  28. ADC_Cmd(ADC1,ENABLE);
  29. //校准
  30. //1.复位校准
  31. ADC_ResetCalibration(ADC1);
  32. //2.等待复位校准完成
  33. while(ADC_GetResetCalibrationStatus(ADC1) == SET);
  34. //3.开始校准
  35. ADC_StartCalibration(ADC1);
  36. //4.获取校准标志位,等待校准完成
  37. while(ADC_GetCalibrationStatus(ADC1) == SET);
  38. }
  39. //获取ADC的数据
  40. uint16_t Task01_AD_GetValue(void)
  41. {
  42. ADC_SoftwareStartConvCmd(ADC1,ENABLE);//软件触发转换
  43. while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);//等待转换完成
  44. return ADC_GetConversionValue(ADC1);
  45. }
  1. #include "task02.h"
  2. void Task02_Init()
  3. {
  4. //开启RCC时钟
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
  7. //配置ADCCLK预分频器
  8. RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  9. //配置GPIO为模拟输入
  10. GPIO_InitTypeDef My_GPIO_InitStructure;
  11. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  12. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  13. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14. GPIO_Init(GPIOA,&My_GPIO_InitStructure);
  15. //ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);
  16. //初始化ADC
  17. ADC_InitTypeDef My_ADC_InitStructure;
  18. My_ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//单次转换
  19. My_ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据右对齐
  20. My_ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//无触发源,由软件触发
  21. My_ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
  22. My_ADC_InitStructure.ADC_NbrOfChannel = 1;//选择通道1
  23. My_ADC_InitStructure.ADC_ScanConvMode = DISABLE;//单通道模式
  24. ADC_Init(ADC1,&My_ADC_InitStructure);
  25. //开启ADC电源
  26. ADC_Cmd(ADC1,ENABLE);
  27. //校准数据
  28. //复位校准
  29. ADC_ResetCalibration(ADC1);
  30. //等待复位校准完成
  31. while(ADC_GetResetCalibrationStatus(ADC1) == SET);
  32. //开始校准
  33. ADC_StartCalibration(ADC1);
  34. //等待校准完成
  35. while(ADC_GetCalibrationStatus(ADC1) == SET);
  36. }
  37. uint16_t Task02_AD_GetValue(uint8_t ADC_Channel)
  38. {
  39. //选择规则组的输入通道
  40. ADC_RegularChannelConfig(ADC1,ADC_Channel,1,ADC_SampleTime_55Cycles5);
  41. ADC_SoftwareStartConvCmd(ADC1,ENABLE);//软件触发转换
  42. while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);//等待转换完成
  43. return ADC_GetConversionValue(ADC1); //获取数据
  44. }

8-1 DMA直接存储器取存

DMA库函数:

  1. //恢复初始化DMA
  2. void DMA_DeInit(DMA_Channel_TypeDef* DMAy_Channelx);
  3. //初始化DMA
  4. void DMA_Init(DMA_Channel_TypeDef* DMAy_Channelx, DMA_InitTypeDef* DMA_InitStruct);
  5. //DMA结构体赋默认值
  6. void DMA_StructInit(DMA_InitTypeDef* DMA_InitStruct);
  7. //DMA是否启动
  8. void DMA_Cmd(DMA_Channel_TypeDef* DMAy_Channelx, FunctionalState NewState);
  9. //DMA中断配置
  10. void DMA_ITConfig(DMA_Channel_TypeDef* DMAy_Channelx, uint32_t DMA_IT, FunctionalState NewState);
  11. //DMA设置当前数据寄存器,即设置传输计数器
  12. void DMA_SetCurrDataCounter(DMA_Channel_TypeDef* DMAy_Channelx, uint16_t DataNumber);
  13. //DMA获取当前数据寄存器
  14. uint16_t DMA_GetCurrDataCounter(DMA_Channel_TypeDef* DMAy_Channelx);
  15. //获取标志位
  16. FlagStatus DMA_GetFlagStatus(uint32_t DMAy_FLAG);
  17. //清除标志位
  18. void DMA_ClearFlag(uint32_t DMAy_FLAG);
  19. //获取中断标志位
  20. ITStatus DMA_GetITStatus(uint32_t DMAy_IT);
  21. //清除中断标志位
  22. void DMA_ClearITPendingBit(uint32_t DMAy_IT);

8-2 DMA数据转运&DMA+AD多通道

配置DMA的步骤:

1.启动RCC时钟

2.调用DMA初始化函数DMA_Init,配置参数

3.开启DMA,调用DMA_Cmd

4.(如果要硬件触发,要调用XXX_MDACmd,开启触发信号的输出)

5.(如果要DMA中断,调用DMA_ITConfig,开启中断输出)

 main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "OLED.h"
  3. #include "task01.h"
  4. #include "task02.h"
  5. #include "delay.h"
  6. //任务1 软件触发DMA转运数据
  7. uint8_t DataA[] = {0x01,0x02,0x03,0x04};
  8. uint8_t DataB[] = {00,00,00,00};
  9. void Task01()
  10. {
  11. OLED_Init();
  12. Task01_Init((uint32_t)DataA,(uint32_t)DataB,4);
  13. OLED_ShowHexNum(1,1,DataA[0],2);
  14. OLED_ShowHexNum(1,5,DataA[1],2);
  15. OLED_ShowHexNum(1,9,DataA[2],2);
  16. OLED_ShowHexNum(1,14,DataA[3],2);
  17. while(1)
  18. {
  19. OLED_ShowHexNum(4,1,DataB[0],2);
  20. OLED_ShowHexNum(4,5,DataB[1],2);
  21. OLED_ShowHexNum(4,9,DataB[2],2);
  22. OLED_ShowHexNum(4,14,DataB[3],2);
  23. for(uint16_t i = 0;i < 4;i++)
  24. {
  25. DataA[i] += 0x02;
  26. }
  27. Task01_DMA_Transfer();
  28. Delay_ms(100);
  29. }
  30. }
  31. //ADC作为外设多通道DMA运输
  32. //PA0接电位器 PA1接光温传感器 PA2接温度传感器 PA3接红外传感器
  33. void Task02()
  34. {
  35. OLED_Init();
  36. Task02_Init();
  37. while(1)
  38. {
  39. Task02_AD_GetValue();
  40. // AD_Value[0] = Task02_AD_GetValue(ADC_Channel_0);
  41. // AD_Value[1] = Task02_AD_GetValue(ADC_Channel_1);
  42. // AD_Value[2] = Task02_AD_GetValue(ADC_Channel_2);
  43. // AD_Value[3] = Task02_AD_GetValue(ADC_Channel_3);
  44. // Task02_DMA_Transfer();
  45. OLED_ShowNum(1,1,AD_Value[0],4);
  46. OLED_ShowNum(1,6,AD_Value[1],4);
  47. OLED_ShowNum(2,1,AD_Value[2],4);
  48. OLED_ShowNum(2,6,AD_Value[3],4);
  49. // OLED_ShowNum(3,1,Value[0],4);
  50. // OLED_ShowNum(3,6,Value[1],4);
  51. // OLED_ShowNum(4,1,Value[2],4);
  52. // OLED_ShowNum(4,6,Value[3],4);
  53. Delay_ms(300);
  54. }
  55. }
  56. int main(void)
  57. {
  58. // Task02();
  59. while (1)
  60. {
  61. }
  62. }

 task01.c

  1. #include "task01.h"
  2. uint16_t My_Size;
  3. void Task01_Init(uint32_t ADDrA,uint32_t ADDrB,uint16_t Size)//数据从ADDrA->ADDrB,A是外设,B是存储器
  4. {
  5. My_Size = Size;
  6. //初始化DMA
  7. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//开启RCC时钟
  8. DMA_Cmd(DMA1_Channel1,DISABLE);//先失能DMA
  9. DMA_InitTypeDef My_DMA_InitStructure;
  10. My_DMA_InitStructure.DMA_BufferSize = Size;//缓存区大小,即传输计数器 0~65535
  11. My_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向
  12. //DST是指外设站点作为目的地,SRC是指外设站点作为源头
  13. My_DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;//是否使用存储器到存储器,即选择软件触发还是硬件触发
  14. My_DMA_InitStructure.DMA_MemoryBaseAddr = ADDrB;//存储器站点的起始地址
  15. My_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//存储器站点的数据宽度
  16. My_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器站点地址是否递增
  17. My_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//传输模式,选择是否使用自动重装
  18. //DMA的软件触发和自动重装不能同时启用,否则无法停下
  19. My_DMA_InitStructure.DMA_PeripheralBaseAddr = ADDrA;//外设站点的起始地址
  20. My_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设站点的数据宽度
  21. My_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;//外设外设站点地址是否递增。
  22. My_DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//优先级配置。
  23. DMA_Init(DMA1_Channel1,&My_DMA_InitStructure);
  24. DMA_Cmd(DMA1_Channel1,ENABLE);
  25. }
  26. void Task01_DMA_Transfer(void)
  27. {
  28. DMA_Cmd(DMA1_Channel1,DISABLE);
  29. DMA_SetCurrDataCounter(DMA1_Channel1,My_Size);
  30. DMA_Cmd(DMA1_Channel1,ENABLE);
  31. //等待转运完成
  32. while(DMA_GetFlagStatus(DMA1_FLAG_GL1) == RESET);
  33. DMA_ClearFlag(DMA1_FLAG_GL1);
  34. }

task02.c 

  1. #include "task02.h"
  2. uint16_t AD_Value[4];
  3. void Task02_Init()
  4. {
  5. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//开启DMA时钟
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//开启ADC1时钟
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA时钟
  8. //配置ADC
  9. GPIO_InitTypeDef My_GPIO_InitStructure;
  10. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;
  11. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  12. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_Init(GPIOA,&My_GPIO_InitStructure); //配置GPIO
  14. RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ADC预分频
  15. ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);
  16. ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_55Cycles5);
  17. ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_55Cycles5);
  18. ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_55Cycles5);
  19. ADC_InitTypeDef My_ADC_InitStructure;
  20. My_ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//是否连续转换模式
  21. My_ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据右对齐
  22. My_ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//不接触发源
  23. My_ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//ADC为独立模式
  24. My_ADC_InitStructure.ADC_NbrOfChannel = 4;
  25. My_ADC_InitStructure.ADC_ScanConvMode = ENABLE;//配置多通道模式
  26. ADC_Init(ADC1,&My_ADC_InitStructure);//初始化ADC
  27. DMA_Cmd(DMA1_Channel1,DISABLE);//先失能DMA
  28. DMA_InitTypeDef My_DMA_InitStructure;
  29. My_DMA_InitStructure.DMA_BufferSize = 4;//缓存区大小,即传输计数器 0~65535
  30. My_DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向
  31. //DST是指外设站点作为目的地,SRC是指外设站点作为源头
  32. My_DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//是否使用存储器到存储器,即选择软件触发还是硬件触发
  33. My_DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)AD_Value;//存储器站点的起始地址
  34. My_DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;//存储器站点的数据宽度
  35. My_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器站点地址是否递增
  36. My_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//传输模式,选择是否使用自动重装
  37. //DMA的软件触发和自动重装不能同时启用,否则无法停下
  38. My_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;//外设站点的起始地址
  39. My_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设站点的数据宽度
  40. My_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设外设站点地址是否递增。
  41. My_DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//优先级配置。
  42. DMA_Init(DMA1_Channel1,&My_DMA_InitStructure);
  43. DMA_Cmd(DMA1_Channel1,ENABLE);//使能DMA
  44. ADC_DMACmd(ADC1,ENABLE);//将ADC触发DMA开启
  45. ADC_Cmd(ADC1,ENABLE);//开启ADC
  46. //因使用多通道的ADC,要检验数据
  47. //复位校准
  48. ADC_ResetCalibration(ADC1);
  49. //等待校准复位完毕
  50. while(ADC_GetResetCalibrationStatus(ADC1) == SET);
  51. //开启校准
  52. ADC_StartCalibration(ADC1);
  53. //等待校准完毕
  54. while(ADC_GetCalibrationStatus(ADC1) == SET);
  55. }
  56. void Task02_AD_GetValue(void)
  57. {
  58. DMA_Cmd(DMA1_Channel1,DISABLE);
  59. DMA_SetCurrDataCounter(DMA1_Channel1,4);
  60. DMA_Cmd(DMA1_Channel1,ENABLE);
  61. ADC_SoftwareStartConvCmd(ADC1,ENABLE);//软件触发转换
  62. //等待转运完成
  63. while(DMA_GetFlagStatus(DMA1_FLAG_GL1) == RESET);
  64. DMA_ClearFlag(DMA1_FLAG_GL1);
  65. }

9-1 USART串口协议

9-2 USART串口外设

 

 

 

 

9-3 串口发送&串口发送+接收

配置串口步骤:
1.开启时钟,将要用的USART和GPIO时钟打开
2.GPIO初始化,将TX配置成复用输出,RX配置成输入
3.USART初始化,若需要接收功能,则另外要配置中断,只发送就不用配置中断
4.配置中断,加上ITConfig和NVIC的配置
5.开启USART

 USART的库函数:

  1. //恢复串口初始化
  2. void USART_DeInit(USART_TypeDef* USARTx);
  3. //串口初始化配置函数
  4. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
  5. //给串口配置结构体赋默认值
  6. void USART_StructInit(USART_InitTypeDef* USART_InitStruct);
  7. //配置同步时钟输出
  8. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);
  9. //给串口同步时钟配置结构体赋默认值
  10. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);
  11. //串口启停控制
  12. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  13. //串口中断配置
  14. void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
  15. //串口DMA启停,开启USART到MDA的通道
  16. void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
  17. //设置地址
  18. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
  19. //唤醒串口
  20. void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp);
  21. //接收唤醒启停
  22. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  23. //设置串口LINBreak检测长度
  24. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength);
  25. //串口LINBreak启停
  26. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  27. //串口发送数据,写DR寄存器
  28. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
  29. //串口接收数据,读DR寄存器
  30. uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
  31. //串口发送返回字符
  32. void USART_SendBreak(USART_TypeDef* USARTx);
  33. //设置指定的串口保护时间
  34. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
  35. //设置串口系统时钟预调器
  36. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
  37. //串口智能卡模式启停
  38. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  39. //串口NACK传输启停
  40. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  41. //串口半双工通信启停
  42. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  43. //串口8倍过采样模式启停
  44. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  45. //串口USART的一位采样方法启停
  46. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  47. //配置串口的IrDA接口
  48. void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode);
  49. //串口的IrDA接口启停
  50. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
  51. //获取串口标志位
  52. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  53. //清除串口标志位
  54. void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  55. //获取串口中断标志位
  56. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
  57. //清除串口中断标志位
  58. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);

 main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "OLED.h"
  3. #include "task01.h"
  4. #include "task02.h"
  5. #include "delay.h"
  6. //任务1 串口发送 PA9是RX PA10TX
  7. void Task01()
  8. {
  9. OLED_Init();
  10. Task01_Init();
  11. Task01_USART1_SendByte(0x41);
  12. /第一种串口输出,通过对USART_SendData的封装达成功能
  13. uint16_t Array[4] = {0x11,0x12,0x13,0x14};
  14. Task01_Serial_SendArray(Array,4);
  15. Task01_Serial_SendString("HELLOWORLD");
  16. Task01_Serial_SendNumber(12313123,8);
  17. Task01_USART1_SendByte('A');
  18. printf("你好\r\n");//重定义printf输出到串口中
  19. /第二种串口输出:使用sprintf,适合多串口输出//
  20. char String[100];
  21. sprintf(String,"hello%d\r\n",1214123);
  22. Task01_Serial_SendString(String);
  23. /第三种串口输出:变参函数封装///
  24. Task01_Serial_Printf("NUM = %d\r\n",123);
  25. while(1)
  26. {
  27. // USART_SendData(USART1,'A');
  28. // while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//等待发送完成
  29. // Delay_ms(50);
  30. }
  31. }
  32. //任务2 串口发送+接收
  33. void Task02()
  34. {
  35. OLED_Init();
  36. OLED_ShowString(4,1,"HELLO");
  37. Task02_Init();
  38. uint8_t Data;
  39. while(1)
  40. {
  41. if(Task02_GetSerial_RxFlag() == 1)
  42. {
  43. Data = Task02_GetSerial_RxData();
  44. Task02_USART1_SendByte(Data);
  45. OLED_ShowHexNum(1,1,Data,2);
  46. }
  47. }
  48. }
  49. int main(void)
  50. {
  51. Task01();
  52. while (1)
  53. {
  54. }
  55. }

 task01.c

  1. #include "task01.h"
  2. void Task01_Init(void)
  3. {
  4. //开启时钟
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  7. //GPIO初始化 配置成复用输入输出 PA9是TX PA10RX
  8. GPIO_InitTypeDef My_GPIO_InitStructure;
  9. My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  10. My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
  11. My_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_Init(GPIOA,&My_GPIO_InitStructure);
  13. // My_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  14. // My_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  15. // GPIO_Init(GPIOA,&My_GPIO_InitStructure);
  16. //串口初始化
  17. USART_InitTypeDef My_USART_InitStructure;
  18. My_USART_InitStructure.USART_BaudRate = 9600;//波特率
  19. My_USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不使用硬件流控模式
  20. My_USART_InitStructure.USART_Mode = USART_Mode_Tx;//配置为发送模式
  21. My_USART_InitStructure.USART_Parity = USART_Parity_No;//无校验位
  22. My_USART_InitStructure.USART_StopBits = USART_StopBits_1;//1个停止位
  23. My_USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位数
  24. USART_Init(USART1,&My_USART_InitStructure);
  25. //使能串口
  26. USART_Cmd(USART1,ENABLE);
  27. }
  28. void Task01_USART1_SendByte(uint8_t Data)
  29. {
  30. USART_SendData(USART1,Data);//发送后需要等待数据全部发送完成
  31. while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//等待发送完成
  32. //标志位无需清零,每次调用SendData函数的时候会清零
  33. }
  34. //发送数组
  35. void Task01_Serial_SendArray(uint16_t *Array,uint32_t Length)
  36. {
  37. uint16_t i;
  38. for(i = 0;i < Length; i++)
  39. {
  40. Task01_USART1_SendByte(Array[i]);
  41. }
  42. }
  43. //发送字符串
  44. void Task01_Serial_SendString(char *String)// /r/n可以回车换行
  45. {
  46. uint16_t i;
  47. for(i = 0;String[i] != '\0';i++)
  48. {
  49. Task01_USART1_SendByte(String[i]);
  50. }
  51. }
  52. //辅助发送数字的函数,输出的是x的y次方
  53. uint32_t Task01_Serial_Pow(uint32_t X,uint32_t Y)
  54. {
  55. uint32_t Resuit = 1;
  56. while(Y--)
  57. {
  58. Resuit *= X;
  59. }
  60. return Resuit;
  61. }
  62. //发送数字
  63. void Task01_Serial_SendNumber(uint32_t Number,uint8_t Length)
  64. {
  65. uint16_t i;
  66. for(i = 0;i < Length;i++)
  67. {
  68. Task01_USART1_SendByte(Number / Task01_Serial_Pow(10,Length - i - 1) % 10 + '0');
  69. }
  70. }
  71. //printf重定向
  72. int fputc(int ch,FILE *f)
  73. {
  74. Task01_USART1_SendByte(ch);
  75. return ch;
  76. }
  77. void Task01_Serial_Printf(char *format,...)
  78. {
  79. char String[100];
  80. va_list arg;
  81. va_start(arg,format);
  82. vsprintf(String,format,arg);
  83. va_end(arg);
  84. Task01_Serial_SendString(String);
  85. }

task01.c 

  1. #include "task02.h"
  2. void Task02_Init(void)
  3. {
  4. //初始化串口,让其发送+接收
  5. //开始RCC
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  9. //初始化GPIO PA9是单片机的TX PA10是单片机的RX
  10. GPIO_InitTypeDef My_GPIO_IniterStructure;
  11. My_GPIO_IniterStructure.GPIO_Pin = GPIO_Pin_9;
  12. My_GPIO_IniterStructure.GPIO_Mode = GPIO_Mode_AF_PP;//输出口配为复用输出
  13. My_GPIO_IniterStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14. GPIO_Init(GPIOA,&My_GPIO_IniterStructure);
  15. My_GPIO_IniterStructure.GPIO_Pin = GPIO_Pin_10;
  16. My_GPIO_IniterStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//输入口配为复用浮空输入
  17. GPIO_Init(GPIOA,&My_GPIO_IniterStructure);
  18. //USART初始化
  19. USART_InitTypeDef My_USART_InitStructure;
  20. My_USART_InitStructure.USART_BaudRate = 9600;//波特率设置
  21. My_USART_InitStructure.USART_HardwareFlowControl = DISABLE;//禁用硬件流控制
  22. My_USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//配置为发送+接收的模式
  23. My_USART_InitStructure.USART_Parity = USART_Parity_No;//不使用校验位
  24. My_USART_InitStructure.USART_StopBits = USART_StopBits_1;//1个停止位
  25. My_USART_InitStructure.USART_WordLength = USART_WordLength_8b;//有效数据长度为八位
  26. USART_Init(USART1,&My_USART_InitStructure);
  27. //接收模式可配置中断
  28. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启串口的接收中断、
  29. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//NVIC分组
  30. NVIC_InitTypeDef My_NVIC_InitStructure;
  31. My_NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  32. My_NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  33. My_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  34. My_NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  35. NVIC_Init(&My_NVIC_InitStructure);//初始化NVIC(中断管理器)
  36. //使能串口
  37. USART_Cmd(USART1,ENABLE);
  38. }
  39. //串口发送函数
  40. void Task02_USART1_SendByte(uint8_t Data)
  41. {
  42. USART_SendData(USART1,Data);
  43. while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//等待发送完成
  44. //标志位不用清零,每次使用USART_SendData时会自动清零
  45. }
  46. //串口发送数组
  47. void Task02_Serial_SendArray(uint16_t *Array,uint32_t Length)
  48. {
  49. uint16_t i;
  50. for(i = 0;i < Length;i++)
  51. {
  52. Task02_USART1_SendByte(Array[i]);
  53. }
  54. }
  55. //串口发送字符串
  56. void Task02_Serial_SendString(char *String)
  57. {
  58. uint16_t i;
  59. for(i = 0;String[i] != '\0';i++)
  60. {
  61. Task02_USART1_SendByte(String[i]);
  62. }
  63. }
  64. //辅助发送数字的函数
  65. uint32_t Task02_Serial_Pow(uint32_t X,uint32_t Y)
  66. {
  67. uint32_t Resuit = 1;
  68. while(Y--)
  69. {
  70. Resuit *= X;
  71. }
  72. return Resuit;
  73. }
  74. //串口发送数字
  75. void Task02_Serial_SendNumber(int32_t Number,uint8_t Length)
  76. {
  77. uint16_t i;
  78. for(i = 0;i < Length;i++)
  79. {
  80. Task02_USART1_SendByte(Number / Task02_Serial_Pow(10,Length - i - 1) % 10 + '0');
  81. }
  82. }
  83. printf重定向
  84. //int fputc(int ch,FILE *f)
  85. //{
  86. // Task02_USART1_SendByte(ch);
  87. // return ch;
  88. //
  89. //}
  90. uint8_t Task02_Serial_RxData;
  91. uint8_t Task02_Serial_RxFlag;
  92. uint8_t Task02_GetSerial_RxFlag(void)
  93. {
  94. if(Task02_Serial_RxFlag == 1)
  95. {
  96. Task02_Serial_RxFlag = 0;
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. uint8_t Task02_GetSerial_RxData(void)
  102. {
  103. return Task02_Serial_RxData;
  104. }
  105. void USART1_IRQHandler(void)
  106. {
  107. if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET)
  108. {
  109. Task02_Serial_RxData = USART_ReceiveData(USART1);
  110. Task02_Serial_RxFlag = 1;
  111. USART_ClearITPendingBit(USART1,USART_IT_RXNE);//手动清除中断标志位
  112. }
  113. }

9-4 USART串口数据包

9-5 串口收发HEX数据包&串口收发文本数据包

9-6 FlyMcu串口下载&STLINK Utility

10-1 IIC通信协议

10-2 MPU6050简介

10-3 软件IIC读写MPU6050

10-4 IIC通信外设

10-5 硬件IIC读写MPU6050

11-1 SPI通信协议

11-2 W25Q64简介

11-3 软件SPI读写W25Q64

11-4 SPI通信外设

11-5 硬件SPI读写W25Q64

12-1 Unix时间戳

12-2 BKP备份寄存器&RTC实时时钟

12-3 读写备份寄存器&实时时钟

13-1 PWR电源控制

14-1 WDG看门狗

14-2 独立看门狗&窗口看门狗

15-1 FLASH 闪存

15-2 读写内部FLASH&读取芯片ID

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/457358
推荐阅读
相关标签
  

闽ICP备14008679号