Skip to main content
ELim
Associate
October 2, 2019
Question

STM32 with UART protocol sensors (URM07 ultrasonic sensors)

  • October 2, 2019
  • 1 reply
  • 2108 views

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);

}

}

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

This topic has been closed for replies.

1 reply

Tesla DeLorean
Guru
October 2, 2019

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

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
ELim
ELimAuthor
Associate
October 2, 2019

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

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

 {

...

 }

Tesla DeLorean
Guru
October 2, 2019

No, on the receiving side.​

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