Set timer to count form 0 to 10000 counts equivalent to 625ms
#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 preesvlare of 1000 y and overflow count of 10000
by default the timer get a Tfrec of 16MHz*/
TIM_Handler.Instance = TIM1; /*Tiomer TIM to configure*/
TIM_Handler.Init.Prescaler = 1000; /*preescaler Tfre / Prescaler*/
TIM_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; /*count from 0 to overflow value*/
TIM_Handler.Init.Period = 10000; /*limit count (overflow)*/
/*use the previous parameters to set configuration on TIM1*/
HAL_TIM_Base_Init( &TIM_Handler );
/*init the timer count from 0*/
HAL_TIM_Base_Start( &TIM_Handler );
while (1)
{
/*Iquire if the timer reaches its overflow value, this is going to set the UIF flag*/
if( __HAL_TIM_GET_FLAG( &TIM_Handler, TIM_FLAG_UPDATE) == SET )
{
/*Clear the UIF flag, it must be done by software*/
__HAL_TIM_CLEAR_FLAG( &TIM_Handler, TIM_FLAG_UPDATE );
/*do something, the program will reach here every
Time = 1 / (Tfrec / Preescaler / Period)
Time = 1 / (16MHz / 1000 / 10000) = 625ms*/
}
}
}