<> Application of ultrasonic ranging module

<> Ultrasonic ranging module :

There are many types of ultrasonic ranging module , At present, the more commonly used are URM37 The default value of ultrasonic sensor is 232 Interface , Can be adjusted to TTL Interface ,URM05 The test distance of high power ultrasonic sensor can reach 10 rice , At present, the test distance is far away , In addition, there are several commonly used foreign models SRF Series of ultrasonic modules , At present, the accuracy of the ultrasonic module can reach 100% 1cm

<> working principle :

Ultrasonic ranging module is a product used to measure distance , By sending and receiving ultrasound , Using time difference and sound propagation speed , Calculate the distance from the module to the obstacle in front .

<> Specific use and code

1, Define pin
#include<reg52.h> #include<stdio.h> sbit Trig = P1^0; sbit Echo = P1^1;
2, Setting serial port baud rate
void init_115200() { SCON = 0x50; T2CON |= 0X30; TH1 = 0xFF; TL1 = 0xFD;
RCAP2H = 0XFF; RCAP2L = 0XFD; TR2 = 1; ES = 1; EA = 1; ET0=1; }
3, Set sending time
void Delay10us() { TMOD |= 0x1; TH0 = 0xFF; TL0 = 0xF6; TR0 = 1; while(!TF0);
TF0 = 0; } void CSB_Rstart() // Start the ultrasonic module , initialization { Trig=0; Trig=1; Delay10us();
Trig=0; }
4, Get time and distance
int gettime()// Acquisition time { unsigned int time = 0; time = TH0<<8 | TL0;
//TH0*256+TL0 return time; } float CSB_Getdis(unsigned int time) // Get the distance { float
distance; distance = (float)time * 0.0017; TH0=0; TL0=0;// Clear timer · return
distance; } void star() { TH0 = 0; TL0 = 0; TR0 = 1; } void end() { TR0 = 0; }
void CSB_GetOnce() // Get the distance of ultrasonic distance measurement module { CSB_Rstart(); while(!Echo);// When Echo Wait for zero
star(); while(Echo);// When Echo by 1 Count and wait end(); }
5, Print out the measured distance
int main() { unsigned int time = 0; float dis; char buf[24]={'\0'};
init_115200(); while(1) { CSB_GetOnce(); time = gettime(); dis =
CSB_Getdis(time); sprintf(buf,"dis=%fcm\r\n",dis); delay(); upt(buf); } return
0; }
Print function
void shuchu(char c) { SBUF = c; while(TI==0); TI = 0; } void upt(char *p) {
while(*p != '\0'){ shuchu(*p); p++; } }
Delay Functions
void delay() { int i; int j; for(i=0;i<100;i++) for(j = 0;j<2000;j++); }

Technology