How to Connect an Encoder to STM32.

One of my previous articles about encoders described how they work and ways to process the data coming from them. The methods described can be implemented on simple microcontrollers like the Atmega; more advanced ones, like STM32, have a dedicated encoder interface that processes the data from the encoder by itself and, based on this data, increments or decrements the counter register depending on the direction of rotation.
How to Connect an Encoder to STM32.

How to Connect an Encoder to STM32.

This interface is implemented using two channels (TI1 and TI2) of the selected timer. On the stm32f103vet6 I managed to get it (the encoder) working on timers three and eight; I also tried to get it working on timer one, but without success. Reading the internet, I found that other people had run into the same problem, i.e. they managed to get the encoder working on one timer but not on another, even though the timers were identical, as in my case with timers one and eight.

The signals from the inputs pass through a digital filter and then go to an edge detector. The input digital filter is needed to eliminate contact bounce and is essentially a decrementing (down-counting) counter; physically the bounce of course doesn't disappear, but thanks to a "special" algorithm the system doesn't "feel" it. The edge detector sets a 1 on the corresponding output depending on whether the signal is rising or falling: TI1F_Rising for rising and TI1F_Falling for falling. Then the signals go to a multiplexer that determines on which edge the capture happens, and then to the input selection multiplexer.
How to Connect an Encoder to STM32.

Above is a picture showing how the digital filter works. Suppose we know that the signal is unstable for 5 samples; then, after a transition to a new state, the microcontroller should not respond to changes of the input state for 8 samples (we take a margin). If after 8 samples the signal state is confirmed, it is passed to the TI1F output. We've sorted out the logic; let's move on to the configuration.

The ICxF bit field in the CCMR1 register is used to configure the input filter; besides the number of samples during which the microcontroller won't respond to input signals, you can also configure the sampling frequency. Essentially, we set the delay time from the arrival of an edge to the "confirming" sample.
How to Connect an Encoder to STM32.

Fdts is configured in the CKD bits, and CK_INT is most likely TIMxCLK from RCC; N is the number of samples.
How to Connect an Encoder to STM32.


In the SMCR register, the SMS bits determine on the edges of which input counting takes place, while whether the counter value increases or decreases depends on the state of the other input, as can be seen in the picture below.
How to Connect an Encoder to STM32.

  • SMS=‘001’ — counting on TI2FP2 edges
  • SMS=‘010’ — counting on TI1FP1 edges
  • SMS=‘011’ — counting on both edges

In the CCER register, the CCxP bits determine on which edge the TIxFP1 signal appears.
  • 1 — on the falling edge
  • 0 — on the rising edge

In the CCMR1 register, the CCxS bits configure the input selection multiplexer.

The value in the ARR register determines the value up to which the counter can change; when the counter value exceeds ARR, the counter resets to zero.

You can find out the direction of rotation by reading the DIR bit in the CR1 register.
  • 1 — the counter value decreases
  • 0 — the counter value increases


Now let's use the encoder to turn the LEDs on the board on and off one after another.

#include "stm32f10x.h"   

void init_tim8()
{	
        //enable clocking of timer 8
	RCC->APB2ENR |= RCC_APB2ENR_TIM8EN;

       //configure the pins the encoder is connected to as inputs with pull-up
	GPIOC->CRL &= ~(GPIO_CRL_CNF6_0 | GPIO_CRL_CNF7_0);
	GPIOC->CRL |= GPIO_CRL_CNF6_1 | GPIO_CRL_CNF7_1;
	GPIOC->ODR |= GPIO_ODR_ODR6 | GPIO_ODR_ODR7;
	
         //configure the filter
        TIM8->CCMR1 |= TIM_CCMR1_IC1F | TIM_CCMR1_IC2F;
        //configure the multiplexer
	TIM8->CCMR1| = TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
        //the TIxFP1 signal will appear on the rising edge
	TIM8->CCER &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
        //count on both edges 
	TIM8->SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;
         //maximum counter value 11
	TIM8->ARR = 11;
        //start the counter
	TIM8->CR1 |= TIM_CR1_CEN ;
	//reset the counter register
	TIM8->CNT = 0;

}
	

int main(void)
{
        //configure the pins the LED is connected to
	RCC->APB2ENR |= RCC_APB2ENR_IOPEEN | RCC_APB2ENR_IOPCEN; 
	GPIOE->CRL = GPIO_CRL_CNF5_1 | GPIO_CRL_CNF6_1;
	GPIOE->ODR |= GPIO_ODR_ODR5 | GPIO_ODR_ODR6;
	
	init_tim8();
	
	while(1)
	{
                //if the counter value is 4, turn on the first LED
		if(TIM8->CNT == 4)
		{
			GPIOE->BSRR = GPIO_BSRR_BR5;
		}
		else
		{
			 GPIOE->BSRR = GPIO_BSRR_BS5;
		}
		
		//if the counter value is 8, turn on the second LED
		if(TIM8->CNT == 8)
		{
			GPIOE->BSRR = GPIO_BSRR_BR6;
		}
		else
		{
			 GPIOE->BSRR = GPIO_BSRR_BS6;
		}
	}

In this example, the counter value changes by 4 with each click of the encoder.
I bought the encoder here.