cancel
Showing results for 
Search instead for 
Did you mean: 

Timer's Combined Channels - Encoder Mode

HMsDobby2
Associate II

Hello,

 

I'm using the nucleo-h743zi2 board.
When obtaining the value of TIM2->CNT (or TIM5->CNT) through the combined channels of TIM2 or TIM5 on this board, without any special settings, I can perceive the encoder rotating in reverse as a negative value as it passes through 0. (For example, with a Counter Period set to 1000, rotating in the negative direction would result in 0, -1, -2, -3, etc.)

However, when obtaining encoder values from other General Purpose Timers, it doesn't result in negative values but instead jumps to the maximum positive value. (For example, with a Counter Period set to 1000, rotating in the negative direction would result in 0, 1000, 999, 998, etc.)

Is it possible to obtain negative values in this situation through settings?
The difference I found between these two timers is that the counter resolution is 32-bit, whereas in other General Purpose Timers, it's 16-bit.

 

Thank you in advance for any advice you can provide.

1 ACCEPTED SOLUTION

Accepted Solutions
AScha.3
Chief

Hi,

the counter have no negative values :

see rm:

AScha3_0-1715063457687.png

So you read out timer with something wrong : maybe you read it as int ?

(signed...so compiler will show 0xFFFF as -1 . )

Read it, as it is : uint16_t , or uint32_t . Then check, what it gives you back.

 

btw

You could do it intentionally also, to get negative values : if you set the ARR to max. value (0xFFFF for 16 bit counter), and read it as int16_t , then compiler will use it as signed and you get: 0,1,2,3...or 0,-1,-2,...:

but be aware of roll over : turn negative will go from 0 to 0xFFFF , 0 -> -1 , but at 0x8000 next is 0x07FFF,

so - 32768 -> + 32767 . 🙂

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

1 REPLY 1
AScha.3
Chief

Hi,

the counter have no negative values :

see rm:

AScha3_0-1715063457687.png

So you read out timer with something wrong : maybe you read it as int ?

(signed...so compiler will show 0xFFFF as -1 . )

Read it, as it is : uint16_t , or uint32_t . Then check, what it gives you back.

 

btw

You could do it intentionally also, to get negative values : if you set the ARR to max. value (0xFFFF for 16 bit counter), and read it as int16_t , then compiler will use it as signed and you get: 0,1,2,3...or 0,-1,-2,...:

but be aware of roll over : turn negative will go from 0 to 0xFFFF , 0 -> -1 , but at 0x8000 next is 0x07FFF,

so - 32768 -> + 32767 . 🙂

If you feel a post has answered your question, please click "Accept as Solution".