我自己写了一个超声波测距的程序,用51单片机,1602液晶显示,但是现在显示不出来,请各位大神帮忙
下边是我写的程序。#include<reg52.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsig...
下边是我写的程序。
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
ulong S;
uchar ge,shi,bai;
uint num;
uchar time;
sbit TX=P3^1;
sbit RX=P3^2;
sbit key1=P2^3;
sbit key2=P2^4;
sbit lcd_rs=P2^5;
sbit lcd_rw=P2^6;
sbit lcd_e=P2^7;
uchar flag;
uchar code table[]="distance";
uchar distance[]={0,1,2,3,4,5,6,7,8,9};
/*****************测距部分*******************/
void Delay100ms() //@11.0592MHz
{
unsigned char i, j;
i = 180;
j = 73;
do
{
while (--j);
} while (--i);
}
void Delay5us() //@11.0592MHz
{
}
void Delay10us() //@11.0592MHz
{
unsigned char i;
i = 2;
while (--i);
}
void count()
{
time=TH0+TL0*256;
S=time*1.73/100;
bai=S/100;
shi=S%100/10;
ge=S%100%10;
if(S>400||flag==1)
{
flag=0;
}
}
void timer0()
{
TMOD=0x01;
EA=1;
IT0=1;
ET0=1;
if(flag==0)
{
if(RX==1);
{
TH0=0x00;
TL0=0x00;
TR0=1;
}
if(RX==0);
{
TR0=0;
}
}
}
void startmodule()
{
TX=1;
Delay10us();
_nop_();
_nop_();
TX=0;
}
void Timer_0() interrupt 1
{
flag=1;
TH0=0x00;
TL0=0x00;
}
/***************1602显示部分******************/
void write_com(uchar com)
{
lcd_rs=0;
lcd_rw=0;
P1=com;
Delay5us();
lcd_e=1;
Delay5us();
lcd_e=0;
}
void write_data(uchar date)
{
lcd_rs=1;
lcd_rw=0;
P1=date;
Delay5us();
lcd_e=1;
Delay5us();
lcd_e=0;
}
uchar ReadDataLCM(void)
{
lcd_rs = 1;
lcd_rw = 1;
lcd_e = 0;
lcd_e = 0;
lcd_e = 1;
return(P1);
}
uchar ReadStatusLCM(void)
{
P1 = 0xFF;
lcd_rs = 0;
lcd_rw = 1;
lcd_e = 0;
lcd_e = 0;
lcd_e = 1;
return(P1);
}
void lcd_init()
{
lcd_e=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void displayonechar(uchar x,uchar y,uchar dat)
{
uchar address;
if(y==1)
{
address=0x80+x;
}
else
{
address=0xc0+x;
}
write_com(address);
write_data(dat);
}
void display()
{
write_com(0x80+0x10);
for(num=0;num<15;num++)
{
write_data(table[num]);
Delay5us();
}
displayonechar(0,1,distance[bai]);
displayonechar(1,1,'.');
displayonechar(2,1,distance[shi]);
displayonechar(3,1,distance[ge]);
Delay100ms();
}
void main()
{
startmodule();
lcd_init();
ReadDataLCM();
timer0();
count();
display();
} 展开
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
ulong S;
uchar ge,shi,bai;
uint num;
uchar time;
sbit TX=P3^1;
sbit RX=P3^2;
sbit key1=P2^3;
sbit key2=P2^4;
sbit lcd_rs=P2^5;
sbit lcd_rw=P2^6;
sbit lcd_e=P2^7;
uchar flag;
uchar code table[]="distance";
uchar distance[]={0,1,2,3,4,5,6,7,8,9};
/*****************测距部分*******************/
void Delay100ms() //@11.0592MHz
{
unsigned char i, j;
i = 180;
j = 73;
do
{
while (--j);
} while (--i);
}
void Delay5us() //@11.0592MHz
{
}
void Delay10us() //@11.0592MHz
{
unsigned char i;
i = 2;
while (--i);
}
void count()
{
time=TH0+TL0*256;
S=time*1.73/100;
bai=S/100;
shi=S%100/10;
ge=S%100%10;
if(S>400||flag==1)
{
flag=0;
}
}
void timer0()
{
TMOD=0x01;
EA=1;
IT0=1;
ET0=1;
if(flag==0)
{
if(RX==1);
{
TH0=0x00;
TL0=0x00;
TR0=1;
}
if(RX==0);
{
TR0=0;
}
}
}
void startmodule()
{
TX=1;
Delay10us();
_nop_();
_nop_();
TX=0;
}
void Timer_0() interrupt 1
{
flag=1;
TH0=0x00;
TL0=0x00;
}
/***************1602显示部分******************/
void write_com(uchar com)
{
lcd_rs=0;
lcd_rw=0;
P1=com;
Delay5us();
lcd_e=1;
Delay5us();
lcd_e=0;
}
void write_data(uchar date)
{
lcd_rs=1;
lcd_rw=0;
P1=date;
Delay5us();
lcd_e=1;
Delay5us();
lcd_e=0;
}
uchar ReadDataLCM(void)
{
lcd_rs = 1;
lcd_rw = 1;
lcd_e = 0;
lcd_e = 0;
lcd_e = 1;
return(P1);
}
uchar ReadStatusLCM(void)
{
P1 = 0xFF;
lcd_rs = 0;
lcd_rw = 1;
lcd_e = 0;
lcd_e = 0;
lcd_e = 1;
return(P1);
}
void lcd_init()
{
lcd_e=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void displayonechar(uchar x,uchar y,uchar dat)
{
uchar address;
if(y==1)
{
address=0x80+x;
}
else
{
address=0xc0+x;
}
write_com(address);
write_data(dat);
}
void display()
{
write_com(0x80+0x10);
for(num=0;num<15;num++)
{
write_data(table[num]);
Delay5us();
}
displayonechar(0,1,distance[bai]);
displayonechar(1,1,'.');
displayonechar(2,1,distance[shi]);
displayonechar(3,1,distance[ge]);
Delay100ms();
}
void main()
{
startmodule();
lcd_init();
ReadDataLCM();
timer0();
count();
display();
} 展开
展开全部
你好,我前段时间使用了HC-SR04超声波测距仪来测距离,用51单片机控制,LCD1602显示距离,精确到0.01cm,最大可测400cm。
我把我的程序给你,能做个测距和显示的参考。
若我们使用的是同一型号的超声波测距仪,可以回复我一起讨论。
希望我的回答能帮助到你。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
福州大禹电子科技
2024-12-16 广告
2024-12-16 广告
福州大禹电子科技有限公司成立于2008年自创办以来,专注超声产品的研发和产业化,始终坚持技术创新是企业发展动力的理念,运用最基础的物理声学原理,攻克了一系列卡脖子难题,成功打破了国外垄断,把超声技术运用到了工控、水利、市政、环保、医疗,海洋...
点击进入详情页
本回答由福州大禹电子科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询