Timer TIM4上的STM32旋转编码器配置

Timer TIM4上的STM32旋转编码器配置,timer,stm32,Timer,Stm32,我试图在STM32F4DISCOVERY板上使用TIM4作为正交编码器输入。 这是我的密码: RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStru

我试图在STM32F4DISCOVERY板上使用TIM4作为正交编码器输入。 这是我的密码:

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);

/* Configure the timer */
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

/* TIM4 counter enable */
TIM_Cmd(TIM4, ENABLE);
遗憾的是,当我转动编码器时,TIM4->CNT计数器不会移动。我和TIM8配合得很好。 以下是工作TIM8和非工作TIM4的完整代码:


手动移动编码器后,我通过在gdb中打印rT2()进行检查。

除非使用外部电阻器,否则可能需要使用上拉输入
或者给编码器供电。我通常将编码器接地,并使用内部上拉设置空闲状态。

我使用STM32F407从3个光学编码器读取编码器计数。我使用的是ChibiOS RTOS,因此计时器结构与ST外围库计时器结构略有不同,但信息基本相同。以下是我如何配置实际计时器的寄存器:

stm32_tim_t * encoderTimers[3] = {STM32_TIM8, STM32_TIM3, STM32_TIM4};
for (auto timer : encoderTimers) {
  timer->SMCR = 3;          // Encoder mode 3
  timer->CCER = 0;          // rising edge polarity
  timer->ARR = 0xFFFF;      // count from 0-ARR or ARR-0
  timer->CCMR1 = 0xC1C1;    // f_DTS/16, N=8, IC1->TI1, IC2->TI2
  timer->CNT = 0;           // Initialize counter
  timer->EGR = 1;           // Generate an update event
  timer->CR1 = 1;           // Enable the counter
}
此外,请确保在RCC的APB1或APB2寄存器中启用计时器外围设备。比如:

RCC->APB1ENR |= ((1 <<  2)  // TIM4  --  Front wheel angle measurement
             |   (1 <<  1));// TIM3  --  Steer angle measurement

RCC->APB1ENR |=((1带标准驱动器的编码器

#include "mbed.h"
#include "stm32f4xx.h"
#include "stm32f4xx_hal_tim_ex.h"

TIM_HandleTypeDef timer;
TIM_Encoder_InitTypeDef encoder;

//direction to PA_9 -- step pulse to PA_8

int main(){
     GPIO_InitTypeDef GPIO_InitStruct;
        __TIM1_CLK_ENABLE();
        __GPIOA_CLK_ENABLE();
        GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLDOWN;
        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    timer.Instance = TIM1;
    timer.Init.Period = 0xffff;
    timer.Init.Prescaler = 0;
    timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    timer.Init.CounterMode = TIM_COUNTERMODE_UP;

    encoder.EncoderMode = TIM_ENCODERMODE_TI12; 
    encoder.IC1Filter = 0x0f;
    encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; 
    encoder.IC1Prescaler = TIM_ICPSC_DIV4;
    encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;

    encoder.IC2Filter = 0x0f;
    encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING;    
    encoder.IC2Prescaler = TIM_ICPSC_DIV4;
    encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI;

    HAL_TIM_Encoder_Init(&timer, &encoder);
    HAL_TIM_Encoder_Start(&timer,TIM_CHANNEL_1);   


    TIM1->EGR = 1;           // Generate an update event
    TIM1->CR1 = 1;           // Enable the counter


 while (1) {
        int16_t count1; 
        count1=TIM1->CNT; 

        printf("%d\r\n", count1);
        wait(1.0);

 };
}

谢谢!我花了很多时间研究STM32的旋转编码器教程和库,这是唯一一个有效的解决方案。