急求结合单片机STC89C52,HC-SR04超声波传感器,LCD1602的超声波测距代码
LZ看了好多别人的代码,自己也写过代码,但是总是不行,由于是初学者,恳请大神指导! 展开
这个是主函数的
1602.h的内容如下:
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit E=P2^7;
sbit RW=P2^6;
sbit RS=P2^5;
void delay()
{
uchar i=12;
while(i--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
bit Busy(void)
{
bit busy_flag = 0;
RS = 0;
RW = 1;
E = 1;
delay();
busy_flag = (bit)(P0 & 0x80);
E = 0;
return busy_flag;
}
void wcmd(uchar del)
{
while(Busy());
RS = 0;
RW = 0;
E = 0;
delay();
P0 = del;
delay();
E = 1;
delay();
E = 0;
}
void wdata(uchar del)
{
while(Busy());
RS = 1;
RW = 0;
E = 0;
delay();
P0 = del;
delay();
E = 1;
delay();
E = 0;
}
void L1602_init(void)
{
wcmd(0x38);
wcmd(0x0c);
wcmd(0x06);
wcmd(0x01);
}
void L1602_char(uchar hang,uchar lie,char sign)
{
uchar a;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
wcmd(a);
wdata(sign);
}
void L1602_string(uchar hang,uchar lie,uchar *p)
{
uchar a,b=0;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
while(1)
{
wcmd(a++);
b++;
if((*p == '\0')||(b==17)) break;
wdata(*p);
p++;
}
}
广告 您可能关注的内容 |