How to setup Interface Encoder mode for the STM32F0

40 Views Asked by At

Alright, so my goal is to setup Interface Encoder mode on pins a1 and a5. To read the TIM2->CNT. Ive got a rotary Encoder hooked up to both pins and ground/5v. I've looked at the datasheet and I think I've configured the setup properly but I cant get it to work.

This is my setup

void init_hardware_timer_version(void)
{
    RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Enable GPIOA clock
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // Enable TIM2 clock

    TIM2->ARR = 12; // Set auto-reload register to maximum value

    // Configure capture/compare mode
    TIM2->CCMR1 |= (TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0);

    // Configure capture/compare polarity
    TIM2->CCER &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);

    // Configure the slave mode controller
    TIM2->SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;

    // Enable the timer
    TIM2->CR1 |= TIM_CR1_CEN;
}

Now my expectation is that after calling up on theTIM2->CNT in the main that the value changes based on the rotation of the Encoder. However the CNT seems to remain 0. I'm not sure why its not working.

Edit: This is how I setup the pins

GPIOA->MODER |= GPIO_MODER_MODER1_1 | GPIO_MODER_MODER5_1; // Set PA1 and PA5 to alternate function mode
GPIOA->AFR[0] |= (1 << (1 * 4)) | (1 << (5 * 4)); // Set alternate function AF1 for PA1 and PA5 (TIM2_CH2 and TIM2_CH1)
1

There are 1 best solutions below

0
wek On

You haven't told us which STM32F0xx are you using; but in all 'F0xx datasheet I've checked ('F031/'F042/'F051/'F072/'F091), TIM2_CHx on PA1 and PA5 is AF2, not AF1.