2024-02-28 07:12 PM
When I send data from esp32 to stm32, it works fine. But when esp32 initially receives data from stm32 using uart, the data seems to have some random characters.
When i send 75, it looks like this in serial monitor:
Received: %$^#^12
Received: 12
Received: 12
I used KeilC and ArduinoIDE. I just want to get "Received: 12" from stm32. This is my code:
esp32:
#include <HardwareSerial.h> #define UART_TX_PIN 17 // TX #define UART_RX_PIN 16 // RX int d1 = 7; int d2 = 5; char tx_array[10]; int flag = 0; HardwareSerial SerialUART(2); // Create an instance of HardwareSerial for UART1 void setup() { Serial.begin(115200); // Initialize Serial Monitor for debugging SerialUART.begin(115200, SERIAL_8N1, UART_RX_PIN, UART_TX_PIN); // Initialize UART1 } void loop() { tx_array[0] = d1 + '0'; tx_array[1] = d2 + '0'; tx_array[2] = '\n'; // Line feed character //tx_array[3] = '\0'; // Null terminator SerialUART.print(tx_array); if (SerialUART.available()) { String receivedData = SerialUART.readStringUntil('\n'); // Read until newline if (receivedData != "") { // Process received data Serial.println("Received: " + receivedData); } } delay(500); // Add a small delay to prevent flooding the buffer }
and stm32f103c8t6:
#include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "string.h" #include "stdlib.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ char tx_buffer[10] = {0}; char rx_buffer[10] = {0}; char rx_array[10] = {0}; char result[10] = {0}; int received_value1; int received_value2; uint8_t count = 0; uint8_t data = 0; volatile uint32_t delayCounter = 0;
#define DELAY_THRESHOLD 500
UART_HandleTypeDef huart1; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); char* fuzzy_logic_func(char input_array[]) { input_array[strlen(input_array)] = '\0'; received_value1 = input_array[0] - '0'; //char to int received_value2 = input_array[1] - '0'; int result_var = received_value1 + received_value2; sprintf(result, "%d", result_var); strcat(result, "\n"); delayCounter++; HAL_UART_Transmit(&huart1, (uint8_t*)result, strlen(result), 100); return result; } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { rx_buffer[count] = data; count++; if (data == 10) { for(int i = 0; i<10; i++) { rx_array[i] = rx_buffer[i]; rx_buffer[i] = 0; //count = 0; } count = 0; fuzzy_logic_func(rx_array); } HAL_UART_Receive_IT(&huart1, &data, 1); } int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); HAL_UART_Receive_IT(&huart1, &data, 1); while (1) { if (delayCounter >= DELAY_THRESHOLD)
{
// Perform actions that were delayed
// Reset the delay counter
delayCounter = 0;
} } } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } /** * @brief USART1 Initialization Function * @param None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOA_CLK_ENABLE(); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */
2024-02-29 04:12 AM - edited 2024-02-29 04:13 AM
Check by oscilloscope or logic analyzer Tx signal from STM32. When you use internal oscillator, signal timing can be out of tolerace.Or some garbage is in the transmit buffer.