cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 with UART protocol sensors (URM07 ultrasonic sensors)

ELim
Associate II

Question:

What function could be used/ written to receive the data (URM07)?

Currently, integrating an URM07 ultrasonic sensor on STM32f1xx, according to the datasheet the transmission, have tried the function on UART (HAL_UART_RxCpltCallback) to receive the return data using UART interrupt, however the receive output back from debugging statement (printf) from huart1, output shows 0x

instead of the reference output shown in the reference link

Transmit: 0x55 0xAA 0x11 0x00 0x03 0x13                  //[ 55 AA 11 00 03 13 ]

Receive:   0x55 0xAA 0x11 0x02 0x03 0x00 0xFF 0x14

Ref link: https://wiki.dfrobot.com/URM07-UART_Ultrasonic_Sensor_SKU__SEN0153

MCU configuration:

huart1 (activated with UART1 global interrupt)

huart2 (activated with UART2 global interrupt)

huart3 (activated with UART3 global interrupt)

RCC HSE activated

snippet for transmission and recieve

/*------------------------------------------- Transmit-----------------------------------------*/

#include "main.h"

#include "stm32f1xx_hal.h"

#include <stdio.h>

#include <stdlib.h>                                               

#include <math.h>                                                

#include <string.h>                                               

#include <stdbool.h>

//Variables

#define header_H   0x55 //Header

#define header_L   0xAA //Header

#define device_Addr 0x11 //Address

#define data_Length 0x00 //Data length

#define get_Dis_CMD 0x02 //Command: Read Distance

#define checksum   (header_H+header_L+device_Addr+data_Length+get_Dis_CMD)

unsigned char i=0;

unsigned int Distance=0;

unsigned int Distance1=0;

unsigned int Distance2=0;

unsigned char Rx_DATA[8];

unsigned char Rx_DATA1[8];

unsigned char Rx_DATA2[8];

unsigned char CMD[6]={ header_H,header_L,device_Addr,data_Length,get_Dis_CMD,checksum};

int main(void){

 HAL_Init();

 SystemClock_Config();

 MX_USART1_UART_Init();

 MX_USART2_UART_Init();

 MX_USART3_UART_Init();

while (1){

 for(i=0; i<6; i++)

 {

  while (!(huart2.Instance->SR & UART_FLAG_TXE));

  while (!(huart3.Instance->SR & UART_FLAG_TXE));

     huart2.Instance->DR = CMD[i];

     huart3.Instance->DR = CMD[i];

     //transmitting this way allows me to send the bytes to communicate to URM07

    // in the example

    // for(i=0;i<6;i++){

   //Serial1.write(CMD[i]);

 }

  HAL_UART_Receive_IT(&huart2, Rx_data1, 1);

  HAL_UART_Receive_IT(&huart3, Rx_data2, 1);

if(Transfer_cplt1 || Transfer_cplt2){

 Distance1=((Rx_DATA1[5]<<8)|Rx_DATA1[6]); //Read the distance value

 Distance2=((Rx_DATA2[5]<<8)|Rx_DATA2[6]); //Read the distance value

}

char array[32];

sprintf(array,"D1:%d D2:%d",Distance1,Distance2);

printf("%s\r\n", array);

// printf - debug statement is in huart1

/*PUTCHAR_PROTOTYPE

{

 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);

return ch;

}

*/

}

}

/*--------------------------------------------------------------------------------------------------------------*/

/*-----------------------------------------------------Receive----------------------------------------------*/

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

           if (huart->Instance == USART2) {

while (!(huart1.Instance->SR & UART_FLAG_RXNE));

RXDATA1[i++] = Rxdata1;

if( i == 8 ){ i = 0; Transfer_cplt1=1;}

HAL_UART_Receive_IT(&huart2, Rx_data1, 1);

}

           if (huart->Instance == USART3) {

while (!(huart1.Instance->SR & UART_FLAG_RXNE));

RXDATA2[i++] = Rxdata2;

if( i == 8){ i = 0; Transfer_cplt2=1;}

HAL_UART_Receive_IT(&huart3, Rx_data2, 1);

}

}

/*------------------------------------------------------------------------------------------------------------------*/

5 REPLIES 5

Having one global variable as the index for two independent buffers is probably not advisable.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Do you mean this index? that have to seperate to write into each UART channels?

 for(i=0; i<6; i++)

 {

...

 }

No, on the receiving side.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ELim
Associate II

yes it make sense, thanks, however from the advice it could be for both, not to mix the indexes

making it

while(1){

writechn1();//huart2

readchn1();

writechn2();//huart3

readchn2(); // however this is the main question, which is interrupting the progress of the integration( HAL_UART_RxCpltCallback a single function to callback, splitting it might cause redefinition, which pose back to the main question)

// transmit out function from huart1

}

void writechn1(){

           for(i=0; i<6; i++)

 {

  while (!(huart2.Instance->SR & UART_FLAG_TXE));

     huart2.Instance->DR = CMD[i];

 }

}

void writechn2(){

              for(y=0; y<6; y++)

 {

    while (!(huart3.Instance->SR & UART_FLAG_TXE));

     huart3.Instance->DR = CMD[y];

 }

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

           if (huart->Instance == USART2) {

while (!(huart2.Instance->SR & UART_FLAG_RXNE)); 

RXDATA1[i++] = Rxdata1; 

if( i < 8 ){ i = 0; Transfer_cplt1=1;} 

HAL_UART_Receive_IT(&huart2, Rx_data1, 1); 

}

           if (huart->Instance == USART3) {  

while (!(huart3.Instance->SR & UART_FLAG_RXNE));

RXDATA2[i++] = Rxdata2;

if( y < 8){ y = 0; Transfer_cplt2=1;}

HAL_UART_Receive_IT(&huart3, Rx_data2, 1);

}

}

in the reference code, have tested the output with no sensors on a MCU similar from the ref link, receive return data

12:46:36.921 -> 0cm // Distance

12:46:36.921 -> 0 //Rx_DATA[0]

12:46:36.921 -> 0 //[1]

12:46:36.921 -> 0 //[2]

12:46:36.921 -> 0 //[3]

12:46:36.921 -> 0 //[4]

12:46:36.921 -> 0 //[5]

12:46:36.921 -> 0 //[6]

12:46:36.921 -> 0 //[7]

when serial1 TX,RX connected (with no sensors (e.g huart2) STM32 MCU should give me this output

Serial.println(Rx_DATA[i]) of each indexes ---> STM32 output through the array should be in the same

12:47:58.666 -> 4608cm

12:47:58.666 -> 85 [0]

12:47:58.666 -> 170 [1]

12:47:58.666 -> 17 [2]

12:47:58.666 -> 0 [3]

12:47:58.666 -> 2 [4]

12:47:58.666 -> 18 [5]

12:47:58.666 -> 0 [6]

12:47:58.666 -> 0 [7]

ELim
Associate II

To add on, (seperating the channels )

Sensors functions can only transmit and receive in bytes ( for STM32 is unsigned char) (especially receive)

Therefore, is there any function to receieve data type unsigned char (bytes)

convert through the  

Distance1=((Rx_DATA1[5]<<8)|Rx_DATA1[6]); //conversion of unsigned char to unsigned int

void writechn1(){

 //unsigned char CMD[6]={ header_H,header_L,device_Addr,data_Length,get_Dis_CMD,checksum};

      HAL_UART_Transmit(&huart2, &CMD[0], sizeof(CMD[0]),10);

      HAL_UART_Transmit(&huart2, &CMD[1], sizeof(CMD[1]),10);

      HAL_UART_Transmit(&huart2, &CMD[2], sizeof(CMD[2]),10);

      HAL_UART_Transmit(&huart2, &CMD[3], sizeof(CMD[3]),10);

      HAL_UART_Transmit(&huart2, &CMD[4], sizeof(CMD[4]),10);

      HAL_UART_Transmit(&huart2, &CMD[3], sizeof(CMD[5]),10);

   

 }

void readchn1_2(){

//HAL_UART_RxCpltCallback

}

void writechn2(){

 //unsigned char CMD[6]={ header_H,header_L,device_Addr,data_Length,get_Dis_CMD,checksum};

      HAL_UART_Transmit(&huart3, &CMD[0], sizeof(CMD[0]),10);

      HAL_UART_Transmit(&huart3, &CMD[1], sizeof(CMD[1]),10);

      HAL_UART_Transmit(&huart3, &CMD[2], sizeof(CMD[2]),10);

      HAL_UART_Transmit(&huart3, &CMD[3], sizeof(CMD[3]),10);

      HAL_UART_Transmit(&huart3, &CMD[4], sizeof(CMD[4]),10);

      HAL_UART_Transmit(&huart3, &CMD[3], sizeof(CMD[5]),10);

 }