stm32F1 uses us-100 ultrasonic ranging

Publisher:幸福家庭Latest update time:2017-09-27 Source: eefocusKeywords:stm32F1  us-100 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

As always, use CubeMX for peripheral configuration.

Using the level mode requires two captures, and sending a 10us trigger pulse requires a delay. 103VBT6 does not have many timers, so the UART port is more convenient.


Hardware Configuration:

1) Set the UART baud rate to 9600

2) Enable NVIC interrupt


software:

I use US-100 to measure the consumption of pellets in the hopper. The project uses FreeRTOS system.

1)

Enable UART2 port monitoring in the main function


  1. HAL_UART_Receive_IT(&huart2, rx_data_sonic, 2);  


After triggering the module, it will return 2 bytes of data, and we will read 2 bytes directly here.


2)

In the Task, it is triggered every 10 seconds and a byte 0x55 is sent. The task runs every 20ms and uses a count.


  1. check_interval++;  

  2. if (check_interval >= 499) {  

  3.   HAL_UART_Transmit(&huart2, &command_sonic, 1, 1);  

  4.   check_interval = 0;  

  5. }  



3)

Do calculations in the interrupt callback function.

The hopper height is 50 cm, so the returned value is limited. For example, if the hopper cover is opened, the data of several meters measured will be thrown away. There is no need to consider the case where the hopper is opened.


  1. //Ultrasonic data  

  2. if (huart->Instance == USART2) {  

  3.   uint16_t distance = rx_data_sonic[0]*256 + rx_data_sonic[1];  

  4.     

  5.   if (Q_SonicDataHandle != NULL) {  

  6.     if (distance < 500) {  

  7.       osMessagePut(Q_SonicDataHandle, (uint32_t)distance, 0);  

  8.     }  

  9.   }  

  10.   HAL_UART_Receive_IT(&huart2, rx_data_sonic, 2);   

  11.     

  12. }  


At the end of the statement, you need to start the serial port interrupt monitoring again.


Keywords:stm32F1  us-100 Reference address:stm32F1 uses us-100 ultrasonic ranging

Previous article:STM32 timer single pulse output
Next article:STM32 port mode configuration

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号