最近在测试定时器的PWM输入捕获模式
用以测试PWM的周期频率,分别用TIM3 CH1 接一路PWM输入 TIM4 CH4接一路PWM输入,输入波形为方波
频率为276HZ左右,使用TIM3的那一路可以正确的测出频率,但使用TIM4 CH4的那一路出现异常,值忽然很高忽然很低(输入源比较稳定,276HZ左右),查了一些资料发现CH4通道不能使用PWM输入捕获模式,只能使用一般的输入捕获模式,是不是这样的?另外能不能提供一些CH4通道的输入捕获模式实例程序?
一下是我的代码:
TIM3 CH1 :这个是正确的
void luce_drv_mcu_tim3_init(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/****TIMER2_CLK = 25MHZ****/
/****25MHZ/25000 = 1KHZ *****/
/* Time base configuration */
/* 计数器的时钟频率CK_CNT等于TIMER2_CLK/(TIM_Prescaler+1)*/
/*定时周期 T = (TIM_Period+1)*(1/CK_CNT) s*/
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 4;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Time base configuration */
TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_PWMI;
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_DIV1;
TIM_ICInitStructure.TIM_ICFilter = TIM_ICFilter_Mask;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); //复位模式为从模式
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); //使能主从模式
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM3,TIM_IT_CC1, ENABLE); //使能CC1中断请求
}
TIM4 CH4 异常的
void luce_drv_mcu_tim4_init(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/****TIMER2_CLK = 25MHZ****/
/****25MHZ/25000 = 1KHZ *****/
/* Time base configuration */
/* 计数器的时钟频率CK_CNT等于TIMER2_CLK/(TIM_Prescaler+1)*/
/*定时周期 T = (TIM_Period+1)*(1/CK_CNT) s*/
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 4;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* Time base configuration */
TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = TIM_ICFilter_Mask;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); //复位模式为从模式
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //使能主从模式
/* TIM enable counter */
TIM_Cmd(TIM4, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM4,TIM_IT_CC4, ENABLE); //使能CC4中断请求
}
对这些分别进行过屏蔽
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); //复位模式为从模式
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //使能主从模式
可结果还是有问题,一样忽高忽低。 |