Recently used timer function, understand the next, Stm32 timer function is very powerful, things are also quite a lot of, datasheet also said a big paragraph, this I will not say, here only explain how to configure the simplest timing function configuration.
Environment Description: MCU:STM32F4 series, with a universal timer TIM3
The first thing to know is that TIM3 is attached to the APB1 bus, which is described in stm32f417xx_datasheet-p.19, there is a block diagram called Device overview, forget, or cut a picture
And what's the APB1 clock? This depends on your own configuration of how much, usually in the Setsysclock () function, I here is the system clock 4, the system clock here is 168MHz, so APB1 on the clock is 42MHz, here do not think TIM3 clock is 42MHz, Because APB1 to TIM3 also go through a multiplier, this multiplier only when the front system clock to APB1 of the frequency divider is not 1 o'clock, and we said before the APB1 is the system clock 4, so here the multiplier effective, so TIM3 clock for the APB1 octave, that is 84MHz.
OK, get the timer input clock, we configure the timer simple function actually only need two parameters: prescaler frequency and auto Reload value
Tout = ((arr+1) * (psc+1))/tclk;
Where arr is the automatic reload value, the PSC is the Prescaler value, and the TCLK is the timer clock.
For example, I want the 1s clock timer, you can set: Arr =9999;psc=8399;
Notice the value range of ARR and PSC here, the former is u32, the latter is U16.
Nvic_inittypedef nvic_initstructure;
TIM3 clock Enable
Rcc_apb1periphclockcmd (RCC_APB1PERIPH_TIM3, enable);
Enable the TIM3 gloabal Interrupt
nvic_initstructure.nvic_irqchannel = tim3_irqn;
nvic_initstructure.nvic_irqchannelpreemptionpriority = 0x3;
nvic_initstructure.nvic_irqchannelsubpriority = 0x3;
Nvic_initstructure.nvic_irqchannelcmd = ENABLE;
Nvic_init (&nvic_initstructure);
Tim_timebasestructure.tim_period = arr;
Tim_timebasestructure.tim_prescaler = PSC;
Tim_timebasestructure.tim_clockdivision = 0;
Tim_timebasestructure.tim_countermode = tim_countermode_up;
The up-counting mode, which is counting from 0 to arr, generates a tim_it_update interrupt when it counts to arr, and other interrupts are optional
tim_timebaseinit (TIM3, &tim_timebasestructure );
/* TIM interrupts Enable *
/Tim_itconfig (TIM3, tim_it_update, enable);
/* TIM3 Enable counter *
/Tim_cmd (TIM3, enable);
So the timer starts to work, and then writes the interrupt handler function,
void Tim3_irqhandler (void)
{
if (Tim_getitstatus (TIM3, tim_it_update)! = RESET)
{
tim_ Clearitpendingbit (TIM3, tim_it_update);
Your code
}
}
At this point, the timing function of the TIM3 is completed, by the PSC and ARR values, we can set the desired timing time.
Of course, this is just a simple function of timing, there are a lot of cool things, if there are errors, please correct me.