tags: Using ultrasonic ranging module Ultrasonic wave
Ultrasonic Ranging module performs a variety of types, now more commonly used URM37 ultrasonic sensor 232 is by default an interface, the interface can be adjusted to TTL, URM05 high-power ultrasonic transducer can be 10 meters from the test, the test is considered to be the more distant a the models, in addition to the more common abroad several SRF series of ultrasonic module, the module current ultrasonic energy to the accuracy of 1cm
Ultrasonic Ranging module is used to measure the distance of a product, and on receiving an ultrasonic wave transmitted by using the time difference and the speed of sound, the module calculates the distance to the front obstacle.
1. Definitions pin
#include<reg52.h>
#include<stdio.h>
sbit Trig = P1^0;
sbit Echo = P1^1;
2, setting the 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, the transmission time set
void Delay10us()
{
TMOD |= 0x1;
TH0 = 0xFF;
TL0 = 0xF6;
TR0 = 1;
while(!TF0);
TF0 = 0;
}
void CSB_Rstart () // start ultrasonic module initialization
{
Trig=0;
Trig=1;
Delay10us();
Trig=0;
}
4, acquisition time and distance
int gettime () // get 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 the timer ·
return distance;
}
void star()
{
TH0 = 0;
TL0 = 0;
TR0 = 1;
}
void end()
{
TR0 = 0;
}
void CSB_GetOnce () // Get a distance of ultrasonic ranging module
{
CSB_Rstart();
while (Echo!); // wait zero when Echo
star();
while (Echo); // if the count is 1 and Echo wait
end();
}
5, the measured distance printed
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 function
void delay()
{
int i;
int j;
for(i=0;i<100;i++)
for(j = 0;j<2000;j++);
}
I hope that God will help me see why only around 17cm is around 17cm. Is it overflow of the timer?...
1. This article is a record on the use of ultrasonic module ranging. The ultrasonic ranging module (HC-SR04) detection angle used in this time is <5 ° wide voltage between 3.3 to 5V, and the ma...
1. Basic parameters Operating Voltage DC5V Working current 15mA working frequency 40Khz Farther range 4m Recent range 2cm Measurement angle 15° Input trigger angle 10us TTL pulse Output echo signa...
HC-SR04 principle Focus 5v power supply, Tri pin needs at least 10us high pulse to trigger, Echo returns the time from ultrasonic emission to return How to calculate the distance? Look at the third pi...
1, HC-SR04 module introduction HC-SR04 module advantage This module is stable, the measure distance is accurate, the module is high, and the blind zone is small. Product application area: 1, robot avo...
Trig Trigger control signal input, Echo echo signal output. The write program sends a low-level low-time pulse to the Trig to trigger the ranging; the pulseIn function waits for the pin to become HIGH...
Here I will briefly ultrasonic ranging module SR04 ultrasonic sensor: It uses ultrasonic distance sensor detection characteristics, with which two ultrasonic probes, are used in transmitting and recei...
Original link: https: //blog.csdn.net/m0_37655357/article/details/72934643 First, let's look at the basic functions and principles of this module. HC-SR04 ultrasonic ranging module may provide about 2...
US-015 Ultrasonic Ranging Module Ultrasonic Sensor US-020 Upgrade Version Send Full Set of Information Information download address: http://pan.baidu.com/s/1c0AfkIG US-015 ultrasonic ranging module 1 ...