学前班
最后登录1970-1-1
在线时间 小时
注册时间2017-8-24
|
最近在使用F429的编码器模式,从网上找了好多资料,感觉结果并不理想,电机买的是平衡小车之家的49那款,AB项编码器.
发现 TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising)函数 ;后面2个参数配置为TIM_ICPolarity_Rising或者TIM_ICPolarity_Falling,转动电机编码盘,计数器基本无变化,0,1之间抖动。 设置为TIM_ICPolarity_BothEdge后,可以计数, 但该库函数,标准的值里没有TIM_ICPolarity_BothEdge 这个选项。不理解啊,还请各位看下。小弟只想在上升沿读取编码器的值。该如何配置?
附上部分代码
完整的有点大,发不上来
// 编码器的配置代码
void Encoder_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 10000;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
// NVIC_choice(1); //不开中断
TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising TIM_ICPolarity_BothEdge éÏéyÑØ2¶»ñ
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0;//ÂË2¨
TIM_ICInit(TIM1,&TIM_ICInitStructure);
//TIM_ClearFlag(encoder_TIM,TIM_FLAG_Update); //清除TIM1标记,不开中断,未使用
//TIM_ITConfig(encoder_TIM,TIM_IT_Update,ENABLE); //使能TIM1中断
TIM_SetCounter(TIM1,0); //设置计数器的值为0
TIM_Cmd(TIM1, ENABLE); //启用TIM2
}
//编码器的配置代码
void Encoder_GpioConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_TIM1);
}
void delay(uint32_t delay)
{
for( ; delay !=0 ; delay--);
}
int main(void)
{
int value = 0;
int fx = 0;
Debug_USART_Config(); ////配置串口
Encoder_GpioConfig(); //配置编码器GPIO
Encoder_Config(); //配置编码器
LED_GPIO_Config(); //LED配置
GPIO_Configuration(); //GPIO配置,高电平让电机转动
GPIO_SetBits(GPIO_PIN_IN1_2_PORT,GPIO_PIN_IN2); //使电机转动
while(1)
{
printf("计数值 = %d \n",Encoder_Val); // Encoder 为 TIM1->CR1
delay(100000);
};
}
|
|