Skip to main content
Rfari.1
Associate
Aug 5, 2021 4:13 AM UTC
Solved

Custom made timer or ready made timer for STM32F103C8

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.
TDK
TDKBest answer
Aug 5, 2021 1:54 PM UTC

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""."

3 replies

KnarfB
Super User
Aug 5, 2021 6:45 AM UTC

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
Aug 5, 2021 1:54 PM UTC

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
Aug 5, 2021 3:13 PM UTC

Hi @TDK​ , @KnarfB​ 

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