Ultrasonic Sensor Distance Measurement Function in C
This is a function in a program written in C language that is used to measure distance using an ultrasonic sensor. The function is named 'Get_distance_Value' and it takes no arguments.
Inside the function, there are several variables defined and initialized, including a static variable 'HC_Time' which is used to store the time measured by the sensor, a variable 'i' used for a loop counter, and a static variable 'old_dat' which is used to store the last distance measurement.
The function begins by checking if the variable 'HC_start' is set to 1. If it is, this indicates that a distance measurement should be taken. The variable 'HC_start' is then set to 0 to indicate that the measurement has started.
The function then sends a high pulse to the TRIG_1 pin of the ultrasonic sensor, waits for a short period of time, and then sets the pin back to low to start the measurement.
The function then waits for the ECHO_1 pin to go high, indicating that the sensor has detected an object. It then initializes the timer registers TH1 and TL1 to 0, starts the timer, and waits for the ECHO_1 pin to go low again.
Once the ECHO_1 pin goes low, the function stops the timer and calculates the time elapsed using the values stored in TH1 and TL1. This time is then converted to distance using a formula that takes into account the speed of sound and the time elapsed.
The function then adds a small offset to the distance measurement and stores the result in the variable 'Dist'. It also updates the variable 'old_dat' with the new measurement.
Finally, the function resets the timer registers and exits.
原文地址: https://www.cveoy.top/t/topic/naCu 著作权归作者所有。请勿转载和采集!