TIM2配置:
timer.h
#if FAN_PWM_Count
extern unsigned char TIM2CH1_CAPTURE_STA; //输入捕获状态
extern unsigned int TIM2CH1_CAPTURE_Count; //输入捕获溢出计数
extern long long TIM2CH1_CAPTURE_VAL; //输入捕获值
extern long long TIM2CH1_CAPTURE_HIGHVAL;
extern unsigned long int TIM2_ARR;
#endif
timer.c
#if FAN_PWM_Count
unsigned char TIM2CH1_CAPTURE_STA; //输入捕获状态
unsigned int TIM2CH1_CAPTURE_Count; //输入捕获溢出计数
long long TIM2CH1_CAPTURE_VAL; //输入捕获值 一个周期捕获值
long long TIM2CH1_CAPTURE_HIGHVAL; //一个周期高电频的值
unsigned long int TIM2_ARR = 0xFFFF;
#endif
timer.c重定义HAL_TIM_IC_CaptureCallback
HAL_TIM_PeriodElapsedCallback
#if FAN_PWM_Count
/* Input capture event */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
if(htim ->Instance == TIM2){
if(!(TIM2CH1_CAPTURE_STA&0X80))//还未成功捕获
{
if(TIM2CH1_CAPTURE_STA&0x20) //捕获到第二个下降沿
{
__HAL_TIM_DISABLE(&htim2);
TIM2CH1_CAPTURE_STA|=0X80;
TIM2CH1_CAPTURE_VAL = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);
TIM2CH1_CAPTURE_VAL = TIM2CH1_CAPTURE_VAL + 2 + TIM2_ARR*TIM2CH1_CAPTURE_Count - TIM2CH1_CAPTURE_HIGHVAL;//获取当前的捕获值.//+ (TIM2CH1_CAPTURE_STA&0X1F - 1)*TIM2_ARR
if(TIM2CH1_CAPTURE_HIGHVAL>TIM2CH1_CAPTURE_VAL)
TIM2CH1_CAPTURE_HIGHVAL-=TIM2CH1_CAPTURE_VAL;
else if(TIM2CH1_CAPTURE_HIGHVAL==TIM2CH1_CAPTURE_VAL)
TIM2CH1_CAPTURE_HIGHVAL=0;
//一定要先清除原来的设置!!
//配置TIM5通道1上升沿捕获
__HAL_TIM_SET_CAPTUREPOLARITY(&htim2, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
}
else if(TIM2CH1_CAPTURE_STA&0X40) //捕获到第一个下降沿
{
TIM2CH1_CAPTURE_STA|=0X20; //标记成功捕获到一次高电平脉宽
TIM2CH1_CAPTURE_HIGHVAL = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);
TIM2CH1_CAPTURE_VAL = 0;
TIM2CH1_CAPTURE_HIGHVAL = TIM2CH1_CAPTURE_HIGHVAL + 2 + TIM2_ARR*TIM2CH1_CAPTURE_Count; // 消除误差
}
else //还未开始,第一次捕获上升沿
{
TIM2CH1_CAPTURE_STA=0; //清空
TIM2CH1_CAPTURE_HIGHVAL = 0;
TIM2CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
__HAL_TIM_DISABLE(&htim2); //关闭定时器5
__HAL_TIM_SET_COUNTER(&htim2,0);
//一定要先清除原来的设置!!
__HAL_TIM_SET_CAPTUREPOLARITY(&htim2, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);//定时器5通道1设置为下降沿捕获
__HAL_TIM_ENABLE(&htim2); //使能定时器5
}
}
}
}
#endif
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim ->Instance == TIM2) {
if((TIM2CH1_CAPTURE_STA&0X80)==0) //还未成功捕获
{
if(TIM2CH1_CAPTURE_STA&0X40 || TIM2CH1_CAPTURE_STA&0X20) //已经捕获到高电平了
{
if(TIM2CH1_CAPTURE_Count == 0xFFFF) //高电平太长了
{
TIM2CH1_CAPTURE_STA|=0X80; //标记成功捕获了一次
TIM2CH1_CAPTURE_VAL = 0XFFFFFFFF;
}
else {
TIM2CH1_CAPTURE_Count++;
}
}
}
}
}
main()使能timer调取TIM2CH1_CAPTURE_VAL
TIM2CH1_CAPTURE_HIGHVAL
计算频率
HAL_TIM_Base_Start_IT(&htim2);
uiFanSpeed = 60*(Tim2_Chn1_CountSUM/TIM2CH1_CAPTURE_VAL)/2;
//计算风扇转速 N = 60*f/P; p = 2; f = TIM2CH1_CAPTURE_VAL/
完整代码块:
if(TIM2CH1_CAPTURE_STA&0X80) //成功捕获到了一次高电平 TIM2CH1_CAPTURE_STA = 0x80
{
uiFanSpeed = 60*(Tim2_Chn1_CountSUM/TIM2CH1_CAPTURE_VAL)/2;//计算风扇转速 N = 60*f/P; p = 2; f = TIM2CH1_CAPTURE_VAL/
TIM2CH1_CAPTURE_STA=0;
TIM2CH1_CAPTURE_Count = 0;
__HAL_TIM_ENABLE(&htim2); //使能定时器5
}
————————————————
版权声明:本文为CSDN博主「明东(Maxwell)」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Maxwell_321/article/details/129043725
|