cancel
Showing results for 
Search instead for 
Did you mean: 

ultrasonic A02YYUW not working in STM32L443VCT6

roystm321
Associate III

Hi,

I try to comuunicate between STM32L443VCT6  and ultrasonic A02YYUW (By UART1) 

Just to make sure that the ultrasonic is working ok, i tested it by UART1 with model: stm32 neucler f441 and everthing works ok.

 

This is the code for the STM32L443VCT6:

 

 

 

#include "main.h" #include "string.h" #include "stdio.h" #include "stdlib.h" #include <stdbool.h> UART_HandleTypeDef huart1; uint8_t data[4] = {0}; // Buffer to hold the 4 bytes from the sensor float distance = 0.0; void SystemClock_Config(void); void MX_GPIO_Init(void); void MX_USART2_UART_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); // Use internal oscillator (MSI) MX_GPIO_Init(); USART1_UART_Init(); /* Infinite loop */ while (1) { /* Wait for the start corrector (0xFF) */ do { HAL_UART_Receive(&huart1, &data[0], 1, HAL_MAX_DELAY); } while (data[0] != 0xFF); /* Clear the data array before receiving new data */ //clearDataArray(); /* Start receiving 3 more bytes of data */ for (int i = 1; i < 4; i++) { HAL_UART_Receive(&huart1, &data[i], 1, HAL_MAX_DELAY); // Blocking receive } /* Process the sensor data */ processSensorData(); HAL_Delay(100); // Delay for sensor refresh (similar to Arduino delay) } /* Infinite loop */ while (1) { // Keep running if needed, or you can add additional functionality } } void processSensorData(void) { /* Validate the checksum */ int sum = (data[0] + data[1] + data[2]) & 0x00FF; // if (sum == data[3]) { /* Calculate the distance */ distance = (data[1] << + data[2]; // Combine the two bytes into a 16-bit value distance = distance / 10.0; // Convert distance to cm /* Blink the LED to indicate valid data */ if(distance<10){ HAL_Delay(500); } // } else { // Handle checksum error (optional) //} } /* GPIO Initialization Function */ void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* Enable GPIO clock for PA9 and PA10 (USART1 TX/RX pins) */ __HAL_RCC_GPIOA_CLK_ENABLE(); /* Configure PA9 (USART1 TX) */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; // AF7 for USART1 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Configure PA10 (USART1 RX) */ GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; // AF7 for USART1 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } void USART1_UART_Init(void) { /* Enable USART1 clock */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); // Enable clock for Port E __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_USART1_CLK_ENABLE(); /* Initialize USART1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 9600; 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(); // Handle initialization error } } /* System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; // Configure the main internal regulator output voltage if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) { Error_Handler(); } // Initializes the RCC Oscillators according to the specified parameters // Here we use the internal Multi-Speed Internal (MSI) oscillator RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; // MSI range 6 corresponds to 4 MHz RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; RCC_OscInitStruct.PLL.PLLM = 1; RCC_OscInitStruct.PLL.PLLN = 40; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; 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_PLLCLK; 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_4) != HAL_OK) { Error_Handler(); } // Enable MSI Auto-calibration HAL_RCCEx_EnableMSIPLLMode(); } /* Error Handler */ void Error_Handler(void) { __disable_irq(); while (1) { // Blink the LED to indicate an error HAL_Delay(500); } }
View more

 

 

10 REPLIES 10
roystm321
Associate III

Ok i will try it, thanks