?? 超聲波測距離.c
字號:
#include <AT892051.H>
#define unit unsigned int
#define uchar unsigned char
sbit fs=P3^0; //發送端;
sbit h=P3^7;
sbit l=P3^5; //數碼管位選端;
sbit m=P3^4;
uchar tab[16]={0x28,0xEB,0x32,0xA2,0xE1,0xA4,0x24,0xEA,0x20,0xA0,0x60,0x25,0x3C,0x23,0x34,0x74};//段碼;
uchar u[3]; //顯示數組;
unit count,b;
void delay(unit a) //延時;
{
unit m;
for(m=0;m<a;m++);
}
void tx() //從P3.0發出40KHz的脈沖
{
uchar n,p;
for(n=0;n<40;n++){
fs=1;
for(p=0;p<3;p++);
fs=0;
for(p=0;p<2;p++);
fs=0;
}
}
void display(void) //顯示;
{
for(;;)
{
l=1;m=1;h=1;
P1=tab[u[0]];
m=0;
delay(10);
m=1;
P1=tab[u[1]];
l=0;
delay(10);
l=1;
P1=tab[u[2]];
h=0;
delay(10);
h=1;
}
}
void rx() interrupt 0 //外部中斷0,接收信號
{
TR0=0;
count=TH0*256+TL0;
if(count<300);
if(count>=300)
{
b=(17*count)/1000;
u[0]=b%10;
u[1]=(b/10)%10;
u[2]=(b/100)%10;
display();
}
}
void over()interrupt 1 //T0溢出為無效測量FFF;
{
u[0]=15;
u[1]=15;
u[2]=15;
display();
}
void main()
{
fs=0;
delay(8600);
TH0=0;
TL0=0;
TMOD=0x01;
TR0=1;
EA=1;
ET0=1;
PT0=1;
tx();
IT0=1;
IE=0x83;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -