Skip to main content
Rfari.1
Associate
August 5, 2021
Solved

Custom made timer or ready made timer for STM32F103C8

  • August 5, 2021
  • 3 replies
  • 986 views

Hi.

I am a beginner in STM32 development. I created a custom timer for STM32F103C8 blue pill in the While Loop which will execute a function to send AT Command to GSM modem as described below. In terms of cpu usage, is it better to create a custom timer or use ready made timers in the MCU? Thank you in advance.

uint32_t counter = 0;
while (1) {	
 if (counter == 8000000){
 HAL_UART_Transmit(&huart1, (uint8_t *)"AT\r",sizeof("AT\r"), 50);
 counter = 0;
 }
 counter++;
}

This topic has been closed for replies.
Best answer by TDK

Also note that the compiler can optimize out the delay entirely because it has no other effect and the variables are not defined as volatile.

HAL_Delay would be a much better solution.

3 replies

KnarfB
Super User
August 5, 2021

What you have created is a busy loop consuming 100% CPU time, this is *not* called a "timer". The simplest improvement would be using HAL_Delay instead of the counter, thereby freeing alot of CPU time. HAL_Delay uses a timer interrupt internally. Next step could be using a dedicated hardware timer and calling HAL_UART_Transmit from its interrupt handler. This also depends on the overall design of your code.

TDK
TDKBest answer
August 5, 2021

Also note that the compiler can optimize out the delay entirely because it has no other effect and the variables are not defined as volatile.

HAL_Delay would be a much better solution.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Rfari.1
Rfari.1Author
Associate
August 5, 2021

Hi @TDK​ , @KnarfB​ 

Thank you for the feedback. HAL Delay is just what i need to estimate the timing for the AT command execution