您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 51单片机超声波测距程序
//超声波测距,测距范围2cm-400cm;#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedcharsbittrig=P1^0;sbitecho=P3^2;sbittest=P1^1;//测试灯sbitdula=P2^6;sbitwela=P2^7;sbitBEEP=P2^3;uinttimeh,timel,distance;uintge,shi,bai,xiaoshu,flag,time;/*共阴极数码管不带小数点代码表*/ucharcodelist[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};/*共阴极数码管带小数点代码表*/ucharcodelisttwo[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};/*长延时函数*/voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=100;y0;y--);}/*短延时函数*/voiddelay20us(){uchara;for(a=0;a100;a++);}/*报警函数*/voidbeer(){//BEEP=0;delay(10);}/*定时器初始化*/voidinitime0(){TMOD=0x01;TH0=0;TL0=0;EA=0;ET0=0;EX0=0;}/*外部中断函数*/voidestern()interrupt0{timeh=TH0;timel=TL0;beer();flag=1;//进入中断,标志位就置1EX0=0;//同时关断外部中断和定时器TR0=0;}/*显示函数*/voiddisplay(distance){bai=distance/1000;shi=distance%1000/100;ge=distance%100;xiaoshu=distance%10;dula=1;P0=list[xiaoshu];dula=0;P0=0xff;wela=1;P0=0xf7;wela=0;delay(2);dula=1;P0=listtwo[ge];dula=0;P0=0xff;wela=1;P0=0xfb;wela=0;delay(2);dula=1;P0=list[shi];dula=0;P0=0xff;wela=1;P0=0xfd;wela=0;delay(1);dula=1;P0=list[bai];dula=0;P0=0xff;wela=1;P0=0xfe;wela=0;delay(1);}/*被调用子函数*/voiddiaoyong(){uinti;EA=0;echo=1;//为了检测电平的高低,首先必须拉高trig=1;delay20us();trig=0;while(echo==0);//如果进入中断即接收到超声波就向下执行flag=0;//接收到就清除标志位EA=1;//同时打开总中断EX0=1;//打开外部中断TR0=1;//开定时器TH0=0;//定时器清零TL0=0;for(i=0;i100;i++)//等待测量的结果{display(distance);//用100次显示循环来延时,解决数码管显示不亮问题}//delay(50);//用延时函数数码管闪烁TR0=0;//延时一段时间后关断定时器EX0=0;//延时一段时间后关断外部中断if(flag==1)//如果进入中断,说明测距已经测好{time=timeh*256+timel;//计算测定距离,并显示distance=time*0.1720;display(distance);}if(flag==0)//如果没有进入中断,距离为0,同时灯闪烁{distance=0;test=!test;}}/*主函数*/voidmain(){initime0();test=0;trig=0;EA=1;while(1){diaoyong();display(distance);}}
本文标题:51单片机超声波测距程序
链接地址:https://www.777doc.com/doc-1758283 .html