//超声波模块程序
//超声波模块程序
//Trig P2^0
//Echo P3^2
#include
#define uchar unsigned char
#define uint unsigned int
int time;
int succeed_flag;
uchar timeL;
uchar timeH;
sbit Trig P1^0;
sbit Echo P3^2;
uchar code
table[] {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f};
uchar code table1[] {0,1,2,3,4,5,6,7};
//
void delay(uint z)
{
uint x,y;
for(x z;x>0;x--)
for(y 110;y>0;y--);
}
//
void delay_20us()
{
uchar a ;
for(a 0;a<100;a++);
}
//*****************************************************
**********
//显示数据转换程序
void display(uint temp)
{
uchar ge,shi,bai;
bai temp/100;
shi (temp%100)/10;
ge temp%10;
P2 table1[2];
P0 table[ge];
delay(1);
P2 table1[1];
P0 table[shi];
delay(1);
P2 table1[0];
P0 table[bai];
delay(1);
}
//*****************************************************
**********
void main()
{
uint distance;
// test 0;
Trig 0; //首先拉低脉冲输入引脚
EA 1; //打开总中断0
TMOD 0x10; //定时器1,16位工作方式
while(1)
{
EA 0; //关总中断
Trig 1; //超声波输入端
delay_20us(); //延时20us
Trig 0; //产生一个20us的脉冲
while(Echo 0); //等待Echo 回波引脚变高电平
succeed_flag 0; //清测量成功标志
EA 1;
EX0 1; //打开外部中断0
TH1 0; //定时器1清零
TL1 0; //定时器1清零
TF1 0; //计数溢出标志
TR1 1; //启动定时器1
delay(20); //等待测量的结果
TR1 0; //关闭定时器1
EX0 0; //关闭外部中断0
if(succeed_flag 1)
{
time timeH*256+timeL;
distance time*0.0172; //厘米
}
if(succeed_flag 0)
{
distance 0; //没有回波则清
零
// test !test;