Skip to main content
ACekm.1
Associate
December 6, 2022
Question

UART WITH STM32L476

  • December 6, 2022
  • 4 replies
  • 2985 views

i was planning to do an experiment i need to insert two numbers and calculate the sum of that two inserted number. Then i need to see them in terminal. However i have confused a bit. Could anyone help me? I get stuck over here.

 /* USER CODE END 2 */

 int First_Digit;

 int Second_Digit;

 int Sum_Digits;

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

   /* USER CODE END WHILE */

uint8_t First_Digit[] = "first digit \r\n"; // data to send

HAL_UART_Transmit(&huart2, First_Digit, 18, 100);

HAL_Delay(2000);

uint8_t Second_Digit[] = "second digit \r\n"; // data to send

HAL_UART_Transmit(&huart2, Second_Digit, 18, 100);

HAL_Delay(2000);

uint8_t Sum_Digits[] = "sum \r\n"; // data to send

HAL_UART_Transmit(&huart2, Sum_Digits, 12, 100);

HAL_Delay(2000);

   /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

This topic has been closed for replies.

4 replies

Bob S
Super User
December 6, 2022

"experiment" == "homework" ? :)

First thing, do not hard-code string lengths into HAL_UART_Transmit() calls. Use strlen() (**NOT** sizeof()) instead:

HAL_UART_Transmit(&huart2, First_Digit, strlen(First_Digit), 100);

So how are you stuck? What is your program doing that you don't expect? Or what is it NOT doing that you want it to do?

ACekm.1
ACekm.1Author
Associate
December 7, 2022

i can not printed the sum of two numbers. i can print first digit, second digit. however i can not be able to print sum and dont know how to make calculation in uart

Tesla DeLorean
Guru
December 6, 2022

You'll need to receiver characters, and perhaps a RETURN/ENTER character to mark the end of the input, and then convert those characters into a numeric form you can do math on.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
ACekm.1
ACekm.1Author
Associate
December 7, 2022

could u explain me more please if u check i have also posted full code that i have developed.

Tesla DeLorean
Guru
December 7, 2022

Make sure to NUL terminate input you are feeding to string functions.

Perhaps also watch the values returned by functions, and buffer length

int read_inputs(void)
{
 char buffer[255];
 int i=0;
 
 while(1)
 {
 char button_pressed;
 if (HAL_UART_Receive(&huart2, &button_pressed, 1, 100000000) == HAL_OK)
 {
 HAL_UART_Transmit(&huart2, &button_pressed, 1, 100000000);
 if ((button_pressed == '/r') || (button_pressed == '/n') || (i >= (sizeof(buffer) - 2)))
 break;
 buffer[i++] = button_pressed;
 }
 }
 
 buffer[i++] = 0; // NUL terminate
 
 return atoi(buffer);
}

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
December 7, 2022

A console example using interrupts and sw fifo would probably help a significant number of coders....

Bob S
Super User
December 7, 2022
Piranha
Principal III
December 7, 2022

For educational purposes one should actually go through all of those approaches: polling, IRQ, IRQ+DMA and IRQ with software FIFO.

S.Ma
Principal
December 10, 2022

Both methods cover 2 compromizes:

Best for reactivity and small ram usage

Best for low latency and minimizing interrupt occurence rate.

Thoughts?