Set timer to count up to up to 300ms, from 0 to 4800 counts, and trigger an interrupt on every overflow, in this interrupt the overflow value will change using the special macro __HAL_TIM_SET_AUTORELOAD
#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 up to 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_UP; /*count from Period value to zero*/
TIM_Handler.Init.Period = 4800; /*count to 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 );/*fix priority to level 2*/
HAL_NVIC_EnableIRQ( TIM1_BRK_UP_TRG_COM_IRQn );
/*init the timer count up to 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 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 )
{
static uint8_t flag = 0;
const uint32_t values[] = { 1600, 4800 };
/*Using this macro like function we set a new overflow value, in this case will be
1600 or 4800*/
__HAL_TIM_SET_AUTORELOAD( htim, values[ flag ] );
/*toggle the variable to help us switch the overflow values for the next
interrupt*/
flag ^= 0x01;
}
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*/
HAL_TIM_IRQHandler( &TIM_Handler );
}