⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.c

📁 超声波测距 很方便的测量距离 超声波测距 很方便的测量距离
💻 C
字号:
//**********************陆军上将******************************//
//---------------------------------------------------------------//
#include<reg51.h>
#include<absacc.h>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long

sbit red=P2^0;
sbit yellow=P2^1;
sbit green=P2^2;  
sbit out=P3^0; 	      //超声波发射控制口
sbit DQ=P2^5;
uchar temp_value;      //温度值
uchar TempBuffer[4];

unsigned char code dis3[] = {"状态——安全距离"};
unsigned char code dis30[] = {"状态——保持距离"};
unsigned char code dis31[] = {"状态——危险距离"};
ulong a,b,c,d,f,S,G;
uchar flag;
uchar cdist[6];

////////////////////////////////////////////////////////////////////////////////////
/////////////////////////发射函数
void delayk(uint z)  //100uS延时子程序
{
     uint x,y;
     for(x=z;x>0;x--)
	     for(y=11;y>0;y--);
}

////////////////////////////////////////////////
void send(void)	 //超声波发射控制子程序 
{
     out=1;
     TR0=1;		//启动定时器0
     delayk(2);	// 延时200uS
	 out=!out;  
     delayk(200);	// 延时200mS

}

//////////////////////////////////////////////////////////
/////////////////////////////////////////////////
/**********温度计**********************/

/////////////////////////////////////////////////////////
////////////////////////////////// 	lcd显示器
												
uint comm,dat1,dat2,dat3;
uchar table2[]={"当前距离—"};
uchar table1[]="状态——安全距离";
sbit rst=P2^0;
sbit rs=P2^1;
sbit rw=P2^2;
sbit e=P2^3;
sbit psb=P2^4;
void delay(uint time);
void xiezhiling(void);
void xiexianshi(void);





/////////////////////////////////////////
void xianshi1()
{ 
	uchar i;
	psb=1;
	rst=0;
	_nop_();
	_nop_();
	_nop_();
	rst=1;
	comm=0x88;
	xiezhiling();
	for(i=0;i<16;i++)
	{ 
		 dat1=table1[i];
		 xiexianshi();
		 delay(100);
	}
}

//////////////////////////////////////////
void xianshi2()
{
	uchar i;
	psb=1;
	rst=0;
	_nop_();
	_nop_();
	_nop_();
	rst=1;
	comm=0x90;
	xiezhiling();
		for(i=0;i<10;i++)
		{ dat1=table2[i];
		   xiexianshi();
		   delay(100);
		} 
}


////////////////////////////////////////////
void delay(uint time)//延时多长时间?
{ 
	uint i;
	for(i=0;i<time;i++);
}

////////////////////////////////////////////
void xiezhiling(void)
{
	rs=0;
	rw=0;
	P1=comm;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	e=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	e=0;
}
void xiexianshi(void)
{ 
	rs=1;
	rw=0;
	P1=dat1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	e=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	e=0;
}

void init(void)
{
	comm=0x30;
	xiezhiling();
	delay(100);
	comm=0x0c;
	xiezhiling();
	delay(100);
	comm=0x01;
	xiezhiling();
	delay(100);
	comm=0x02;
	xiezhiling();
	delay(100);
}
////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////显示温度

///////////////////////////////////////////////////////////

//////////////////////////////////////////////// 距离处理
void dtime(void)
{
	uint i;
	d=b*256+a;
	S=d*170;
	G=S/10000;

	cdist[0]=(G/100)%10+'0';
	cdist[1]='.';
	cdist[2]=(G/10)%10+'0';
	cdist[3]=G%10+'0';
	cdist[4]=0xc3;
    cdist[5]=0xd7;
    psb=1;
	comm=0x95;
    xiezhiling();
	for(i=0;i<6;i++)
	    {  
		  dat1=cdist[i];
	      xiexianshi();
	      delay(1000);
	   }
}


////////////////////////////////////////////////////////////////////////
/////////////////////////////////中断接收函数	
void service_int0() interrupt 0 using 0
{

	TR0=0;		  //	 定时器0停止计时
	IE=0x00;	  //	 中断禁止
	b=TH0;		  //
	a=TL0;		  //
	dtime();   	 //      计算距离
	TH0=0x00;	  //	 计数器高八位
	TL0=0X00;     //	 计数器第八位
	IE=0x81;      //	 开总中断,外部中断0允许

}

/////////////////////////////////////////////////////////////

 ///////////////////////////////////////////////////////////////
 /////////////////////////////////////////主函数

void main(void)
{
   TMOD=0X01;	  //
   TH0=0x00;
   TL0=0x00;
   IE=0x81;	    //	 开总中断,外部中断0允许
   out=0;
   init();
 
   xianshi1();
   xianshi2();

   while(1)
   {
   		send();
   }		  


}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -