//l输入通道和捕获通道的映射关系 ,有直连和非直联两种,定时器TI1,TI1FP1和TI1FP2,由IC1捕获/比较寄存器1捕获叫直联,如果由捕获/比较寄存器2捕获叫非直联;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_IT_Update :更新中断
TIM_IT_CC1 :定时器捕获通道1中断
TIM_IT_CC2 :定时器捕获通道2中断
TIM_IT_CC3 :定时器捕获通道3中断
TIM_IT_CC4 :定时器捕获通道4中断
选择不同通道,要设不同的中断;
计数器(TIMx_CNT)只有在捕获得时候才会把数据传给捕获/ 比较寄存器(TIMx_CCR),当脉冲宽度大于定时器中断时,需要通过变量来累加时间:
声明结构体,
Capture_Period 是用来保存定时器溢出次数的
Capture_StartFlag:开始捕获标志;
Capture_FinishFlag:结束捕获标志,表示一次脉冲宽度捕获结束
Capture_CcrValue:读取CCR保存的值,即没有溢出CNT保存在CCR里的值;
typedef struct
{
uint8_t Capture_FinishFlag;
uint8_t Capture_StartFlag;
uint16_t Capture_CcrValue;
uint16_t Capture_Period;
}TIM_ICUserValueTypeDef;
void TIM4_IRQHandler(void)
{
if ( TIM_GetITStatus ( TIM4, TIM_IT_Update) != RESET )
{
TIM4_ICUserValue.Capture_Period ++;
TIM_ClearITPendingBit ( TIM4, TIM_FLAG_Update );
}
if ( TIM_GetITStatus (TIM4, TIM_IT_CC3 ) != RESET)
{
if ( TIM4_ICUserValue.Capture_StartFlag == 0 )
{
TIM_SetCounter ( TIM4, 0 );
TIM4_ICUserValue.Capture_Period = 0;
TIM4_ICUserValue.Capture_CcrValue = 0;
TIM_OC1PolarityConfig(TIM4, TIM_ICPolarity_Falling);
TIM4_ICUserValue.Capture_StartFlag = 1;
}
else
{
TIM4_ICUserValue.Capture_CcrValue = TIM_GetCapture1(TIM4);
TIM_OC1PolarityConfig(TIM4, TIM_ICPolarity_Rising);
TIM4_ICUserValue.Capture_StartFlag = 0;
TIM4_ICUserValue.Capture_FinishFlag = 1;
}
TIM_ClearITPendingBit (TIM4,TIM_IT_CC3);
}
}
脉宽计算: time = TIM_ICUserValueStructure.Capture_Period * (GENERAL_TIM_PERIOD+1) +
(TIM_ICUserValueStructure.Capture_CcrValue+1);