一个老项目,以前是测试固定频率,没考虑溢出问题,运行良好,
现在偶尔会有低频,所以得兼顾,但是加入更新中断后,频繁进入中断,是哪里出问题了?
输入捕获从模式复位,更新中断进行计数累加,记录溢出。
- static void TIM2_Mode_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- TIM_DeInit(TIM2);
-
- /* Time base configuration */
-
- TIM_TimeBaseStructure.TIM_Period = 0xffff;
- TIM_TimeBaseStructure.TIM_Prescaler = 71;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
-
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
- TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
-
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV4;
- //选择输入比较滤波器 0x0 - 0xF
- TIM_ICInitStructure.TIM_ICFilter = 0x8; //0x8
-
- TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
-
- /* Select the TIM3 Input Trigger: TI2FP2 */
- TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
-
- /* Select the slave Mode: Reset Mode */
- TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
-
- /* Enable the Master/Slave Mode */
- TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
-
- /* Enable the CC2 Interrupt Request */
- TIM_ITConfig(TIM2, TIM_IT_Update|TIM_IT_CC2, ENABLE);
-
- /* TIM enable counter */
- TIM_Cmd(TIM2, ENABLE);
- }
- void TIM2_ISR(void)
- {
- Uint32 IC2Value;
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除溢出中断标志位
- cnt_TIM2_IT_Update ++;
- }
- if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
- {
- IC2Value = TIM_GetCapture2(TIM2);
- TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); //清除捕获中断标志位
- IC2Value += (cnt_TIM2_IT_Update * 0xFFFF);
- cnt_TIM2_IT_Update = 0; //清零
- }
- }
复制代码
试验结果,不管低频高频,所有的I2CValue值都大于0xFFFF了,是不是每取一次TIM_GetCapture2也会触发更新中断?
然后在TIM6更新中断里自增, 在TIM2捕获中断里TIM6->CNT = 0;
试验了下,可以用。
如果前辈们有更好更简便的方法,请不吝赐教。
设计一个变量统计更新中断的次数,第一次捕获记录这个次数,第二次捕获再记录下这个更新中断的次数,相减就是两次捕获之间的更新中断次数了,这样不需要别的定时器参与
一周热门 更多>