#include<reg51.h>
#include<intrins.h>
#include"lcd.h"
sbit Trig = P2^1;
sbit Echo = P2^0;
sbit beep = P1^5;
unsigned char PuZh[]=" 00-00-00 ";
unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','-','M'};
static unsigned char DisNum = 0; //ÏÔê¾óÃÖ¸Õë
unsigned int time=0;
unsigned long S=0;
bit flag =0;
unsigned char disbuff[4] ={ 0,0,0,0,};
/*******************************************************************************
* oˉ êy Ãû : main
* oˉêy1|Äü : Ö÷oˉêy
* êä èë : ÎT
* êä 3ö : ÎT
*******************************************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100; //Ëã3öà′êÇCM
if((S>=700)||flag==1) //3¬3ö2aá¿·¶Î§ÏÔ꾡°-¡±
{
flag=0;
DisplayOneChar(0, 1, ASCII[11]);
DisplayOneChar(1, 1, ASCII[10]); //ÏÔê¾μã
DisplayOneChar(2, 1, ASCII[11]);
DisplayOneChar(3, 1, ASCII[11]);
DisplayOneChar(4, 1, ASCII[12]); //ÏÔê¾M
}
else
{
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
DisplayOneChar(0, 1, ASCII[disbuff[0]]);
DisplayOneChar(1, 1, ASCII[10]); //ÏÔê¾μã
DisplayOneChar(2, 1, ASCII[disbuff[1]]);
DisplayOneChar(3, 1, ASCII[disbuff[2]]);
DisplayOneChar(4, 1, ASCII[12]); //ÏÔê¾M
}
}
void zd0() interrupt 1 //T0ÖD¶ÏóÃà′¼ÆêyÆ÷òç3ö,3¬1y2a¾à·¶Î§
{
flag=1; //ÖD¶Ïòç3ö±êÖ¾
}
void StartModule() //Æô¶ˉÄ£¿é
{
Trig=1; //Æô¶ˉò»′ÎÄ£¿é
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
Trig=0;
}
void delayms(unsigned int ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
while(--i)
{
j=10;
while(--j);
}
}
}
void Delay10Us_24M(unsigned int n)
{
unsigned int i=0;
char j=0;
for(i=0;i<n;i++)
for(j=0;j<2;j++);
}
void fengmingqi()
{
uint i,j,y;
for(i=0;i<2000;i++)
{beep=~beep;for(j=0;j<24;j++);}
for(y=0;y<2000;y++)
{beep=~beep;for(j=0;j<25;j++);}
for(i=0;i<2000;i++)
{beep=~beep;for(j=0;j<26;j++);}
for(i=0;i<2000;i++)
{beep=~beep;for(j=0;j<27;j++);}
}
void main(void)
{
TMOD=0x01; //éèT0Îa·½ê½1£¬GATE=1£»
TH0=0;
TL0=0;
ET0=1; //ÔêDíT0ÖD¶Ï
EA=1; //¿aÆô×üÖD¶Ï
InitLcd1602();
LcdShowStr(0,0,PuZh);
while(1)
{
StartModule();
while(!Echo); //μ±RXÎaáãê±μè′y
TR0=1; //¿aÆô¼Æêy
while(Echo); //μ±RXÎa1¼Æêy2¢μè′y
TR0=0; //1رռÆêy
Conut(); //¼ÆËã
delayms(80);
if(S<=50)
{
fengmingqi();
}
else
beep=1;
}
} |