2024-10-16 04:36 AM - edited 2024-10-16 04:36 AM
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);
}
}
Solved! Go to Solution.
2024-10-16 05:42 AM
Thank Everyone for you fast response,
I Solved it, i do not understand why..., but the "HAL_MAX_DELAY" blocked the receiveing data,
So i modify it to 300ms and now i get response from the ultrasonic,
There is a tiny glitch, that seldom. the sensor not detect the correct value.
Is there any settings in the code i sent, that i need to pay attention?
Thanks!
2024-10-16 04:53 AM
Post not terribly enlightening..
How's this all wired up, show a schematic or wiring diagram.
Describe expected behaviour. Show salient portion of datasheet. Link to data sheet.
What do you actually observe?
Any data? The wrong data? Looked at signals with a scope or logic analyzer?
Any helpful observations you can share?
"Not Working" is an unhelpful diagnosis / symptom. What's happening vs what's not..
2024-10-16 05:24 AM
As @Tesla DeLorean said, your post is not very clear.
Are you saying that you have this working on an ST Neucleo-F441 board, but not on your own custom board, with an STM32L443VCT6?
If so, you'll need to
2024-10-16 05:42 AM
Thank Everyone for you fast response,
I Solved it, i do not understand why..., but the "HAL_MAX_DELAY" blocked the receiveing data,
So i modify it to 300ms and now i get response from the ultrasonic,
There is a tiny glitch, that seldom. the sensor not detect the correct value.
Is there any settings in the code i sent, that i need to pay attention?
Thanks!
2024-10-16 06:48 AM
@roystm321 wrote:the "HAL_MAX_DELAY" blocked the receiveing data,
Hmmm..., I've seen that somewhere before but can't quite remember where or what the issue was.
I think the maximum actual wait is HAL_MAX_DELAY - 1
Anyhow, as it's solved your problem, please mark the solution:
https://community.st.com/t5/community-guidelines/help-others-to-solve-their-issues/ta-p/575256
2024-10-16 06:52 AM
It looks like that sometime not all the data in the array comes:
HAL_UART_Receive(&huart1, &data[i], 1, HAL_MAX_DELAY);
and then this prevent from the code continue to run..
2024-10-16 07:03 AM - edited 2024-10-16 07:04 AM
@Andrew Neil wrote:I've seen that somewhere before but can't quite remember where or what the issue was
Maybe this (from stm32f0xx_hal_uart.c, but probably widespread):
/**
* @brief This function handles UART Communication Timeout. It waits
* until a flag is no longer in the specified status.
* @param huart UART handle.
* @param Flag Specifies the UART flag to check
* @param Status The actual Flag status (SET or RESET)
* @param Tickstart Tick start value
* @param Timeout Timeout duration
* @retval HAL status
*/
HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
uint32_t Tickstart, uint32_t Timeout)
{
/* Wait until flag is set */
while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
{
/* Check for the Timeout */
if (Timeout != HAL_MAX_DELAY)
{
if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
So, if Timeout == HAL_MAX_DELAY, the code does not do any timeout at all.
@STTwo-32 At the very least, the documentation needs to be updated.
Better would be change the name to actually reflect what it does; eg. instead of
#define HAL_MAX_DELAY 0xFFFFFFFFU
Have something like:
#define HAL_NO_DELAY 0xFFFFFFFFU
#define HAL_MAX_DELAY (HAL_NO_DELAY-1)
2024-10-16 07:03 AM
So returns in 300 ms with an error that's being ignored?
Is data actually being communicated?
2024-10-16 07:06 AM - edited 2024-10-16 07:08 AM
Of course,
I received the data,
For example it fail just 1 in 10 times. but i can know when it fail. i double check each value in the data
2024-10-16 07:14 AM
@roystm321 wrote:It looks like that sometime not all the data in the array comes:
HAL_UART_Receive(&huart1, &data[i], 1, HAL_MAX_DELAY);and then this prevent from the code continue to run..
As noted earlier, that HAL_MAX_DELAY may be giving no delay/timeout at all.
So try HAL_MAX_DELAY - 1
And don't ignore the return value from HAL_UART_Receive()- that's what tells you when it's failed!