#include<reg52.h>
#include<absacc.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define BUZZ XBYTE[0xBFFF]
uchar code table[]=
{0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90};
uchar dspbuf[8]={10,10,10,10,10,10,10,10};
uchar dspcom=0;
sbit TX=P1^0; //发送
sbit RX=P1^1; //接收
uchar a,flag;
void delay()//延迟函数
{
uchar i;
i=30;
_nop_();
_nop_();
_nop_();
while(--i);
}
void display()//显示函数
{
XBYTE[0xE000]=0xff;
XBYTE[0xC000]=(1<<dspcom);
XBYTE[0xE000]=table[dspbuf[dspcom]];
if(++dspcom==8)
{
dspcom=0;
}
}
void init()
{
BUZZ=0;
TMOD=0x11;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
TH1=0;
TL1=0;
EA=1;
ET0=1;
TR0=1;
ET1=0;
TR1=0;
}
void send_wave() //发送方波
{
uchar x;
for(x=0;x<8;x--)//发送八个方波
{
TX=1;
delay();
TX=0;
delay();
}
}
uint getdistance() //测距
{
uint distance,time;
send_wave();
TR1=1;
while((RX==1)&&(TF1==0));
TR1=0;
if(TF1==1)
{
TF1=0;
distance=999;
}
else
{
time=TH1;
time=(time<<8)|TL1;
distance=time*17/1000;
}
TH1=TL1=0;
return distance;
}
void main()
{
uint value;
init();
while(1)
{
if(flag==1)
{
flag=0;
value=getdistance();
dspbuf[0]=value%1000/100;
dspbuf[1]=value%100/10;
dspbuf[2]=value%10;
}
}
}
void timer0() interrupt 1//中断
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
display();
if(++a==100)
{
a=0;
flag=1;
}
}
#include<absacc.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define BUZZ XBYTE[0xBFFF]
uchar code table[]=
{0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90};
uchar dspbuf[8]={10,10,10,10,10,10,10,10};
uchar dspcom=0;
sbit TX=P1^0; //发送
sbit RX=P1^1; //接收
uchar a,flag;
void delay()//延迟函数
{
uchar i;
i=30;
_nop_();
_nop_();
_nop_();
while(--i);
}
void display()//显示函数
{
XBYTE[0xE000]=0xff;
XBYTE[0xC000]=(1<<dspcom);
XBYTE[0xE000]=table[dspbuf[dspcom]];
if(++dspcom==8)
{
dspcom=0;
}
}
void init()
{
BUZZ=0;
TMOD=0x11;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
TH1=0;
TL1=0;
EA=1;
ET0=1;
TR0=1;
ET1=0;
TR1=0;
}
void send_wave() //发送方波
{
uchar x;
for(x=0;x<8;x--)//发送八个方波
{
TX=1;
delay();
TX=0;
delay();
}
}
uint getdistance() //测距
{
uint distance,time;
send_wave();
TR1=1;
while((RX==1)&&(TF1==0));
TR1=0;
if(TF1==1)
{
TF1=0;
distance=999;
}
else
{
time=TH1;
time=(time<<8)|TL1;
distance=time*17/1000;
}
TH1=TL1=0;
return distance;
}
void main()
{
uint value;
init();
while(1)
{
if(flag==1)
{
flag=0;
value=getdistance();
dspbuf[0]=value%1000/100;
dspbuf[1]=value%100/10;
dspbuf[2]=value%10;
}
}
}
void timer0() interrupt 1//中断
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
display();
if(++a==100)
{
a=0;
flag=1;
}
}