LCD1602液晶显示+超声波测距-51单片机

#include 
#include 
#define unchar unsigned char
//unchar LED_D[]= {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned long time,distance;
unsigned int timer=0;
sbit reciever=P3^2;
sbit control=P3^3;
bit flag=0;
sbit RS = P1^5;
sbit RW = P1^6;
sbit E = P1^7;
sbit BF = P0^7;
void Time_init()
{
    TMOD=0x11;
    TH1=(65536-2000)/256;
    TL1=(65536-2000)%256;
    TH0=0;
    TL0=0;
    EA=1;
    ET1=1;
    ET0=1;
    TR1=1;
}
void delay1ms()
{
    unsigned char i,j;
    for(i=0; i<10; i++)
        for(j=0; j<33; j++);
}
void delay(unsigned int n)
{
    unsigned int i;
    for(i=0; i7000||flag)
    {
        flag=0;
        writedata(0x30);
        delay(10);
    }
    else
    {
        unchar shu[8];
        unchar i=0;
        while(distance)
        {
			if(i==1)
			{
				shu[++i]=0;
				continue;
			}
            shu[++i]=distance%10;
            distance/=10;
        }
	
        while(i)
        {
			if(i==2)
			{
				writedata(0x2e);
				i--;
				delay(10);
				continue;
			}
            writedata(shu[i]+0x30);
	    	i--;
            delay(10);
         }
    }

}


void Time_inter0()interrupt 1
{
    flag=1;
}
void Time_inter1()interrupt 3
{
    TH1=(65536-2000)/256;
    TL1=(65536-2000)%256;
    timer++;
    if(timer>=400)
    {
        timer=0;
        control=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        control=0;
    }
}
void main()
{
    Time_init();
    while(1)
    {
        while(!reciever);
        TR0=1;
        while(reciever);
        TR0=0;
        resolve();
    }
}

你可能感兴趣的:(LCD1602液晶显示+超声波测距-51单片机)