cancel
Showing results for 
Search instead for 
Did you mean: 

Why my timer encoder mode oscillates in the same values?

ARami.8
Associate II

Hi, i'm using my STM32F7 nucleo 144 and i'm learning about timer encoder mode and i follow the example in the datasheet but when i check the CNT register it oscillates in the same values and if i use the A0-A1 pins the encoder works bad, can somebody help me please?

// ******************* PORT ****************
	RCC->AHB1ENR |=0x03; // Turn on A,B clocks
// *******************************************
			
// ******************* PINS *****************
	GPIOA->MODER |=0x800; // Alternative A5
	GPIOA->AFR[0] =0x100000; // Alternative function AF1
			
	GPIOB->MODER |=0x80; // Alternative  B3
	GPIOB->AFR[0] =0x1000; // Alternative Function AF1
// *******************************************
			
// ******************* ENCODER TIMER********
	RCC->APB1ENR |=0x01; // Turn on the TIM2 clock 
	TIM2->ARR =1000; // Maximun count
	TIM2->CCMR1 |=0x0101; // select TI1 and TI2
	TIM2->SMCR |=0x03; // Encoder mode 3
	TIM2->CR1 |=0x01; // Timer On
// *******************************************

4 REPLIES 4

Which 'F7?

Read out and check the GPIO registers content, in debugger. In debugger, observe the given pins' GPIO_IDR bit, if it changes according to the input signal. Check (using the Nucleo's User manual), if on the board there's nothing connected to given pins.

> if i use the A0-A1 pins the encoder works bad,

Do you mean PA0 and PA1? What exactly do you mean by "works bad"?

JW

I use the STM32F746ZGTX, and i check the GPIOA->IDR register in the debugger like you told me and it is strange, if i don't touch the encoder the PA1 pin oscillates. i wrote that works bad and exactly is that, when i move the encoder, for example 1 step in the debugger the cnt change 3 steps or 2 steps

> if i don't touch the encoder the PA1 pin oscillates.

Yes, but that's really what's happening on the encoder's output, isn't it.

Use an oscilloscope to observe the encoder's behaviour.

> 1 step in the debugger the cnt change 3 steps or 2 steps

1 step of what? You mean 1 detent on encoder intended as manual input? Those tend to produce a full four-quadrant signal upon moving one detent, so setting timer to count on both edges, it may quite well be that each detent increments the counter by 4.

JW

Thanks, when i conected my osciloscope i watched the proble, it was the cable between my encoder and the stm. if i move the encoder, the stm recognize it and the direction is correct. most all was perfect the "problem" is the value of the CNT register. I move my rotative encoder 1 step (my encoder is the 20 steps) but the CNT count 4 steps or 3 steps. i putted diferents values of the filter, the prescaler but always the CNT count the same values (3 steps or 4 if i move just 1 step in my encoder)