超声波测距C语言源程序代码.docx

上传人:b****8 文档编号:30425226 上传时间:2023-08-14 格式:DOCX 页数:17 大小:17.67KB
下载 相关 举报
超声波测距C语言源程序代码.docx_第1页
第1页 / 共17页
超声波测距C语言源程序代码.docx_第2页
第2页 / 共17页
超声波测距C语言源程序代码.docx_第3页
第3页 / 共17页
超声波测距C语言源程序代码.docx_第4页
第4页 / 共17页
超声波测距C语言源程序代码.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

超声波测距C语言源程序代码.docx

《超声波测距C语言源程序代码.docx》由会员分享,可在线阅读,更多相关《超声波测距C语言源程序代码.docx(17页珍藏版)》请在冰豆网上搜索。

超声波测距C语言源程序代码.docx

超声波测距C语言源程序代码

超声波测距C语言源程序代码

/*{HZ即单位s的倒数}本晶振为12MHZ,因此外部的时钟频率为12MHZ,所以内部的时钟频率为(12MHZ)/12=1MH

即1000000HZ,而机械频率为1/(1MHZ),即每完成一次计算(即定时器的值加一)用时0.000001s,

即1us(微秒).*/

/***********************************************************************************/

//具有模式选择.

#include

#defineUCunsignedchar

#defineUIunsignedint

voiddelay(UI);//延时9.56us程序

sbitbeep=P1^3;//用于声音报警

sbitLv=P1^7;//用于光报警

sbitHong=P1^6;

sbitQD=P3^7;//K8//P3^7口(K8)为确定键,

sbitGX=P3^1;//K7//P3^3口(K2)为修改键,

sbitSX=P3^6;//K6//P3^2(K3)为测量键.

sbitBX=P3^0;//K5//个(K7),十(K6),百(K5),三位修改键

sbita=P1^2;//百位//数码管位选

sbitb=P1^1;//十位

sbitc=P1^0;//个位

sbittrig=P1^4;//方波发射端

sbitecho=P1^5;//超声波接收端

voidIntConfiguration();//用来"设置中断"的函数,P3^3口(K2)为修改键,P3^2(K3)为测量键.

voidTimeConfiguration();//用来"设置定时器"的函数

sbitK1=P3^4;//动态

sbitK4=P3^5;//静态//用于进行模式切换(K1、K4键)

voidxiaxian();//修改函数,用来修改下限

voidshangxian();//修改函数,用来修改上限

UImin[3]={0,5,0};//报警极限,拆分为"百十个"三位

UImax[3]={3,0,0};//MIN,MAX用来存储最大和最小值

voidMINxianshi(UI);//最小范围和最大范围的显示

voidMAXxianshi(UI);

UCcodeCharacterCode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

//数码管数字字符(P2口)

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

voidmain()

{

TimeConfiguration();//设置定时器0

IntConfiguration();//设置中断允许,K4键为修改键,K8键为确定键

while

(1)

{

MINxianshi(40);//1.50169000s

MAXxianshi(40);//1.50098300s

}

}

/*******************************超声波测距函数********************************************/

voidzhongduan_0()interrupt0//测量中断函数(外部中断0)

{

UImoshi=0;

UIjuli=0;

UItime=0;

UIMAX,MIN;

UITT=0;//用于第一次测量时给P1^5口置一,以便正确读取数值

UIt1,t2,t3;

UIGE=0,SHI=0,BAI=0;//先定义三个变量,用来显示测量的距离.

a=0;

b=0;

c=0;

P2=~0x00;//防止最后显示的那个数码管一直亮

MAX=max[0]*100+max[1]*10+max[2];//计算最大与最小值

MIN=min[0]*100+min[1]*10+min[2];

while

(1)//下面进行测量

{

while

(1)/*先进行模式判断*/

{

if(0==K1)

{

moshi=1;

break;//模式1为动态测量

}

if(0==K4)

{

moshi=2;

break;//模式2为静态测量

}

if(0==QD)

return;//结束测量函数

}

/********************************************计算距离************************************************/

loop:

beep=1;//关掉定时器

Lv=1;

Hong=1;//关掉灯

a=0;

b=0;

c=0;

P2=~0x00;//防止最后显示的那个数码管一直亮

if((0==QD)&&(1==moshi))

break;

if((0==QD)&&(2==moshi))

{

delay(55500);

if(0==QD)

{

delay(55500);

if(0==QD)

break;

}

}

t1=35,

t2=35;

t3=35;

trig=0;

echo=0;

delay

(2);//初始化拉低两个端口

trig=1;

delay

(2);

trig=0;//输出端输出27us的高电压,并将输出端口拉低

while(echo==0);//判断是否有回波返回,有则开启定时器

TR0=1;//当有高电平输出时,开启定时器

while(echo==1);

TR0=0;//当高电平变成低电平时,关闭定时器

++TT;//测量值加一,记录测量次数

if(1==TT)

{

delay(55500);

TH0=0x00;TL0=0x00;//定时器的初值,定时器的定时为65536us.

gotoloop;

}

time=TL0+TH0*256;//接下来显示测量的距离

TH0=0x00;TL0=0x00;//定时器的初值,定时器的定时为65536us.

juli=(int)((time*0.034)/2);

BAI=((juli%1000)/100);SHI=((juli%100)/10);GE=(juli%10);

/******************************************两种模式的距离显示********************************************/

if(juli>MAX)

{

Hong=0;

Lv=1;

while(t1--)

{

a=0;

b=1;

c=1;

P2=~CharacterCode[BAI];

delay(400);

a=1;

b=0;

c=1;

P2=~CharacterCode[SHI];

delay(400);

a=1;

b=1;

c=0;

P2=~CharacterCode[GE];

delay(390);

beep=0;

if((1==moshi)&&(0==t1))

gotoloop;

if(moshi==2)

{

t1=2;

if(0==QD)

gotoloop;

}

}

}

elseif(juli

{

Lv=0;

Hong=1;

while(t2--)

{

a=0;

b=1;

c=1;

P2=~CharacterCode[BAI];

delay(500);

a=1;

b=0;

c=1;

P2=~CharacterCode[SHI];

delay(500);

a=1;

b=1;

c=0;

P2=~CharacterCode[GE];

delay(400);

beep=0;

delay(100);

beep=1;

if((1==moshi)&&(0==t2))

gotoloop;

if(2==moshi)

{

t2=2;

if(0==QD)

gotoloop;

}

}

}

else

{

beep=1;

Lv=1;

Hong=1;

while(t3--)

{

a=0;

b=1;

c=1;

P2=~CharacterCode[BAI];

delay(600);

a=1;

b=0;

c=1;

P2=~CharacterCode[SHI];

delay(600);

a=1;

b=1;

c=0;

P2=~CharacterCode[GE];

delay(600);

if((1==moshi)&&(0==t3))

gotoloop;

if(2==moshi)

{

t3=2;

if(0==QD)

gotoloop;

}

}

}//显示结束

}

}

/***********************************************************************************/

voidzhongduan_1()interrupt1//定时器溢出时的中断,显示测得的距离(定时器中断0)

{

TH0=0x00;//定时器的初值,定时器的定时为65536us,

TL0=0x00;

}

/***********************************************************************************/

voidzhongduan_2()interrupt2//修改键(K4)的中断函数(外部中断1)

{

xiaxian();

while(QD==0);

shangxian();

}

/**********************************************************************************/

voidMINxianshi(UITT)//显示最小距离

{

while(TT--)

{

a=0;

b=1;

c=1;

P2=~CharacterCode[min[0]];

delay(500);

a=1;

b=0;

c=1;

P2=~CharacterCode[min[1]];

delay(500);

a=1;

b=1;

c=0;

P2=~CharacterCode[min[2]];

delay(500);

}

P2=~0x00;

delay(55500);

}

voidMAXxianshi(UITT)//显示最大距离

{

while(TT--)

{

a=0;

b=1;

c=1;

P2=~CharacterCode[max[0]];

delay(500);

a=1;

b=0;

c=1;

P2=~CharacterCode[max[1]];

delay(500);

a=1;

b=1;

c=0;

P2=~CharacterCode[max[2]];

delay(500);

}

P2=~0x00;

delay(55500);

}

/***********************************************************************************/

voiddelay(UIT)//延时程序

{

while(T--);

}

/***********************************************************************************/

voidIntConfiguration()//设置中断函数

{

//优先级设置

PT2=0;

PS=0;

PT1=0;

PX1=0;

PT0=1;

PX0=0;

IT1=1;//外部中断0为跳变沿触发

EX1=1;//P3^3口(K4键)修改键,中断允许开启

IT0=1;//外部中断1为跳变沿触发

EX0=1;//P3^2口(K1键)测量键,中断允许开启

ET0=1;//定时器0的中断允许开启

EA=1;

}

voidTimeConfiguration()//设置定时器,以及定时器的初值

{

TMOD=0x01;//设定只使用0号定时器;模式:

定时器;工作方式:

1号工作方式.

//下面是定时器的初始值,TR0,TR1是用来开启定时器的

TH0=0x00;//定时器的初值,定时器的定时为50us.

TL0=0x00;

/*1号定时器不用,所以没有TH1,TL1*/

}

/***********************************************************************************/

voidxiaxian()//修改下限

{

while

(1)

{

if(BX==0)//百位

{

P2=~0x00;

min[0]++;

if(min[0]==10)

min[0]=0;

delay(60000);

}

a=0;

b=1;

c=1;

P2=~CharacterCode[min[0]];

delay(100);

if(SX==0)//十位

{

P2=~0x00;

min[1]++;

if(min[1]==10)

min[1]=0;

delay(60000);

}

a=1;

b=0;

c=1;

P2=~CharacterCode[min[1]];

delay(100);

if(GX==0)//个位

{

P2=~0x00;

min[2]++;

if(min[2]==10)

min[2]=0;

delay(60000);

}

a=1;

b=1;

c=0;

P2=~CharacterCode[min[2]];

delay(100);

if(QD==0)

{

a=0;

b=0;

c=0;

P2=0xff;

break;

}

}

}

voidshangxian()//修改上限

{

while

(1)

{

if(BX==0)//百位

{

P2=~0x00;

max[0]++;

if(max[0]==10)

max[0]=0;

delay(60000);

}

a=0;

b=1;

c=1;

P2=~CharacterCode[max[0]];

delay(100);

if(SX==0)//十位

{

P2=~0x00;

max[1]++;

if(max[1]==10)

max[1]=0;

delay(60000);

}

a=1;

b=0;

c=1;

P2=~CharacterCode[max[1]];

delay(100);

if(GX==0)//个位

{

P2=~0x00;

max[2]++;

if(max[2]==10)

max[2]=0;

delay(60000);

}

a=1;

b=1;

c=0;

P2=~CharacterCode[max[2]];

delay(100);

if(QD==0)

{

a=0;

b=0;

c=0;

P2=0xff;

while(QD==0);

break;

}

}

}

/************************************************************************************/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1