Multiple choices for microsecond delay, which one's better?
Hello, First time using the forum so apologies if I mess something up...
Long story short I wanna get a microsecond delay, I know 2 ways of doing it:
1) I can start a timer with default cube values and use a function like:
void delay (uint16_t delay)
{
__HAL_TIM_SET_COUNTER(&htim1, 0);
while( __HAL_TIM_GET_COUNTER(&htim1) < delay);
}to get the desired delay, or
2) I can start timer, set its prescaler value to specific frequency with interrupt turned on, drop it down to 1khz/mhz for easier calculations and in timer callback function have some variable increase by 1, then use it in different function as time measurement, something similar to this:
uint8_t counter;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
counter++;
}
void main() {
while(){
if(counter==20){
// do smth
counter=0;
}
}
}I have used both of these methods previously and know one is running in blocking mode, other in non-blocking mode. So that got me wondering, what other differences do they have, if blocking mode isn't an issue which one is more preferable to use, takes up less space, is more efficient and accurate? And is there another method even better than these two I can use?
