本帖最后由 haov000 于 2017-11-27 17:00 编辑
stm32f407正交编码器接口,程序已经可以计数,但是不管怎么设置都是A相的上升沿下降沿都计数,即一个正交脉冲会计数2次,不知道stm32程序是不是只能这样,如果1个正交脉冲计数1次,是不是只能自己除以2,不能通过设置寄存器实现,还请指教下,代码如下:
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//引脚初始化
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
if (encoder->PhaseA != RT_NULL)
{
gpio_rcc_enable(encoder->PhaseA->gpio);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = encoder->PhaseA->gpio_pin;
GPIO_Init(encoder->PhaseA->gpio, &GPIO_InitStructure);
}
if (encoder->PhaseB != RT_NULL)
{
gpio_rcc_enable(encoder->PhaseB->gpio);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = encoder->PhaseB->gpio_pin;
GPIO_Init(encoder->PhaseB->gpio, &GPIO_InitStructure);
}
//TIM初始化
tim_rcc_enable(encoder, FALSE);
/* Timer configuration in Encoder mode */
TIM_DeInit(encoder->TIM);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;//No prescaling
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(encoder->TIM, &TIM_TimeBaseStructure);
//这里如果设置为TIM_EncoderMode_TI12,即1个脉冲计数4,否则1个脉冲计数2
TIM_EncoderInterfaceConfig(encoder->TIM, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(encoder->TIM, &TIM_ICInitStructure);
// Clear all pending interrupts
//TIM_ClearFlag(encoder->TIM, TIM_FLAG_Update);
//TIM_ITConfig(encoder->TIM, TIM_IT_Update, ENABLE);
TIM_Cmd(encoder->TIM, ENABLE);
|