定时器输入捕获功能
- 1、通过定时器的输入捕获功能测量PWM波的频率
- 2、PWMI模式测量频率和占空比
1、通过定时器的输入捕获功能测量PWM波的频率
定时器标准库相关的编程接口:
①PWM.c文件的代码如下:
/*通过定时器TIM2生成一个分辨率为10us,频率为1KHz的PWM波,
*/
#include "stm32f10x.h" // Device header/*使用定时器TIM2,通过通道CH1(PA0)输出PWM波
*/
void PWM_Init(void)
{//1.使能时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//2.选择内部时钟TIM_InternalClockConfig(TIM2);//3.对时基单元进行配置TIM_TimeBaseInitTypeDef TIM_TimeInitStruct;TIM_TimeInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟源分频TIM_TimeInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数TIM_TimeInitStruct.TIM_Period = 100 - 1;//计数器,PWM的频率 = 1KHz,T = 1msTIM_TimeInitStruct.TIM_Prescaler = 720 - 1;//预分频器,72M/720 = 100kHzTIM_TimeInitStruct.TIM_RepetitionCounter = 0;//重复计数器TIM_TimeBaseInit(TIM2,&TIM_TimeInitStruct);//4.对输出比较单元进行配置TIM_OCInitTypeDef TIM_OCInitStruct;TIM_OCStructInit(&TIM_OCInitStruct);//给结构体默认初始值TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//选择PWM1模式TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//选择正极性TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//通道使能TIM_OCInitStruct.TIM_Pulse = 0;//CCR的初始值TIM_OC1Init(TIM2, &TIM_OCInitStruct);//5.配置CH1通道对应的引脚PA0RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIOInitStruct;GPIOInitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出GPIOInitStruct.GPIO_Pin = GPIO_Pin_0;GPIOInitStruct.GPIO_Speed = GPIO_Speed_2MHz;GPIO_Init(GPIOA,&GPIOInitStruct);//6.使能定时器TIM_Cmd(TIM2,ENABLE);
}void PWM_Set(uint16_t Compare)
{TIM_SetCompare1(TIM2,Compare);//更改CCR的值,及改变占空比
}void Set_PSC(uint32_t Pres)
{TIM_PrescalerConfig(TIM2,Pres,TIM_PSCReloadMode_Immediate);//更改预分频PSC的值
}
②PWM.h文件的代码如下:
#ifndef __PWM_H
#define __PWM_H
#include "stm32f10x.h" // Device headervoid PWM_Init(void);
void PWM_Set(uint16_t Compare);
void Set_PSC(uint32_t Pres);#endif
③IC.c文件的代码如下:
#include "stm32f10x.h" // Device headervoid IC_Init(void)
{/*由于输入捕获通道和输出比较通道不能够同时的使用我们使用了TIM2的CH1产生了1KHz的PWM波形,所以我们使用TIM3的CH1(PA6)进行对PWM波形的输入进行捕获,测量其频率接线时,将输出PWM的引脚(PA0)连接到输出捕获的引脚(PA6)*/ //1. 使能TIM3的时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//2. 选择内部时钟源TIM_InternalClockConfig(TIM3);//3.配置时基单元TIM_TimeBaseInitTypeDef TIM_TimeInitStruct;TIM_TimeInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟源滤波分频TIM_TimeInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数TIM_TimeInitStruct.TIM_Period = 65536 - 1;//计数器TIM_TimeInitStruct.TIM_Prescaler = 72 - 1;//预分频器,1MHz,即1us计数1次TIM_TimeInitStruct.TIM_RepetitionCounter = 0;//重复计数器TIM_TimeBaseInit(TIM3,&TIM_TimeInitStruct);//4.对输入捕获引脚进行初始化RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIOInitStruct;GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入GPIOInitStruct.GPIO_Pin = GPIO_Pin_6;GPIO_Init(GPIOA,&GPIOInitStruct);//5.对输入捕获单元进行初始化TIM_ICInitTypeDef TIM_ICInitStruct;TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;//选择通道CH1,它不像输出比较每个通道都有一个函数TIM_OC?Init()TIM_ICInitStruct.TIM_ICFilter = 0xF;//对捕获的信号进行滤波TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;//极性不反向TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;//对捕获信号进行1分频TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;//选择直接连接TIM_ICInit(TIM3, &TIM_ICInitStruct);//6.对从模式进行配置TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);//从模式触发源选择TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);//从模式的触发的模式,对计数器清零TIM_Cmd(TIM3,ENABLE);
}uint32_t Get_Freq(void)
{return 1000000 / TIM_GetCapture1(TIM3);//TIM_GetCapture1()为获取通道CH1的CCR的值
}
④IC.h文件的代码如下:
#ifndef __IC_H
#define __IC_H
#include "stm32f10x.h" // Device headervoid IC_Init(void);
uint32_t Get_Freq(void);#endif
⑤主程序文件的代码如下:
/*通过输入捕获功能,捕获定时器产生的PWM波形的频率,和PWM波的占空比
*/#include "stm32f10x.h"
#include "OLED.h"
#include "PWM.h"
#include "IC.h"int main(void)
{OLED_Init();OLED_Clear();PWM_Init();IC_Init();OLED_ShowString(1,1,"Freq:00000Hz");PWM_Set(50);//设置占空比为50%的PWM波形
// Set_PSC(720 - 1);//更改预分频的值,即分辨率的值,72M/720 = 100KHz,//则分辨率为:1/100KHz = 10us,则PWM的T = 100 * 10us = 1ms,F = 1KHzwhile(1){ OLED_ShowNum(1,6,Get_Freq(),5);}
}
2、PWMI模式测量频率和占空比
①IC.c文件的代码如下:
#include "stm32f10x.h" // Device headervoid IC_Init(void)
{/*由于输入捕获通道和输出比较通道不能够同时的使用我们使用了TIM2的CH1产生了1KHz的PWM波形,所以我们使用TIM3的CH1(PA6)进行对PWM波形的输入进行捕获,测量其频率*/ //1. 使能TIM3的时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//2. 选择内部时钟源TIM_InternalClockConfig(TIM3);//3.配置时基单元TIM_TimeBaseInitTypeDef TIM_TimeInitStruct;TIM_TimeInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟源滤波分频TIM_TimeInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数TIM_TimeInitStruct.TIM_Period = 65536 - 1;//计数器TIM_TimeInitStruct.TIM_Prescaler = 72 - 1;//预分频器,1MHz,即1us计数1次TIM_TimeInitStruct.TIM_RepetitionCounter = 0;//重复计数器TIM_TimeBaseInit(TIM3,&TIM_TimeInitStruct);//4.对输入捕获引脚进行初始化RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIOInitStruct;GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入GPIOInitStruct.GPIO_Pin = GPIO_Pin_6;GPIO_Init(GPIOA,&GPIOInitStruct);//5.对输入捕获单元进行初始化TIM_ICInitTypeDef TIM_ICInitStruct;TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;//选择通道CH1,它不像输出比较每个通道都有一个函数TIM_OC?Init()TIM_ICInitStruct.TIM_ICFilter = 0xF;//对捕获的信号进行滤波TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿开始捕获TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;//对捕获信号进行1分频TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;//选择直接连接TIM_ICInit(TIM3, &TIM_ICInitStruct);// TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;//选择通道CH2,它不像输出比较每个通道都有一个函数TIM_OC?Init()
// TIM_ICInitStruct.TIM_ICFilter = 0xF;//对捕获的信号进行滤波
// TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;//下降沿开始捕获
// TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;//对捕获信号进行1分频
// TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_IndirectTI;//选择间接连接
// TIM_ICInit(TIM3, &TIM_ICInitStruct);/*如果配置了通道CH1,然后通过PWMI传入这些参数,那么PWM配置的是和CH1通道的参数相反,且这个函数只支持CH1和CH2。不能这样配置CH3和CH4
*/TIM_PWMIConfig(TIM3,&TIM_ICInitStruct);//和上面的配置相等//6.对从模式进行配置TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);//从模式触发源选择TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);//从模式的触发的模式,对计数器清零TIM_Cmd(TIM3,ENABLE);
}uint32_t Get_Freq(void)//获取频率的函数
{return 1000000 / TIM_GetCapture1(TIM3);//获取通道CH1的CCR的值
}uint32_t IC_GetDuty(void)//获取占空比
{return (TIM_GetCapture2(TIM3) * 100/ TIM_GetCapture1(TIM3)) ;
}
②主程序文件里面的代码如下:
/*通过输入捕获功能,捕获定时器产生的PWM波形的频率,和PWM波的占空比
*/#include "stm32f10x.h"
#include "OLED.h"
#include "PWM.h"
#include "IC.h"int main(void)
{OLED_Init();OLED_Clear();PWM_Init();IC_Init();OLED_ShowString(1,1,"Freq:00000Hz");OLED_ShowString(2,1,"Duty:000%");PWM_Set(30);//设置占空比为30%的PWM波形
// Set_PSC(720 - 1);//更改预分频的值,即分辨率的值,72M/720 = 100KHz,//则分辨率为:1/100KHz = 10us,则PWM的T = 100 * 10us = 1ms,F = 1KHzwhile(1){ OLED_ShowNum(1,6,Get_Freq(),5);OLED_ShowNum(2,6,IC_GetDuty(),3);}
}