First, STM32 Universal timer principle
The STM32 series of CPUs, with up to 8 timers, where TIM1 and TIM8 are advanced timers capable of producing three pairs of PWM complementary outputs, are often used for three-phase motor drives whose clocks are generated by the APB2 output. The other 6 are normal timers, and the clocks are generated by the APB1 output.
is the STM32 reference manual on the clock distribution diagram for the Timer clock section:
As can be seen, the clock of the timer is not directly from APB1 or APB2, but from the input of a multiplier of APB1 or APB2, the blue part of the figure.
The following is a universal Timer 2 clock to illustrate the role of this multiplier: when the APB1 prescaler coefficient is 1 o'clock, the multiplier does not work, the clock frequency of the timer equals the frequency of APB1, when the APB1 prescaler coefficient is other values (that is, the prescaler coefficient is 2, 4, 8 or 16), the multiplier function, The clock frequency of the timer equals the frequency of APB1 twice times.
Maybe some classmates still do not understand, OK, let's give an example to illustrate. Assuming Ahb=36mhz, because the maximum allowable frequency of APB1 is 36MHz, so the prescaler coefficient of APB1 can take any value;
When the prescaler factor = 1 o'clock, the apb1=36mhz,tim2~7 clock frequency =36mhz (multiplier does not work);
When the prescaler factor = 2 o'clock, Apb1=18mhz, the tim2~7 clock frequency =36mhz under the action of the multiplier.
Some people will ask, since the need tim2~7 clock frequency =36mhz, why not directly take APB1 prescaler coefficient =1? The answer is: APB1 not only to provide the clock for the tim2~7, but also to provide clocks for other peripherals, set this multiplier can be used to ensure that other peripherals using a lower clock frequency.
Stm32 Peripheral User Manual,
Another example: When Ahb=72mhz, the APB1 prescaler coefficient must be greater than 2, because the maximum frequency of APB1 can only be 36MHz. If the APB1 prescaler factor = 2, the tim2~7 can still get a 72MHz clock frequency because of the multiplier. The ability to use a higher clock frequency has undoubtedly increased the resolution of the timer, which is why the multiplier was designed.
Timer_cfg (); //Timer configuration //turn on timer 2 tim_cmd (TIM2, ENABLE); voidtimer_config (void) { rcc_apb1periphclockcmd (RCC_APB1Periph _tim2,enable); tim_deinit (TIM2); tim_ timebasestructure.tim_period=2000-1; //the value of the auto-reload register tim_ Timebasestructure.tim_prescaler= (36000-1); //clock Prescaler frequency tim_timebasestructure.tim_clockdivision=tim_ckd_div1; //Sampling Crossover tim_timebasestructure.tim_countermode=tim_countermode_up; //Up counting Mode tim_timebaseinit (tim2,&tim_timebasestructure); tim_clearflag (TIM2 , tim_flag_update); // Clear overflow Interrupt flag tim_itConfig (tim2,tim_it_update,enable); tim_cmd (tim2,enable); /Opening Clock }
Let's explain each statement. First we want to use the timer, we must make the timer clock, this is the function rcc_apb1periphclockcmd (), and through it to open the rcc_apb1periph_tim2.
Tim_deinit (TIM2), the function is mainly used to reset the TIM2 timer to enter the initial state.
Then we assign a value to the automatic reload register, and the size of the Tim_period actually indicates that an update or break will occur once the tim_period count is required. Next we need to set the clock Prescaler frequency Tim_prescaler, here is a formula, we say for example: clock frequency =72mhz/(clock Prescaler + 1). Describes the current set of this tim_prescaler, directly determines the clock frequency of the timer. The popular point is that it counts how many times a second can be counted. such as the calculated clock frequency is 2000, that is,
A second accounting 2000 times, and if Tim_period is set to 4000, that is, 4,000 times the count will be interrupted once. Since the clock frequency is counted 2000 times a second, it will be interrupted once for 2 seconds.
The next code, there is a need to pay attention to, tim_timebasestructure.tim_countermode=tim_countermode_up; we generally use the up-counting mode, that is, each count will be added 1, Until the register overflow has been interrupted. Finally, do not forget, need to enable the timer!!
Interrupt time = (tim_prescaler+1) * (tim_period+1)/flk
Use the above formula to calculate: Interrupt Time (2000-1+1) * (36000-1+1)/72000000=1 sec
Step five: Write the interrupt service program. It is also important to note that one of the first steps to get into the interrupt service is to clear out the interrupt flag bit. Since we are using up overflow mode, we use the
The function should be: Tim_clearitpendingbit (tim2,tim_flag_update);.
Every jump 0.1ms
STM32 Universal Timer Configuration