In order to calculate the current speed, one option is to read the current position every 20ms. The average speed is position_old minus position_current. This works well if there are many pulses per 20ms but at low speeds, the accuracy drops. Once you get speed=0, then speed=2 and then speed=0 again.
The better approach would be to measure the time between rising flanks. This provides you with the exact time, hence the speed.
- But can you have two timers active on the same pins? Then Input Capture would be the way to go.
- Can you configure an ISR to fire at the rising flank while the timer is used as encoder? Then I can use the systicks to derive the speed.
I am using TIM5 CH1&2 on PA0&1.
(I could obviously connect the input pin to another MCU pin and do the measurement there but don't like that workaround.)
https://github.com/wernerdaehn/CC3D-CableCam-Controller/blob/master/Implementation.md