我正在尝试在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的完整代码:
https://gist.github.com/nraynaud/5082298
我手动移动编码器后在gdb中打印rT2()进行检查。
答案 0 :(得分: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配置为备用功能。
答案 1 :(得分:2)
除非使用外部电阻,否则可能需要使用上拉输入 或为编码器供电。我通常将编码器连接到地,并使用内部上拉来设置空闲状态。
答案 2 :(得分:0)
带标准驱动程序的编码器
#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);
};
}