2

我正在尝试将 TIM4 用于我的 STM32F4DISCOVERY 板上的正交编码器输入。这是我的代码:

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 的完整代码: https ://gist.github.com/nraynaud/5082298

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

4

3 回答 3

4

我使用 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

最后,您需要确保正确配置 GPIO 设置。这意味着将 GPIO 配置为备用功能。

于 2013-05-15T22:48:21.850 回答
2

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

于 2013-04-20T20:01:54.200 回答
2

带标准驱动器的编码器

#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);

 };
}
于 2015-10-18T13:43:31.100 回答