The program will setup timer to an overflow for a period of time of 300ms, but the interrupt will not be triggered until this overflow happens three times
#include "bsp.h"
int main( void )
{
TIM_HandleTypeDef TIM_Handler; /*TIM initial structure*/
HAL_Init(); /*Init HAL library*/
__HAL_RCC_TIM1_CLK_ENABLE(); /*Enable clock on TIM1*/
/*configure timer 1 with a preescaler of 1000 and starting count of 4800
by default the timer get a Tfrec of 16MHz from the APB bus*/
TIM_Handler.Instance = TIM1; /*Timer TIM to configure*/
TIM_Handler.Init.Prescaler = 1000; /*preescaler Tfrec / Prescaler*/
TIM_Handler.Init.CounterMode = TIM_COUNTERMODE_DOWN; /*count from Period value to zero*/
TIM_Handler.Init.Period = 4800; /*down count from 4800*/
TIM_Handler.Init.RepetitionCounter = 3; /*repeat the count another 3 times before generate an update event*/
/*use the previous parameters to set configuration on TIM1*/
HAL_TIM_Base_Init( &TIM_Handler );
/*Enable interrupt vector TIM1_BRK_UP_TRG_COM_IRQ where the CPU will jump in any of
the following events: TIM1 Break, Update, Trigger and Commutation */
HAL_NVIC_SetPriority( TIM1_BRK_UP_TRG_COM_IRQn, 2, 0 );/*se fija la prioridad a nivel 2*/
HAL_NVIC_EnableIRQ( TIM1_BRK_UP_TRG_COM_IRQn );
/*init the timer count from oveflow value (4800 in this case) and trigger an interrupts*/
HAL_TIM_Base_Start_IT( &TIM_Handler );
while (1)
{
/*Do nothing*/
}
}
/* when the timer TIM1 reaches its count down to zero the number of times indicated
in RepetitionCounter this function will be called by HAL_GPIO_EXTI_IRQHandler
which is in turn called from the TIM1_BRK_UP_TRG_COM_IRQHandler interrupt vector*/
void HAL_TIM_PeriodElapsedCallback( TIM_HandleTypeDef *htim )
{
/*This code will reached every 300ms * (RepetitionCounter + 1) */
}