Timer 使用NUCLEO-F072RB读取正交编码器输出

Timer 使用NUCLEO-F072RB读取正交编码器输出,timer,stm32,encoder,stm32f0,Timer,Stm32,Encoder,Stm32f0,我正在使用该板结合读取直流电机编码器的输出,以测量其速度。根据数据表,使用定时器TIM2和TIM3可以执行此读取。为此,我遵循和源代码编写了以下代码: /** - configure A6 for encoder input (T1) */ GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_I

我正在使用该板结合读取直流电机编码器的输出,以测量其速度。根据数据表,使用定时器TIM2和TIM3可以执行此读取。为此,我遵循和源代码编写了以下代码:

/** - configure A6 for encoder input (T1) */
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_Init (GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_InitStruct.GPIO_Pin, GPIO_AF_1);

/** - configure A7 for encoder input (T2) */
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_Init (GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_InitStruct.GPIO_Pin, GPIO_AF_1);

/**********************************************/
/* Encoder input setup */

/* TIM3 clock enabled */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

/* TIM3 DeInit */
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure); 

/* Configure the timer TIM3 for encoder input */
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,  TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_SetAutoreload (TIM3, 0xffff);

/* TIM3 counter enable */
TIM_Cmd(TIM3, ENABLE);
在这里,我对两个输入使用备用函数1,根据数据表,它对应于TIM3。据我所知,在这种设置下,每当T1上有上升沿(本例中为GPIO A6)时,TIM3计数器增加1,当T2(A7)上有上升沿时,计数器减少1

出于调试目的,我将一个值设置为可以监视的变量。如果计数器为1或更大,则设置值为
INT16\u MAX
,否则设置
0

if (TIM_GetCounter(TIM3) > 0x0) {
    Inports.Encoder_Input = INT16_MAX;
} else {
    Inports.Encoder_Input = 0;
}
在使用实际编码器之前,我曾尝试使用开关手动生成上升沿,并使用另一个GPIO的PWM输出为输入(GPIO的A6和A7)馈电。这样,我希望TIM3计数器开始向上/向下计数,但什么也没有发生。我注意到,当我将信号连接到A6或A7时,它会退化,这可能解释了为什么没有检测到上升沿。用直流万用表测量这些输入上的电阻,我得到大约40欧姆,这对于输入端口来说是非常低的

如何正确设置计时器和GPIO,以便能够读取编码器信号并使TIM3计数器开始计数