HAL library timer 3 interrupt flip LED

  Turn on timer 3 to realize the LED flip every 1s, the formula  Tout = (ARR + 1) * (PSC + 1) / TCLK

(1) ARR: automatic reload value / count cycle;

(2) PSC: frequency division coefficient;

(3) TCLK: input clock 72M

 

 

 

 

 

 

  • Timer introduction:

There are 8 timers in the SMT32F1 series: (differentiation)
advanced timers (TIM1, TIM8); / 16 bits / can generate DMA requests / have capture and / compare channels / have complementary outputs / can be used for PWM motor control
general timer (TIM2 , TIM3, TIM4, TIM5); / 16-bit / no capture
basic timer (TIM6, TIM7). / 16-bit / no capture

 

  1. cubeMX starts timer 3 

  2. Join in main.c

(1) __HAL_TIM_CLEAR_IT (& htim3, TIM_IT_UPDATE); // Clear the update interrupt flag in the TIM initialization process to avoid interruption when you start TIm

(2) HAL_TIM_Base_Start_IT (& htim3);  // Enable the update interrupt of TIM3 and start the timer

 

  3. Set the overflow time in tim.c: Tout = (ARR + 1) * (PSC + 1) / TCLK

 

 

 

 

   4. Add TIM3 interrupt callback function in tim.c

 

 

 

 

(Ps: Interrupt service function in the process: [void TIM3_IRQHandler (void)] Repeated definition leads to compilation failure, in fact, void TIM3_IRQHandler itself has no need to write)

 

Guess you like

Origin www.cnblogs.com/oneme1world/p/12677517.html