为什么用STM32的TIM2定时器读取编码器时,TIM2->CNT的值会从32767突然跳变到94934528,这是为什么?

2019-07-20 23:42发布

[mw_shl_code=c,true]void TIM2_Configuration(void)//编码器接口设置(TIM2)/PA15-A相 PB3-B相[/mw_shl_code] [mw_shl_code=c,true]{ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; //GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); //关闭JTAG接口 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //关闭JTAG接口 开启SWD RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_PinRemapConfig(GPIO_FullRemap_TIM2,ENABLE); //TIM2引脚重定向 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PA15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_WriteBit(GPIOA, GPIO_Pin_15,Bit_SET); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PB3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_3,Bit_SET); TIM_DeInit(TIM2); TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xffff; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIMx时钟频率除数的预分频值 不分频 TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //设置定时器2为编码器模式 IT1 IT2为上升沿计数 TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 6; //输入滤波器 TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清除所有标志位 TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //允许中断更新 TIM2->CNT = 0; TIM_Cmd(TIM2, ENABLE); }[/mw_shl_code]
encoder_num=TIM_GetCounter(TIM2);

encoder_num的值从1一直到32767,然后突然跳变到94934528



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。