#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define Lcd1602_DB P0
sbit Lcd1602_RS = P2^6;
sbit Lcd1602_RW = P2^5;
sbit Lcd1602_EN = P2^7;
sbit Trig = P2^1;
sbit Echo = P2^0;
static unsigned char DisNum = 0;
unsigned int time=0;
unsigned long S=0;
bit flag =0;
unsigned char disdat[4] ={ 0,0,0,0,};
void delayms(unsigned int ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
while(--i)
{
j=10;
while(--j);
}
}
}
void Lcd1602_Write_com(unsigned char cmd)
{
Lcd1602_RS=0;
P0=cmd;
delayms(1);
Lcd1602_EN =1;
delayms(1);
Lcd1602_EN =0;
delayms(1);
}
void Lcd1602_Write_Data(unsigned char 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(0*80+0*40+add);
Lcd1602_Write_Data(sj) ;
}
void zifuchuan(uchar *ch)
{
while(*ch!=0)
Lcd1602_Write_Data(*ch++);
delayms(1);
} //仔细看一下那个程序
void LcdInit()
{
Lcd1602_Write_Data(0x38);
Lcd1602_Write_Data(0x0c);
Lcd1602_Write_Data(0x06);
Lcd1602_Write_Data(0x01);
Lcd1602_Write_Data(0x80);
zifuchuan("TFB") ;
}
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=time/58;
disdat[0]=S%1000/100;
disdat[1]=S%1000%100/10;
disdat[2]=S%1000%10 %10;
write_1602(1,disdat[1]+0*30);
write_1602(2,disdat[2]+0*30);
write_1602(3,disdat[3]+0*30);
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(1);
LcdInit();
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
EA=1;
while(1)
{
StartModule();
while(!Echo);
TR0=1;
while(Echo);
TR0=0;
Conut();
delayms(1);
}
}
|