当前位置:网站首页>51 single chip microcomputer learning notes 7 -- Ultrasonic Ranging
51 single chip microcomputer learning notes 7 -- Ultrasonic Ranging
2022-06-29 05:16:00 【GenCoder】
Ultrasonic ranging practice
1. ultrasonic
Bats and some marine animals can use high-frequency sound for echolocation or information exchange . They can send out ultrasonic waves from the throat through the mouth or nasal cavity , Use the folded sound wave to orient , And determine the position of nearby objects 、 Size and whether it is moving . Ultrasound is a frequency higher than 20000 Hertz sound wave , It has good directionality , Strong penetration , It is easy to obtain more concentrated sound energy , Spread far in the water , It can be used for ranging 、 velocity measurement 、 cleaning 、 welding 、 Gravel 、 Sterilization, disinfection, etc . In medicine 、 military 、 Industry 、 There are many applications in agriculture . Ultrasound is named because its lower frequency limit is greater than the upper limit of human hearing . Scientists call the number of vibrations per second the frequency of sound , It's in Hertz (Hz). The frequency of sound waves that we human ears can hear is 20Hz-20000Hz. therefore , We put the frequency higher than 20000 Hertz sound waves are called “ ultrasonic ”. The ultrasonic frequency commonly used in medical diagnosis is 1 Megahertz -30 Megahertz . Theoretical research shows that , With the same amplitude , The vibration energy of a body is proportional to the vibration frequency , When ultrasonic wave propagates in medium , The frequency of medium particle vibration is very high , So the energy is very large . In the dry winter in northern China , If the ultrasonic wave is introduced into the water tank , The violent vibration will break the water in the tank into many small droplets , Then use a small fan to blow the fog drops into the room , Can increase indoor air humidity , This is the principle of ultrasonic humidifier . Such as pharyngitis 、 Tracheitis and other diseases , It is difficult to use the blood flow to make the medicine reach the diseased area , Using the principle of humidifier , Atomize the liquid medicine , Let the patient inhale , It can improve the curative effect . Using the huge energy of ultrasonic wave can also make the stones in human body vibrate violently and break , So as to alleviate the pain , To achieve the goal of healing . Ultrasound is widely used in medicine , It can sterilize the articles .
2. Ultrasonic module
2.1 Module parameters
The commonly used ultrasonic module in ultrasonic experiment is 4 Needlelike SR04, Also have 5 Ultrasonic module of needle 
Main technical parameters
1: Use voltage :DC—5V
2: Quiescent current : Less than 2mA
3: Level output : high 5V
4: Level output : At the end of 0V
5: Sensing angle : No more than 15 degree
6: Detection distance :2cm - 450cm
7: High precision Can be up to 0.2cm
8: Wiring way ( Above, from left to right ):VCC、Trig( Control terminal )、Echo( The receiver )、 GND
2.2 Module schematic diagram

2.3 Ultrasonic potential difference ranging
The interface between ultrasonic sensor and single chip microcomputer system constitutes the hardware system of distance detection , Under the control of the system software , A trigger pulse sent by the single chip microcomputer to the potential difference ultrasonic sensor , The potential difference ultrasonic sensor will generate a short pulse after being triggered by this pulse 40 kHz Pulse electrical signal , this 40 kHz After the pulse electrical signal is processed by the excitation transducer , To convert energy into mechanical vibration , Its vibration frequency is about 20 kHz above , Thus, ultrasonic wave is formed , The signal is tapered " Radiant port " Place the ultrasonic signal in the air at about... Per second 1 130 At a speed of feet .
When the emitted ultrasonic signal encounters an obstacle , Immediately reflected back . The receiver receives the reflected ultrasonic signal , Through its internal transformation , Turn ultrasonic waves into weak electrical oscillations , And amplify the signal , The required pulse signal can be obtained , This pulse signal is returned to the single chip microcomputer , Indicates that the echo is detected , This pulse width corresponds to the time required for the burst echo to return to the sensor , The timing is shown in the figure .
2.4 Module features
HC-SR04 The ultrasonic ranging module has the advantages of accurate ranging distance , Energy and harmony SRF05,SRF02 And so on , Measuring distance 2cm ~ 450cm ( Actually measured :6cm ~ 330cm).
working principle
1) use IO Trigger ranging , Give at least 10us High level signal .
2) Automatic module sending 8 individual 40KHz The square wave , Automatically detects if a signal is returned .
3) Return with signal , Through the module IO Output high level , The duration of high level is the time from emission to return of ultrasonic wave . Just provide one 10uS The above pulse trigger signal , The module will emit 8 individual 40kHz Cycle level and detect echo . Once the echo signal is detected, the echo signal is output . The pulse width of the echo signal is proportional to the distance measured . Thus, the distance can be calculated by the time interval between the transmitted signal and the received echo signal .
The formula :
or uS/148= Inch
or distance = High level time * The speed of sound (340M/S) /2
The recommended measurement period is 60ms above , To prevent the influence of the transmitted signal on the echo signal .
1、 This module is not suitable for live connection , To make a live connection , Let's start with the module GND Connect the end first , Otherwise, the normal operation of the module will be affected .
2、 When ranging , The area of the object to be measured is not less than 05 Square meters and the plane shall be flat as far as possible , Otherwise, the measurement results will be affected .
3. Ultrasonic ranging programming
According to the ultrasonic sequence diagram , send out 10us Trigger trigger signal , And record the time interval of the echo signal
unsigned int RunOnce()
{
unsigned int time;
//10us High level sends trigger signal
Trig = 0;
Trig = 1;
Delay10us();
Trig = 0;
// Wait for the high level signal to be received
while(!Echo);
//T0 clear 0 Recount ( High level duration )
TH0 = 0;
TL0 = 0;
TR0 = 1;
// Wait for the end of high-level signal reception
while(Echo);
// close T0 Count
TR0 = 0;
// The assignment time is high , Company us
time = TH0*256 + TL0; // TH0<<8 | TL0
TH0 = 0;
TL0 = 0;
return time;
}
10us Time delay timer for 0 To complete ( Timer 0 For configuration details, see Timer interrupt )
// Time delay 10us
void Delay10us()
{
TMOD |= 0x01; //16 Bit timer / Counter ,TH0、TH1 All use
TH0 = 0xFF;
TL0 = 0xF6;
TR0 = 1; //TR0 by 1 Time allowed T0 Start counting
while(!TF0); // When T0 Exit on overflow while
TF0 = 0; //TF0 Set up 0
}
Convert the time interval of ultrasonic echo signal into distance
float GetDistance(unsigned int time)
{
float distance;
distance = (float)time * 0.017; //cm
return distance;
}
stay main Function to make function calls , complete demo as follows
#include<reg52.h>
#include<intrins.h>
sbit Trig = P0^0;
sbit Echo = P0^1;
// Millisecond delay function
void delay_ms(unsigned int t)
{
unsigned char i, j, z;
for(z = t;z>0;z--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
// Time delay 10us
void Delay10us()
{
TMOD |= 0x01; //16 Bit timer / Counter ,TH0、TH1 All use
TH0 = 0xFF;
TL0 = 0xF6;
TR0 = 1; //TR0 by 1 Time allowed T0 Start counting
while(!TF0); // When T0 Exit on overflow while
TF0 = 0; //TF0 Set up 0
}
float GetDistance(unsigned int time)
{
float distance;
distance = (float)time * 0.017; //cm
return distance;
}
unsigned int RunOnce()
{
unsigned int time;
//10us High level sends trigger signal
Trig = 0;
Trig = 1;
Delay10us();
Trig = 0;
// Wait for the high level signal to be received
while(!Echo);
//T0 clear 0 Recount ( High level duration )
TH0 = 0;
TL0 = 0;
TR0 = 1;
// Wait for the end of high-level signal reception
while(Echo);
// close T0 Count
TR0 = 0;
// The assignment time is high , Company us
time = TH0*256 + TL0; // TH0<<8 | TL0
TH0 = 0;
TL0 = 0;
return time;
}
void main()
{
unsigned int time = 0;
float distance;
while(1)
{
time = RunOnce(); // When calculating ultrasonic ranging Time when the sensor receives high level
distance = GetDistance(time);
}
}
3.1 ultrasonic + Running water lamp
Use the running water lamp to light up a number “ Show ” Ultrasonic distance
#include<reg52.h>
#include<intrins.h>
sbit Trig = P0^0;
sbit Echo = P0^1;
// Millisecond delay function
void delay_ms(unsigned int t)
{
unsigned char i, j, z;
for(z = t;z>0;z--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
// Time delay 10us
void Delay10us()
{
TMOD |= 0x01; //16 Bit timer / Counter ,TH0、TH1 All use
TH0 = 0xFF;
TL0 = 0xF6;
TR0 = 1; //TR0 by 1 Time allowed T0 Start counting
while(!TF0); // When T0 Exit on overflow while
TF0 = 0; //TF0 Set up 0
}
float GetDistance(unsigned int time)
{
float distance;
distance = (float)time * 0.017;
return distance;
}
unsigned int RunOnce()
{
unsigned int time;
//10us High level sends trigger signal
Trig = 0;
Trig = 1;
Delay10us();
Trig = 0;
// Wait for the high level signal to be received
while(!Echo);
//T0 clear 0 Recount ( High level duration )
TH0 = 0;
TL0 = 0;
TR0 = 1;
// Wait for the end of high-level signal reception
while(Echo);
// close T0 Count
TR0 = 0;
// The assignment time is high , Company us
time = TH0*256 + TL0; // TH0<<8 | TL0
TH0 = 0;
TL0 = 0;
return time;
}
void RunLED(unsigned int LEDnum)
{
unsigned int RunNum = LEDnum;
switch(RunNum){
case 0:
P1 = 0x00;
delay_ms(20);
P1 = 0xff;
delay_ms(20);
break;
case 1: P1 = 0xfe;
break;
case 2: P1 = 0xfc;
break;
case 3: P1 = 0xf8;
break;
case 4: P1 = 0xf0;
break;
case 5: P1 = 0xe0;
break;
case 6: P1 = 0xc0;
break;
case 7: P1 = 0x80;
break;
case 8: P1 = 0x00;
break;
default:
P1 = 0x00;
break;
}
}
void main()
{
unsigned int time = 0;
float distance;
while(1)
{
time = RunOnce();
distance = GetDistance(time);
RunLED((int)(distance/10));
}
}
3.2 ultrasonic + Buzzer
Make the buzzer call out different frequencies according to different ultrasonic intervals
#include<reg52.h>
#include<intrins.h>
#define ON 0
#define OFF 1
sbit Trig = P0^0;
sbit Echo = P0^1;
// Millisecond delay function
void delay_ms(unsigned int t)
{
unsigned char i, j, z;
for(z = t;z>0;z--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
// Time delay 10us
void Delay10us()
{
TMOD |= 0x01; //16 Bit timer / Counter ,TH0、TH1 All use
TH0 = 0xFF;
TL0 = 0xF6;
TR0 = 1; //TR0 by 1 Time allowed T0 Start counting
while(!TF0); // When T0 Exit on overflow while
TF0 = 0; //TF0 Set up 0
}
float GetDistance(unsigned int time)
{
float distance;
distance = (float)time * 0.017;
return distance;
}
unsigned int RunOnce()
{
unsigned int time;
//10us High level sends trigger signal
Trig = 0;
Trig = 1;
Delay10us();
Trig = 0;
// Wait for the high level signal to be received
while(!Echo);
//T0 clear 0 Recount ( High level duration )
TH0 = 0;
TL0 = 0;
TR0 = 1;
// Wait for the end of high-level signal reception
while(Echo);
// close T0 Count
TR0 = 0;
// The assignment time is high , Company us
time = TH0*256 + TL0; // TH0<<8 | TL0
TH0 = 0;
TL0 = 0;
return time;
}
void main()
{
unsigned int time = 0;
float distance;
while(1)
{
time = RunOnce();
distance = GetDistance(time);
if(distance <= 10.0){
BUZZER = ON;
delay_ms(50);
BUZZER = OFF;
delay_ms(50);
}
else if((10.0 < distance) && (distance <= 20.0)){
BUZZER = ON;
delay_ms(100);
BUZZER = OFF;
delay_ms(100);
}
else if((20.0 < distance) && (distance <= 50.0)){
BUZZER = ON;
delay_ms(160);
BUZZER = OFF;
delay_ms(160);
}
}
}
边栏推荐
- The translation of those exquisite lines in the eighth season of the big bang
- GBASE 8s must be a DBSA、路径更改导致无法启动的解决方法
- D Author: import C programming in D
- HTTP Caching Protocol practice
- 2022 community group buying industry research industry development planning prospect investment market analysis report (the attachment is the online disk link, and the report is continuously updated)
- Real time waveform calculation function of Waveform Recorder mr6000
- Robot reinforcement learning -- first person vs third person
- Distributed transaction Seata
- [code Capriccio - dynamic planning] longest common subsequence
- real time AI based system questionaires
猜你喜欢

网传广东一名学生3次考上北大,3年共赚200万元奖金

Cloud native annual technology inventory is released! Ride the wind and waves at the right time

In 2022, I haven't found a job yet. I have been unemployed for more than one year. What is the "old tester" for eight years?

如何用万用表测试电子部件

Technical specifications of Tektronix tds3054b oscilloscope

5000+ word interpretation | Product Manager: how to do a good job in component selection?

笔记本访问台式机的共享磁盘

How to choose congestion model and anemia model

Software architecture experiment summary

Kubernetes backup disaster recovery service product experience tutorial
随机推荐
Easy to get started naturallanguageprocessing series topic 7 text classification based on fasttext
Research Report on the overall scale, major manufacturers, major regions, products and applications of electric hydrofoil surfboards in the global market in 2022
JVM memory tuning method
【IoT】公众号“简一商业”更名为“产品人卫朋”说明
HTTP Caching Protocol practice
Tcapulusdb Jun · industry news collection (III)
Tcapulusdb Jun · industry news collection (V)
Analysis report on the investment market of the development planning prospect of the recommended rare earth industry research industry in 2022 (the attachment is a link to the online disk, and the rep
机器人强化学习——第一人称 VS 第三人称
Open source demo| you draw and I guess -- make your life more interesting
2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up
The subnet of the pool cannot be overlapped with that of other pools.
docker 创建的 mysql8 怎么改密码
Research Report on the overall scale, major manufacturers, major regions, products and applications of high temperature film capacitors in the global market in 2022
2022 recommended quantum industry research industry development planning prospect investment market analysis report (the attachment is a link to the online disk, and the report is continuously updated
D Author: import C programming in D
Mediator pattern
data management plan
The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
I haven't encountered these three problems. I'm sorry to say that I used redis