2022-12-06 07:23 AM
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 */
}
2022-12-06 10:20 AM
"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?
2022-12-06 10:30 AM
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.
2022-12-06 06:42 PM
A console example using interrupts and sw fifo would probably help a significant number of coders....
2022-12-07 12:43 AM
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
2022-12-07 01:03 AM
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
/* 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, "\r\n First_Digit:", 18, 100);
read_inputs();
//L_Delay(1500);
uint8_t Second_Digit[] = "second digit \r\n"; // data to send
HAL_UART_Transmit(&huart2, "\r\n Second_Digit:", 18, 100);
read_inputs();
//L_Delay(1500);
uint8_t Sum_Digits[] = "sum \r\n"; // data to send
HAL_UART_Transmit(&huart2, "\r\n Sum_Digits:", 50, 100);
convert_cton();
//L_Delay(1500);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
int read_inputs() {
char button_pressed = "";
char buffer[255];
int i=0;
while(button_pressed != '\r')
{
HAL_UART_Receive(&huart2, &button_pressed, 1, 100000000);
HAL_UART_Transmit(&huart2, &button_pressed, 1, 100000000);
buffer[i++] = button_pressed;
}
return atoi(buffer);
}
//int read_outputs() {
// char button_pressed = "";
//char buffer[255];
//int i = 0;
// while(button_pressed !='\r')
//{
//HAL_UART_Receive(&huart2, &button_pressed, 1, 100000000);
// HAL_UART_Transmit(&huart2, &button_pressed, 1, 100000000);
// buffer[i++] = button_pressed;
//}
//return atoi(buffer);
//}
int convert_cton() {
char button_pressed = "";
int Sum_Digits = (int)(button_pressed)+0;
printf("%d", Sum_Digits);
return 0;
}
2022-12-07 03:44 AM
could u explain me more please if u check i have also posted full code that i have developed.
2022-12-07 07:41 AM
Not IRQ but DMA: https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx
2022-12-07 08:53 AM
For educational purposes one should actually go through all of those approaches: polling, IRQ, IRQ+DMA and IRQ with software FIFO.
2022-12-07 09:25 AM
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);
}