#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Lcd1602_RS = P2^6;
sbit Lcd1602_RW = P2^5;
sbit Lcd1602_EN = P2^7;
sbit Trig = P2^1;
sbit Echo = P2^0;
sbit beep = P2^3;
unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','-','M'};
unsigned int time=0;
unsigned long S=0;
bit flag =0;
uchar disdat[4] ={0,0,0,0,};
void delayms(uint ms)
{
uchar i=100,j;
for(;ms;ms--)
{
while(--i)
{
j=10;
}
}
}
void Lcd1602_Write_com(uchar cmd)
{
Lcd1602_RS=0;
P0=cmd;
delayms(1);
Lcd1602_EN =1;
delayms(1);
Lcd1602_EN =0;
delayms(1);
}
void Lcd1602_Write_Data(uchar shu)
{
Lcd1602_RS=1;
P0=shu;
delayms(1);
Lcd1602_EN =1;
delayms(1);
Lcd1602_EN =0;
delayms(1);
}
void write_1602(uchar add,uchar sj)
{
Lcd1602_Write_com(0x80+0x40+add);
Lcd1602_Write_Data(sj) ;
}
void zifuchuan(uchar *ch)
{
while(*ch!=0)
Lcd1602_Write_Data(*ch++);
delayms(5);
} //仔细看一下那个程序
void feng()
{
if((S>=50)||flag==1)
{
beep = ~beep;
}
else
{
beep = 1; //取消报警
}
}
void LcdInit()
{
Lcd1602_EN =0;
Lcd1602_RW =0;
Lcd1602_Write_com(0x38);
Lcd1602_Write_com(0x0c);
Lcd1602_Write_com(0x06);
Lcd1602_Write_com(0x80);
Lcd1602_Write_com(0x01);
zifuchuan("cejujuli") ;
}
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100;
disdat[0]=S%1000/100;
disdat[1]=S%1000%100/10;
disdat[2]=S%1000%10%10;
write_1602(1,disdat[0]+0x30);
write_1602(2,disdat[1]+0x30);
write_1602(3,disdat[2]+0x30);
write_1602(5,'c');
write_1602(6,'m');
}
void zd0() interrupt 1
{
flag=1;
}
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 main(void)
{
delayms(5);
LcdInit();
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
EA=1;
beep = 0;
delayms(150);
while(1)
{
StartModule();
while(!Echo);
TR0=1;
while(Echo);
TR0=0 ;
if((S>=50)||flag==1)
{
flag = 0;
feng ();
if(beep == 1);
Conut();
delayms(5);
}
}
}
|