The ultrasonic sensor uses HC-SR04, which has four pins VCC, GND, Trig, and Echo. The method of use is: set Trig to high level and keep it at least 10us, the sensor will send out 8 pulses of sound waves. For the entire time required for the sound wave to generate an echo, Echo is high. By measuring the high level time, the elapsed time can be known, and the distance to the obstacle can be calculated.
To measure the time that Echo is high level is to use the STM32F401RETX general timer to capture the input. The measured high level time passes the formula: distance = Thigh *340/2 (m), where Thigh is in seconds (s), 340 Meters per second (m/s) is the speed of sound.
learn more about STM32 related products, please click here.
learn more about STM32 technical information, please click here.
According to the STM32F401RETX data manual, the board used has four general-purpose timers TM2-TM5, and TIM3 timer (16 bits) is used in this design. STM32F401RETX has multiple functional modes such as input capture mode, output comparison mode, single pulse mode, etc. Among them, the input capture mode can be set to capture rising or falling edges to capture the input signal. When a capture event occurs, the current count value can be obtained. If the interrupt is enabled, the interrupt can be triggered. This design uses the input capture mode to capture and time the Echo signal.
According to the HC-SR04 data manual, the ultrasonic sensor has a range of 2cm-4m, and the range of Thigh is roughly 0.0235~0.0002857s when substituted into the distance formula, that is, the range of 1/Thigh is between 42.5-8500Hz, so choose the counter A clock of 1MHz can meet the requirements.
1.Use STM32CubeMX to configure the corresponding pins to generate project code.
1) Configuration pin
Set TIM3 as the internal clock input on the left end and enable channel 1. You can see that PA6 on the right turns green. Look for the external pin number D12 corresponding to PA6 in the circuit diagram. This pin will be connected to the Echo pin of the ultrasonic sensor. In order to facilitate the device connection, the external pin D11 adjacent to D12 is selected as the output to connect to the Trig, so the corresponding pin PA7 of D11 is configured as a GPIO output. The pin configuration diagram and circuit diagram are shown below.
2) Configure timer TIM3 parameters
The STM32F401RETX STMicroelectronics board does not have an external crystal, so the clock source is HIS, and the timer clock frequency is 16MHz. As shown below.
The timer TIM3 parameter configuration is shown in the figure below, the prescaler coefficient is 15, that is, the final frequency of the TIM3 timer is 16 MHz/(15+1)=1MHz; the maximum count value is 0xffff (65535), and the interrupt is enabled in the NVIC .
3) Generate project code
2. Open the generated project code with Keil and add processing logic code
1) STM32F401RETX First define the number of capture capture_cnt, capture count value capture_value1, capture_value2, and high level time hightime and distance distance.
2) Check the TIM function in the HAL library, and add the following code to the while of the main function. Perform different operations according to the number of captures. When the number of STM32F401RETX captures is 0, enable the input capture of the TIM3 interrupt mode and set the trigger capture mode to rising edge trigger; when the capture is sent, enter the capture interrupt function (the interrupt function is automatically generated, and the trigger callback is called Function, it can be processed directly in the callback function), get the current count value in the capture callback function and assign it to capture_value1, then change the capture mode to falling edge trigger; when the capture occurs again, assign the current count value to capture_value2, and then stop Input capture. In the main function, the time can be obtained according to the direct difference between capture_value1 and capture_value2, and then the distance can be obtained by substituting the distance formula.
3) Open the serial port to receive in the while loop of the main function, and when it is judged that the received data is 00, perform distance measurement: Give the Trig pin of the ultrasonic module a high level.
Manufacturer: Texas Instruments
IC DGTL MEDIA PROCESSOR 337NFBGA
Product Categories: DSP
Lifecycle:
RoHS:
Manufacturer: Texas Instruments
IC DSP FIXED-POINT 688FCBGA
Product Categories: DSP
Lifecycle:
RoHS:
Manufacturer: Texas Instruments
IC DSP MILTICORE 561FCBGA
Product Categories: DSP
Lifecycle:
RoHS:
Manufacturer: Texas Instruments
IC DSP FIX/FLOAT POINT 361NFBGA
Product Categories: DSP
Lifecycle:
RoHS:
Looking forward to your comment
Comment