今天终于搞定了多路定时器输入捕获!使用的还是ST提供的【NUCLEO-F411RE】板子。在写多路捕获的时候,有个问题需要注意:捕获参数的初始化需要分别进行,比如:
[mw_shl_code=cpp,true]//初始化TIM5输入捕获参数
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2;//使能2通道
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射CH1->TI1,CH2->TI2,CH3->TI3,CH4->TI4
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//配置输入滤波器 不滤波
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInit(TIM5, &TIM5_ICInitStructure);[/mw_shl_code]
若如下进行初始化,则可能出现捕获中断错误:
[mw_shl_code=cpp,true] TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2| TIM_Channel_1;//使能四个通道
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射CH1->TI1,CH2->TI2,CH3->TI3,CH4->TI4
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//配置输入滤波器 不滤波
TIM_ICInit(TIM5, &TIM5_ICInitStructure);[/mw_shl_code]
这一点要注意!
代码添加到附件里,有兴趣的可以看看 |