The HAL_Delay function allows us to insert delays in milliseconds by counting through the interrupt of the Systick timer configured by the HAL library to occur every millisecond ( this is done in the HAL_InitTick
function called in HAL_Init
). But we can also use it to establish time periods with a "seed" variable.
#include "bsp.h"
int main(void)
{
/*seed variable to track the last time we check the systick count*/
uint32_t tickstart;
/*Init library and the systick timer*/
HAL_Init();
/*get the current tick for the first time*/
tickstart = HAL_GetTick();
while (1)
{
/* query if already pass 200ms (ticks) since we got the current tick value*/
if( (HAL_GetTick() - tickstart) >= 200 )
{
/*at this point 200ms has been elapsed and we need to take the current tick count again*/
tickstart = HAL_GetTick();
/*we do anything we want here every 200ms.
but is important the code we wrote here DO NOT spend more than 200ms,
otherwise periodicity will be affected */
}
}
}
ints.c
/*To track the tick count we need to call the function HAL_IncTick on the vector interrupt*/
void SysTick_Handler( void )
{
HAL_IncTick( );
}