cancel
Showing results for 
Search instead for 
Did you mean: 

Custom made timer or ready made timer for STM32F103C8

Rfari.1
Associate

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++;
}

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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

View solution in original post

3 REPLIES 3
KnarfB
Principal III

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
Guru

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
Associate

Hi @TDK​ , @KnarfB​ 

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