2024-05-06 10:59 PM
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.
Solved! Go to Solution.
2024-05-06 11:34 PM - edited 2024-05-07 12:01 AM
Hi,
the counter have no negative values :
see rm:
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 . :)
2024-05-06 11:34 PM - edited 2024-05-07 12:01 AM
Hi,
the counter have no negative values :
see rm:
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 . :)