cancel
Showing results for 
Search instead for 
Did you mean: 

HAL Delay () function does not work with HAL_UART_Receive()

xmart
Associate II

Hi everyone

I'm having this problem while using stm32L073RBT:

When I use HAL_UART_Receive() only it seems normal, but when I use HAL Delay () after or before HAL_UART_Receive() the program freezes,Knowing that I did not enable any interrupt!!

Thanks

11 REPLIES 11
TFranke
Associate II

Hmm, what exactly do you expect from your main loop code?

 while (1) { 

HAL_UART_Receive(&huart1,buf,200,500);

HAL_Delay(500);

    i++;

 }

From my point of view your code puts the UART into receive mode for 500ms, then sleeps for another 500ms without being able to receive data. This may work or not, it's a question of chance if you are in receive mode when you transmit your test data. By removing the delay function you just increase the chance for successful reception.

Take a look at HAL_UART_Receive_DMA() or HAL_UART_Receive_IT() functions instead to receive data in the background of your main program execution.

HTH,

Tobias

Right, but I know when the data comes in