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

#include <reg52.h>
#include <intrins.h>
#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; i<n; i++)
        delay1ms();
}
bit busytest()
{
    bit result;
    RS=0;
    RW=1;
    E=1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    result=BF;
    E=0;
    return result;
}
void write_com(unsigned char dictate)
{
    while(busytest());
    RS=0;
    RW=0;
    E=0;
    _nop_();
    _nop_();
    P0=dictate;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    E=1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    E=0;
}
void writeaddress(unsigned char x)
{
    write_com(x|0x80);
}
void writedata(unsigned char y)
{
    while(busytest());
    RS=1;
    RW=0;
    E=0;
    P0=y;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    E=1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    E=0;
}
void Lcdinit()
{
    delay(15);
    write_com(0x38);
    delay(5);
    write_com(0x38);
    delay(5);
    write_com(0x38);
    delay(5);
    write_com(0x0F);
    delay(5);
    write_com(0x04);
    delay(5);
    write_com(0x01);
    delay(5);
}
void resolve()
{
    time=TH0*256+TL0;
    TH0=0;
    TL0=0;
    distance=time*0.17;
    Lcdinit();
    write_com(0x01);
    delay(5);
    writeaddress(0x80);
    delay(5);
    if(distance>7000||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();
    }
}

  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值