US-100 ultrasonic ranging design based on STM32F407

This paper introduces the programming and design of a digital display ultrasonic distance measurement module. Now it introduces the design and programming of this module.

1. Brief description of US-100 ultrasonic

Here's a simple schematic:

The principle of ultrasonic ranging can be seen from the pictures. A simple description is that one end emits ultrasonic wave and the other end receives the ultrasonic wave reflected by contacting the object, and then a relatively accurate data can be obtained by simple arithmetic operation.
In the general ultrasonic ranging module, this object has certain requirements, such as requiring the reflection object to be horizontal, not oblique or uneven, or it will affect the accuracy of the data.

2. US-100 ultrasonic pin wiring

Directly above:

As shown in the figure:
1 connected to power supply;
No. 2 is connected to tx transmitter in usart mode and an IO port in level trigger mode;
Pin 3 is connected to rx receiver in usaer mode and an IO port in level trigger mode;
No. 4 and No. 5 are grounded. When I trigger the level test, only one ground can work;

You may have a question about how to choose the working mode?
If you look at this picture alone, you really don't know how to set it. Because this module is external, it is shown as follows:

Ha ha, as shown in the figure above, removing the rear jumper cap is the trigger level mode, and inserting the jumper cap is the usart working mode.

3, Programming

After reading the first two sections, have you got some understanding and ideas about how to program? Is it a very simple module.
Because I didn't use the function of temperature detection in use, I simply used the level triggering mode, i.e. ordinary IO port configuration, one push-pull output and one input.
Here's a thing that has nothing to do with the program. If you know about the amount of ultrasound, there are two ultrasonic modules on a certain treasure, one is HC-SR04, the other is US-100, and the price difference is quite big; HC-SR04 is more than ten yuan better than US-100.
Why don't I choose HC-SR04 here?
Of course, HC-SR04 was chosen at the beginning, but it was found that the distance measured by HC-SR04 sometimes drifted when it was used later, that is, it suddenly changed a lot. After checking and debugging later, it was found that it could not be solved. The checking and debugging here did not deal with the software program and hardware, because it was modular. Of course, I can't solve it. Maybe my strength hasn't reached that point, hahaha.
We decided to adopt another one when we couldn't solve it, that is, US-100 model. After using this model, we found that the gap was so big So I would like to introduce here that the US-100 does not say HC-SR04, and the ultrasonic selected is also the reason why this model is more expensive

Hahaha, there's a lot of nonsense here. Here's the code and comments:
Ultrasonic initialization:

Function Description: US-100 ultrasonic pin initialization program
 Parameter: NONE
 Return value: NONE
 Pin: Trig----PA4
void Us100_Init(void)
       GPIO_InitTypeDef GPIO_InitStructure;
    //Hourly clock
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);  //Enable GPIOA clock
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;               //Trig
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;           //Output mode.
    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;          //Push pull output
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;        //No top, drop-down mode
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;               //Echo
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;            //Input mode.
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;          //No top, drop-down mode

Here we use a timer to count the time from the time when the ultrasound is sent to the time when it is received.

Timer initialization:

Function Description: Timer 3 initializer
 Parameter: NONE
 Return value: NONE
void Tim3_int(void)
    RCC->APB1ENR|=1<<1; //TIM3 clock enable    
    TIM3->ARR=49999;     //Set the counter automatic reassembly value 50ms, interrupt once, 1us count one number
    TIM3->PSC=83;       //Prescaler   
    TIM3->DIER|=1<<0;    //Allow update interrupt

After the initialization is completed, the application starts.


Function Description: obtain the distance from the ultrasonic side
 Parameter: NONE
 Return value: distance
uint16_t Get_DistanceData(void)
     uint16_t dis;
     uint16_t Distance;
     TIM3 -> CR1 |= (1<<0);
     delay_us(14);                 //The delay here can be 12us-20us, which can pass the test. How much depends on your project requirements
     while(!Echo);                 //Wait for echo signal to go high
     TIM3 -> CNT = 0;              //Let the value of cnt change to 0 and start the calculation
     while(Echo);                  //Wait for echo signal to go low
     TIM3 -> CR1 &=~ (1<<0);       //Stop counting
     dis = TIM3 -> CNT;            //Get intermediate time
     Distance = dis/58;            //Calculate the distance
     return Distance;              //Return distance

Header file:

#ifndef US100_H
#define US100_H

#include "stm32f4xx.h"

#define Trig(x)  (x? GPIO_WriteBit(GPIOA,GPIO_Pin_4,Bit_SET):GPIO_WriteBit(GPIOA,GPIO_Pin_4,Bit_RESET))
#define Echo     (GPIOA -> IDR & (1<<5))

void Us100_Init(void);
void Tim3_int(void);
uint16_t Get_DistanceData(void);


This has been introduced. If you have any questions, you can comment in the comment area. If you think it's good, you can like it!

Tags: Programming

Posted on Mon, 04 May 2020 02:12:49 -0400 by andymoo