我利用TIM4的4个通道做4路输入捕获,程序运行一段时间会进入 硬件错误,找不到原因,求助各位大佬
u8 TIM4CH1_CAPTURE_STA=0; //输入捕获状态
u8 TIM4CH2_CAPTURE_STA=0; //输入捕获状态
u8 TIM4CH3_CAPTURE_STA=0; //输入捕获状态
u8 TIM4CH4_CAPTURE_STA=0; //输入捕获状态
//定时器4中断服务程序
void TIM4_IRQHandler(void)
{
if((TIM4CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
{
if(TIM4CH1_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM4CH1_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
pwm_value[2]=TIM_GetCapture1(TIM4);
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
TIM4CH1_CAPTURE_STA=0;
}else //还未开始,第一次捕获上升沿
{
TIM4CH1_CAPTURE_STA=0; //清空
TIM_SetCounter(TIM4,0);
TIM4CH1_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); //清除中断标志位
}
/////////////////
if((TIM4CH2_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
{
if(TIM4CH2_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM4CH2_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
pwm_value[3]=TIM_GetCapture2(TIM4);
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
TIM4CH2_CAPTURE_STA=0;
}else //还未开始,第一次捕获上升沿
{
TIM4CH2_CAPTURE_STA=0; //清空
TIM_SetCounter(TIM4,0);
TIM4CH2_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); //清除中断标志位
}
/////////////////
if((TIM4CH3_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)//捕获1发生捕获事件
{
if(TIM4CH3_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM4CH3_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
pwm_value[4]=TIM_GetCapture3(TIM4);
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
TIM4CH3_CAPTURE_STA=0;
}else //还未开始,第一次捕获上升沿
{
TIM4CH3_CAPTURE_STA=0; //清空
TIM_SetCounter(TIM4,0);
TIM4CH3_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); //清除中断标志位
}
/////////////////
if((TIM4CH4_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET)//捕获1发生捕获事件
{
if(TIM4CH4_CAPTURE_STA&0X40) //捕获到一个下降沿
{
TIM4CH4_CAPTURE_STA|=0X80; //标记成功捕获到一次上升沿
pwm_value[5]=TIM_GetCapture4(TIM4);
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Rising); //CC1P=0 设置为上升沿捕获
TIM4CH4_CAPTURE_STA=0;
}else //还未开始,第一次捕获上升沿
{
TIM4CH4_CAPTURE_STA=0; //清空
TIM_SetCounter(TIM4,0);
TIM4CH4_CAPTURE_STA|=0X40; //标记捕获到了上升沿
TIM_OC4PolarityConfig(TIM4,TIM_ICPolarity_Falling); //CC1P=1 设置为下降沿捕获
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); //清除中断标志位
}
} |