香主你好,最近做了个stm32f205re的pwm输入捕获,系统时间为120mhz,捕获200hz占空比为50%的连续脉冲,脉宽为2.5ms,用32位定时器timer5进行计数,我设置最高速率的计数,2.5ms的时间内计数为156250左右,算下来一个计数值为16ns,理论计数应该8ns的。下面是我的定时器的初始化函数 TIM5_PWMINPUT_INIT(0xffffffff,0);
函数内容为void TIM5_PWMINPUT_INIT(u32 arr,u32 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //Open TIM5 clock
RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //open gpioB clock
//Connect TIM3 Pin to AF2
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //TIM3_Ch2
// Config TIM3 Pin
/* TIM3 channel 2 pin (PA.07) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/*配置中断优先级*/
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //TIM_ICPolarity_Rising
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM5, &TIM5_ICInitStructure); //PWM输入配置
TIM_SelectInputTrigger(TIM5, TIM_TS_TI1FP1); //选择有效输入端 TIM_TS_TI2FP2
TIM_SelectSlaveMode(TIM5, TIM_SlaveMode_Reset); //配置为主从复位模式
TIM_SelectMasterSlaveMode(TIM5, TIM_MasterSlaveMode_Enable);
TIM_ITConfig(TIM5, TIM_IT_CC1, ENABLE); //中断配置 TIM_IT_CC2 |TIM_IT_Update
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位 TIM_IT_CC2
TIM_Cmd(TIM5, ENABLE);
}
中断函数为
u32 jueyuan_xiangwei_buf[JY_XW_SIZE];
u16 jy_xw_counter=0;
void TIM5_IRQHandler(void)
{
if(CollectFlag ==0 )
{
if (TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)//捕获1发生捕获事件 TIM_IT_CC2
{
duty = TIM_GetCapture1(TIM5); //采集占空比
period = TIM_GetCapture2(TIM5); //采集周期
jueyuan_xiangwei_buf[jy_xw_counter++] = period;
if(jy_xw_counter >= JY_XW_SIZE)
{
CollectFlag = 1;
jy_xw_counter =0;
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位 TIM_IT_CC2
}
我是在仿真器里面查看数值的,请香主帮忙看看我的设置是否有问题,为啥实际值与计算值差一半呢?谢谢 |