本帖最后由 燕川离影 于 2016-3-14 15:48 编辑
最近在学习stm32F405,正交编码器读出来的值一直不正常。两个电机编码器,分别用了TIM3的CH1、CH2和TIM4的CH1、CH2.在SysTick中断(20ms一次)中读取CNT寄存器的值,减去预设值(0xfff),得到cnt的值。读出来的cnt值一直在试0、1、-1之间变化,不知道什么原因。程序是照着F103的代码写的,又参考网上的做了修改,一直调不出来,求大神指点。配置函数:
void wheel_encoder_configuration(void)
{
GPIO_InitTypeDef ENCODER;
TIM_TimeBaseInitTypeDef TIMER3, TIMER4;
TIM_ICInitTypeDef IC_ENCODER;
NVIC_InitTypeDef nvic;
/*enabe clocks*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE);
/*open the alternative function*/
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6 | GPIO_PinSource7, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6 | GPIO_PinSource7, GPIO_AF_TIM4);
/* Configure PA.06,07 as encoder1 input */
ENCODER.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
ENCODER.GPIO_Mode = GPIO_Mode_AF;
ENCODER.GPIO_Speed = GPIO_Speed_100MHz;
ENCODER.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &ENCODER);
/* Configure PB.06,07 as encoder2 input */
ENCODER.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
ENCODER.GPIO_Mode = GPIO_Mode_AF;
ENCODER.GPIO_Speed = GPIO_Speed_100MHz;
ENCODER.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOB, &ENCODER);
/***********configure the TIM3**********/
TIMER3.TIM_Prescaler = 0;
TIMER3.TIM_Period = 0xffff;
TIMER3.TIM_ClockDivision = TIM_CKD_DIV1;
TIMER3.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIMER3);
/***********configure the TIM4***********/
TIMER4.TIM_Prescaler = 0;
TIMER4.TIM_Period = 0xffff;
TIMER4.TIM_ClockDivision = TIM_CKD_DIV1;
TIMER4.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIMER4);
/**********Set the TIM3 and TIM4 to the ecoder mode***************/
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
/**********Set TIM3 and TIM4 to the input capture mode***************/
IC_ENCODER.TIM_Channel = TIM_Channel_1;
IC_ENCODER.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM3, &IC_ENCODER);
IC_ENCODER.TIM_Channel = TIM_Channel_2;
IC_ENCODER.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM3, &IC_ENCODER);
IC_ENCODER.TIM_Channel = TIM_Channel_1;
IC_ENCODER.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM4, &IC_ENCODER);
IC_ENCODER.TIM_Channel = TIM_Channel_2;
IC_ENCODER.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM4, &IC_ENCODER);
/*************Configurate interrupts of TIM3 and TIM4*********/
/* Enable the TIM3 Update Interrupt */
nvic.NVIC_IRQChannel = TIM3_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 2;
nvic.NVIC_IRQChannelSubPriority = 2;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
/* Enable the TIM4 Update Interrupt */
nvic.NVIC_IRQChannel = TIM4_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 2;
nvic.NVIC_IRQChannelSubPriority = 2;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
/*Clear the interruput flag and then enable the interrupt*/
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
/*Set TIM3 and TIM4 CNT to 0x7fff*/
TIM_SetCounter(TIM3,0x7fff);
TIM_SetCounter(TIM4,0x7fff);
/*Enable TIM3 and TIM4 */
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
读值函数:
int encoderR_get_cnt(void)
{
/*get the speed of right wheel */
int cnt = 0;
cnt = (TIM3 -> CNT) - 0x7fff;
TIM3 -> CNT = 0x7fff;
return cnt;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>