Set timer to count down to 300ms, from 4800 to 0 counts, and trigger an interrupt on every underflow.
#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*/
/*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 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 according to TIM1 configuration and the formula:
Period = 300ms / (Preeescaler / Tfrec)
Perios = 300ms / (1000 / 16MHz) = 4800*/
}
ints.c
/*it is necesary to make a reference to TIM structure*/
extern TIM_HandleTypeDef TIM_Handler;
/*Declare interrupt service rutine as it is declare in startup_stm32g0b1xx.s file*/
void TIM1_BRK_UP_TRG_COM_IRQHandler( void )
{
/*HAL library function that attend interrupt on TIM timers
this fucntion amount other things will clear the timer flag and the corresponding
callback functio*/
HAL_TIM_IRQHandler( &TIM_Handler );
}