cancel
Showing results for 
Search instead for 
Did you mean: 

How to properly start and reset timers?

NDjor.1
Associate

I want to connect my MCU with a module which has a specific way of communication. Module sends messages of variable number of characters, where period between two characters is also variable. My idea is to use timer in order to overcome this problem. Let's assume that module sends 5 characters. After reception of the first character, timer will be activated. Assuming that the second character is sent shortly after the first, after it's reception timer will be reset and started again. Principe is the same for the rest 3 characters. As there is no more data to receive, timer will reach it's predefined, experimentally defined value and generate interrupt, therefore notify MCU about one full message reception.

In a nutshell, timer will be reset after reception of each character.

I want to configure TIM7 in normal mode on STM32G070. My main problem is how to reset timer. There are several ways I came across the internet:

  1. Enable/disable CEN bit in CR1 register - timer stops counting and keeps the current value (no reset, no interrupt). This method demands to set timer at initial value (I'm still not sure how), thus spending time
  2. Set UG bit in EGR register - timer is reloaded but global interrupt is triggered by setting UIF bit in SR register (interrupt should be activated only at the reception of the last character).

Reading MCU's reference manual doesn't gave me any idea how to reset timer. As mentioned above, is there a way to reset the timer?

1 REPLY 1

Just simply write zero value to TIM7->CNT register...

BTW: if you are receiving on UART, consider using UART "timeout" feature.