使用的是
tiMER2的CH1通道,PWM信号接在PA0脚。输入PWM的周期信号为100Hz,根据捕获值计算出来却是105Hz,这误差也太大了吧!!哪位高手知道是什么原因呀,指点一下呀,万分感谢呀!!
下面是我的相关部分程序:
1、时钟部分,TIMER2的时钟频率,我是初始化为36M.
RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速AHB时钟=系统时钟/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // 倍频系数为9 PLLCLK=8*9=72
2、TIMER初始化
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 18;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV4;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //复位模式为从模式
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); //使能主从模式
TIM_Cmd(TIM2, ENABLE); //使能TIM2计数器
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); //使能CC2中断请求
3、TIMER中断处理
void TIM2_IRQHandler(void)
{
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
/* Get the Input Capture value */
IC2_Value = TIM_GetCapture2(TIM2);
}
说搞定也没搞定,只是搞清楚原因了。原来STM32的TIMER的时钟经过预分频后,TIMER的时钟误差也相应变大了不少(有点太离谱了)。不分频测量1KHz的脉冲误差0.5us.分18频后的误差就像我上面讲的了。
各位朋友用到这一块,要小心了。
一周热门 更多>