STM32 Timer Timing Calculation

Time:2024-3-21

STM32 Timer Frequency

STM32 Timer Timing Calculation
Timing Time = Timer Frequency / Multiplier / Load Cycle

htim1.Init.Prescaler = 72-1;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 1*1000*1000;

tim = 72×10^6 / (72-1)/ 110001000 = 1us

According to the frequency of the timer clock, for example, the frequency of the clock is 72MHZ, it can be understood that STM32 will count 72M times in one second by itself, the pre-dividing coefficient is to divide the frequency, for example, the dividing coefficient is 72, then the frequency of this clock will become 72MHZ/72=1MHZ, but pay attention to the setting, the value should be 72-1. assuming that the dividing coefficient is 72-1, then the The frequency becomes 1MHZ, which means that the STM32 will count 1M times a second, i.e. once in 1us. Well, the next step is to determine the preload value, such as the need to time 1ms, because 1ms = 1us * 1000, then the preload value is 1000-1; so analogous, in the pre-scaler coefficient is determined in the case of the length of the timing is determined by the preload value. As for the reason for decreasing the value by one, I guess it is because the counting starts from 0, so it has to be decreased by one.

(for) instance

//For the frequency of 71MHZ, 500ms interrupt once, these two parameters are set as follows:
TIM_Prescaler=7199; //prescaler value
TIM_Period=4999; //next update event loads the value of the active auto-reload register period

   //split frequency 7200, using 72000000/7200=10000Hz
   //The period at this point is 1/10000=0.0001s
   //500ms=0.0001s*5000 times
   //5000-1=4999 times
   //i.e. through 4999 runs of the system, that is 500ms

official

An understanding of the timer period formula:

T=(arr+1)*(PSC+1)/Tck

where TCK is the clock frequency, PSC is the clock preshunt coefficient, and arr is the auto-reload value.

f=Tck/(psc+1)*(arr+1)

Tck/(psc+1) is the clock frequency, 1/f is the machine period, and multiplying by (arr+1) gives the timer period.
Example: TCK=72MHZ,psc=71. clock period=1us.(arr+1) value is whatever the timer period is in milliseconds.

reference

Recommended Today

[linux] Permission Understanding

catalogs 1. shell commands and how they work 2. The concept of authority 3. Authority management 2.1 Classification of document visitors (persons) 2.2 File types and access rights (thing attributes) 2.3 Representation of file permission values 2.4 Methods for setting file access rights 3. The file directive 4. Permissions for directories★ 5. Sticky bits★ 6. […]