新手上路
- 积分
- 36
- 金钱
- 36
- 注册时间
- 2016-3-11
- 在线时间
- 5 小时
|

楼主 |
发表于 2016-3-11 22:57:15
|
显示全部楼层
已经解决,是相应的GPIO配置的问题,GPIO要配制成AF模式,并且在打开AF功能的时候不能这么写
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6 | GPIO_PinSource7, GPIO_AF_TIM3);
而是要将PA6和PA7分别打开,像这样
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
改正之后就能读到编码器的数据了。
下面是完整的配置函数:
void wheel_encoder_configuration(void)
{
GPIO_InitTypeDef ENCODER;
TIM_TimeBaseInitTypeDef TIMER3, TIMER4;
TIM_ICInitTypeDef IC_ENCODER;
NVIC_InitTypeDef nvic;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE);
//PA6、PA7的复用功能一定要分开配置才行
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_TIM3);
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);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7, GPIO_AF_TIM4);
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 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*********/
nvic.NVIC_IRQChannel = TIM3_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 2;
nvic.NVIC_IRQChannelSubPriority = 2;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
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);
}
|
|