STM32 timer timing calculation

Assume that the system clock is 72Mhz, TIM1 is obtained by PCLK2 (72MHz), TIM2-7 is obtained by PCLK1

key is to set the clock prescaler, auto-reload The value of the register cycle

/* occurs every 1 second (into the interrupt service routine).

RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2 of SystemInit() of RCC_Configuration() indicates that TIM3CLK is 72MHz. Therefore, each time you enter

断服务程序 interval is


((1+TIM_Prescaler)/72M)*(1+TIM_Period )=((1 +7199)/72M)*(1+9999)=1 seconds*/

The basic settings of the timer

1, TIM_TimeBaseStructure.TIM_Prescaler = 7199;//clock prescaler :时

钟频=72/(clock prescaler +1)

2, TIM_TimeBaseStructure.TIM_Period = 9999; // Automatic reload register cycle value (timing

时间) Cumulative 0xFFFF After the frequency, an update or interrupt is generated (also said to be timed)

3, TIM_TimeBaseStructure.TIM_CounterMode = TIM1_CounterMode_Up; //Timer

mode Upcount

4, TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //Time split value

5, TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Initialize timer 2

6, TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //Open interrupt Overflow interrupt

7 , TIM_Cmd (TIM2, ENABLE); / / open the timer


TIM_TimeBaseStructure.TIM_Prescaler = 35999; / / crossover 35999 72M /

(35 999+1)/2=1Hz 1 second interrupt overflow once

TIM_TimeBaseStructure.TIM_Period = 2000; //Count value 2000

((1+TIM_Prescaler )/72M)*(1+TIM_Period )=(( 1+35999)/72M)*(1+2000)=1 seconds*/