基于89C52单片机的HC-SR04超声波测距系统程序
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit DU=P2^6;
sbit WE=P2^7;
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P2^5;
sbit RX=P3^2;
sbit TX=P1^5;
uint time=0;
unsigned long S=0;
bit flag=0;
uchar disdat[]={0,0,0,0};
void delay(uint xms)
{
uint x,y;
for(x=xms;x>0;x--)
for(y=112;y>0;y--);
}
void write_com(uchar com)
{
RS=0;
RW=0;
P0=com;
delay(5);
EN=1;
delay(5);
EN=0;
}
void write_dat(uchar dat)
{
RS=1;
RW=0;
P0=dat;
delay(5);
EN=1;
delay(5);
EN=0;
}
void write_1602(uchar add,uchar dat2)
{
write_com(0x80+0x40+add);
write_dat(dat2);
}
void ZIFU(uchar *ch)
{
while(*ch!=0)
{
write_dat(*ch++);
delay(20);
}
}
void init()
{
DU=0;
WE=0;
EN=0;
write_com(0x38);
delay(2);
write_com(0x0f);
write_com(0x06);
write_com(0x80);
write_com(0x01);
ZIFU(" From JM Xiao");
}
void count()
{
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,'D');
write_1602(2,'i');
write_1602(3,'s');
write_1602(4,'t');
write_1602(5,'a');
write_1602(6,'n');
write_1602(7,'c');
write_1602(8,'e');
write_1602(9,':');
write_1602(10,disdat[0]+0x30);
write_1602(11,disdat[1]+0x30);
write_1602(12,disdat[2]+0x30);
write_1602(13,'C');
write_1602(14,'M');
}
void start()
{
TX=1;
_nop_(); _nop_();
_nop_(); _nop_();
_nop_(); _nop_();
_nop_(); _nop_();
_nop_(); _nop_();
_nop_(); _nop_();
_nop_(); _nop_();
_nop_(); _nop_();
TX=0;
}
void timer0() interrupt 1
{
flag=1;
}
void main()
{
delay(400);
init();
TMOD=0x01;
TH0=0;
TL0=0;
EA=1;
ET0=1;
while(1)
{
start();
while(!RX);
TR0=1;
while(RX);
TR0=0;
count();
delay(20);
}
}
DU和WE定义的什么啊
2024-09-01 广告