cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Encoder limits

Dmytro Medvid
Associate II

I`m using an encoder on STM32G4 series to set speed of the pump motor. I have values between 0 and 100. I set autoreload value as 200. I`m looking for the solution to avoid counter autoreload. I mean, when counter goes to 200 - it stays 200 instead of become zero after one more turn forward. And if it`s zero - to avoid becoming 200 after one turn backward.

2 REPLIES 2
KnarfB
Principal III

I see no need to limit the counter to a low value. Let it count up to the max 2^16-1 value, the full uint16_t range and beyond (0xFFFE, 0xFFFF, 0, 1, 2,...). To determine the speed, subtract the current counter value from the last value seen. If you do that in uint16_t arithmetic, over-/underflows are handled by the hardware. Do this in a periodic timer interrupt to get the speed as encoder increments per time unit.

hth

KnarfB

Tinnagit
Senior II

I'm unsure that what 's your exactly trouble.
because if you need to find the position or direction of motor that counter is very useful to find it.

but if you want to find the speed only that counter is unnecessary, you just use interrupt to count a pulse on one line and compare it to time. you will get the speed.

but if you need to find both. it 's more complicated you need to keep a previous position and it time to compare with new position and new time. you will get them.