Weird behavior of timer after setting CNT register to 0 after specific time
- August 25, 2023
- 4 replies
- 3983 views
Hi,
I have background knowledge of programming and MCUs but I am fairly new to the STM32 ecosystem. I am trying to measure the RPM of a motor using an optical encoder that gives pulses on the output. I have done this on Arduino using the interrupts and I know that the encoder works well. My application requires me to do it with STM32F030C8T6. I have taken the approach of using two timers for this purpose. I am using TIM3 of this MCU in Encoder mode and TIM14 to generate an interrupt after every 100ms to read the CNT register of TIM3 and then reset it to 0 so that next time I can get the number of pulses. I am sending the number of pulses through a serial to a laptop. Everything is fine until I set the CNT register to zero. The counter increments correctly in increasing order, an interrupt is generated after every 100 ms, and data is sent to serial. but I need to set the CNT to zero to get the number of pulses in 100ms not the incremented value every time. When I set the CNT to 0 as shown in the serial terminal below for some time I get the correct value which is "20" but other times I get 16,17,18 or sometimes under 10. I have attached the SS of the code.