我用的TIMER2的CH1通道,PWM信号接在PA0脚。我输入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:TIMER2初始化 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,中断部分程序 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使用捕获功能,计算输入PWM信号周期,误差很大,求帮忙??
我用的TIMER2的CH1通道,PWM信号接在PA0脚。我输入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:TIMER2初始化
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,中断部分程序
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使用捕获功能,计算输入PWM信号周期,误差很大,求帮忙??
Prescaler = 18: 分频为19 .