超声波

.H文件

#ifndef _SONIC_H_
#define _SONIC_H_

#include "STC15F2K60S2.h"
#include "delay.h"

sbit TX=P1^0;
sbit RX=P1^1;

void send_wave();
unsigned int get_distance();



#endif

.C文件

#include "sonic.h"

void send_wave()
{
	unsigned char j;
	for(j=0;j<=7;j++)
	{
		TX=1;
		delay_10us(1);
		TX=0;
		delay_10us(1);
	}
}

unsigned int get_distance()
{
	unsigned int distance;
	send_wave();
	TR0=1;
	while((RX==1)&&(TF0==0));    //TF0溢出中断标志位 溢出置1
	TR0=0;
	if(TF0==1)
	{
		TF0=0;
		distance=0;
	}
	else
	{
		distance=(TH0<<8)|TL0;
		TH0=0;
		TL0=0;
		distance=distance*17/1000;         //显示单位cm
	}
	return distance;
}

 

你可能感兴趣的:(蓝桥杯_库文件)