Ultrasonic distance measurement with 51 single-chip microcomputer [comprehensive design + defense report + source program + flow chart + demonstration]

1. Design goals

    Ultrasonic distance measurement is realized by using 51 single-chip microcomputer and ultrasonic distance measurement module.

2. Main functions

    Ultrasonic ranging.

3. Hardware part

    51 single-chip microcomputer, ultrasonic ranging module, wire, dynamic digital tube, 74HC245 chip, 74HC138 chip.

figure 1                      

5658471146b44da18b9a665fd9b096e9.png

figure 2

 

09c6004e23c24abdb4b8134c4b0b3e43.png

image 3

 

29d6a054945241fab5a32c0af770efad.png

Figure 4

71d3a20d22454c3596fc21f7a3e2a35a.png

    Figure 1 is the internal circuit diagram of the ultrasonic ranging module.

    Figure 2 is a timing logic diagram of outputting and receiving sound waves of ultrasonic ranging.

    Figure 3 and Figure 4 are the control of the dynamic digital tube. Use the P0_0, P0_1, and P0_2 terminals of the 51 single-chip microcomputer to connect the A, B, and C pins of the 74HC138 chip to control which digital tube is lit.

 

4. Program block diagram

ba980ce9e74f459e8bb3ff52ed4364ad.png

 

5. Code Description

    The code consists of a delay function, a timer function, a distance calculation function, a time measurement function, a digital tube control function, a digital tube display result function, and a main function

    The delay function cooperates with the nixie tube control function to be responsible for the hardware control of the dynamic nixie tube display.

    The timer function is used for timing when ranging.

    The time measurement function calculates the time from sending out to receiving ultrasonic waves.

    The calculated distance function is used to calculate the distance from the measured time.

    The digital tube display result function is used to display the measured results.

    The nixie tube control function is used to control the number of the dynamic nixie tube to light up and the displayed number.

1. Main function

    The main function calls the function, and first calls the time measurement function to obtain the time from sending out to receiving the ultrasonic wave. Then call the calculate distance function to convert time into distance (unit: centimeter). Finally, call the digital tube to display the result function to display the measurement result.

/******************************************主函数***************************************/
void main()
{
 unsigned int time = 0;
 float distance;

 while(1)
 {  
  time = RunOnce();//传感器接收到高电平的时间
  distance = GetDistance(time);
  xianshi(distance);
 }
}

 

2. Sub-function 1

    Nixie tube control function. Use the switch() function to define the signal required to light up each dynamic digital tube in advance according to the table in the hardware. Switch() can collect the value of the input dynamic digital tube function, and the first one is the number of the dynamic digital tube that is lit. In addition, define a digital array, and the content of the array is the output value of the P3 terminal required when a single dynamic digital tube displays 0-9, that is, all off.

unsigned char shuzi[]={0x3f/*0*/,0x06/*1*/,0x5b/*2*/,0x4f/*3*/,0x66/*4*/,0x6d/*5*/,0x7d/*6*/,0x07/*7*/,0x7f/*8*/,0x6f/*9*/};//显示的数字(num)的数组

void shumaguan(unsigned char wei,num)//第几位(wei)显示
{
 switch(wei)
 {
  case 1:P0_2=0;P0_1=0;P0_0=0;break;
  case 2:P0_2=0;P0_1=0;P0_0=1;break;
  case 3:P0_2=0;P0_1=1;P0_0=0;break;
  case 4:P0_2=0;P0_1=1;P0_0=1;break;
  case 5:P0_2=1;P0_1=0;P0_0=0;break;
  case 6:P0_2=1;P0_1=0;P0_0=1;break;
  case 7:P0_2=1;P0_1=1;P0_0=0;break;
  case 8:P0_2=1;P0_1=1;P0_0=1;break;
 }
 P3=shuzi[num];
 delay(1);
}

 

3. Sub-function 2

    Delay function. Cooperating with the digital tube control function, it is responsible for the hardware control of the dynamic digital tube display.

/******************************************延时函数***************************************/
void delay(unsigned int xms)//延时x毫秒 
{
  unsigned int i,j;
  for(i=xms;i>0;i--)
    for(j=112;j>0;j--);
return;
}

 

4. Sub-function 3

    timer function. Because a high level of 10us is required when outputting ultrasonic waves, this function is defined. It can also be used to calculate the time from sending to receiving.

/**********************************定时器函数 延时10us*************************************/
void Delay10us()
{
 TMOD |= 0x01;//16位定时器/计数器
 TH0 = 0xFF;//赋初值
 TL0 = 0xF6;//赋初值
 TR0 = 1;//启动

 while(!TF0);//溢出

 TF0 = 0;//清溢出
}

 

5. Subfunction 4

    Calculate the distance function. Used to convert measured time into distance.

/******************************************算距离函数***************************************/
float GetDistance(unsigned int time)  
{
 float distance;
 distance = (float)time * 0.017;//cm	   距离=高电平时间×声速/2        0.017cm/us
 
 return distance;//将距离返回主函数
}

 

6. Subfunction 5

    Measuring time function. Cooperate with the timer function to realize the emission and time measurement of ultrasonic waves.

/******************************************测时间函数***************************************/
unsigned int RunOnce()  
{
 unsigned int time;

/******************发送10us高电平信号*************/
 Trig = 0;
 Trig = 1;
 Delay10us();
 Trig = 0;
/**************等待高电平信号接收*****************/
 while(!Echo);
/*********T0清0重新计数(高电平持续时间)*********/
 TH0 = 0;
 TL0 = 0;
 TR0 = 1;
/*********等待高电平信号接收结束******************/
 while(Echo);
/*******************关闭T0计数********************/
 TR0 = 0;
/**********高电平时间赋值,单位us*****************/
 time = TH0*256 + TL0; 
 TH0 = 0;
 TL0 = 0;

 return time;
}

 

7. Subfunction 6

    The dynamic digital tube displays the result function. It is used to control the dynamic digital tube to display the measured distance.

/**************************************动态数码管显示结果***********************************/
void xianshi(int d)
{
  int k=8,m;
  while(d!=0)
  {
    m=(d%10);//在数码管上显示个位,从后往前显示
    shumaguan(k,m);
	k--;//数码管向前移位
	d=d/10;
  }
}

 

6. System test  

 

Measure the shortest effective distance, which is 3cm.a0e7078ec3284a8aa845603e7018f632.png

 The farthest effective distance measured is 560cm.

c36a1c75abd74f6dbd6530952018f954.png

 

 7. Source code

#include <REGX51.H>
#include<intrins.h>

sbit Trig = P1^0;
sbit Echo = P1^1;
/******************************************延时函数***************************************/
void delay(unsigned int xms)//延时x毫秒 
{
  unsigned int i,j;
  for(i=xms;i>0;i--)
    for(j=112;j>0;j--);
return;
}
/******************************************数码管控制函数***************************************/
unsigned char shuzi[]={0x3f/*0*/,0x06/*1*/,0x5b/*2*/,0x4f/*3*/,0x66/*4*/,0x6d/*5*/,0x7d/*6*/,0x07/*7*/,0x7f/*8*/,0x6f/*9*/};//显示的数字(num)的数组

void shumaguan(unsigned char wei,num)//第几位(wei)显示
{
 switch(wei)
 {
  case 1:P0_2=0;P0_1=0;P0_0=0;break;
  case 2:P0_2=0;P0_1=0;P0_0=1;break;
  case 3:P0_2=0;P0_1=1;P0_0=0;break;
  case 4:P0_2=0;P0_1=1;P0_0=1;break;
  case 5:P0_2=1;P0_1=0;P0_0=0;break;
  case 6:P0_2=1;P0_1=0;P0_0=1;break;
  case 7:P0_2=1;P0_1=1;P0_0=0;break;
  case 8:P0_2=1;P0_1=1;P0_0=1;break;
 }
 P3=shuzi[num];
 delay(1);
}
/**********************************定时器函数 延时10us*************************************/
void Delay10us()
{
 TMOD |= 0x01;//16位定时器/计数器
 TH0 = 0xFF;//赋初值
 TL0 = 0xF6;//赋初值
 TR0 = 1;//启动

 while(!TF0);//溢出

 TF0 = 0;//清溢出
}
/******************************************算距离函数***************************************/
float GetDistance(unsigned int time)  
{
 float distance;
 distance = (float)time * 0.017;//cm	   距离=高电平时间×声速/2        0.017cm/us
 
 return distance;//将距离返回主函数
}
/******************************************测时间函数***************************************/
unsigned int RunOnce()  
{
 unsigned int time;

/******************发送10us高电平信号*************/
 Trig = 0;
 Trig = 1;
 Delay10us();
 Trig = 0;
/**************等待高电平信号接收*****************/
 while(!Echo);
/*********T0清0重新计数(高电平持续时间)*********/
 TH0 = 0;
 TL0 = 0;
 TR0 = 1;
/*********等待高电平信号接收结束******************/
 while(Echo);
/*******************关闭T0计数********************/
 TR0 = 0;
/**********高电平时间赋值,单位us*****************/
 time = TH0*256 + TL0; 
 TH0 = 0;
 TL0 = 0;

 return time;
}
/**************************************动态数码管显示结果***********************************/
void xianshi(int d)
{
  int k=8,m;
  while(d!=0)
  {
    m=(d%10);//在数码管上显示个位,从后往前显示
    shumaguan(k,m);
	k--;//数码管向前移位
	d=d/10;
  }
}

/******************************************主函数***************************************/
void main()
{
 unsigned int time = 0;
 float distance;

 while(1)
 {  
  time = RunOnce();//传感器接收到高电平的时间
  distance = GetDistance(time);
  xianshi(distance);
 }
}

 

 

 

Guess you like

Origin blog.csdn.net/m0_57007304/article/details/129452740