Skip to main content
Graduate II
May 7, 2024
Solved

Timer's Combined Channels - Encoder Mode

  • May 7, 2024
  • 1 reply
  • 1540 views

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.

    This topic has been closed for replies.
    Best answer by AScha.3

    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 . :)

    1 reply

    AScha.3Answer
    Super User
    May 7, 2024

    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 . :)