STC89C52RC HC-SR04 LCD1602A 制作超声波测距器下载到单片机后运行不正确
时间:10-02
整理:3721RD
点击:
我总共只有12积分,虽然不多,全部悬赏了,请各位大哥小哥大姐们都来帮一下我这个30多岁刚接触单片机的爱好者!
大家可否帮我看看下面程序为什么不能运行在我的单片机上,这个程序是前辈写的,我只是改了一下端口而已,看在我一把老骨头的份上,请各位帮我耐心地看一下,谢谢了!
我的单片机结构图:
运行后效果:
************************************************************************************
下面是主程序:
#include<reg52.h>
#include<1602.h>
sbit trig = P2^0;
sbit echo = P2^1;
int m,dm,cm,mm,L,t;
void delay1(int q)
{
int i,j;
for(i=0;i<13;i++)
for(j=0;j<q;j++)
;
}
void Timer0 (void) interrupt 1 //T0?D????ê±
{
TH0=0;
TL0=0;
}
main()
{
TMOD=0x01; //?????£ê?1£??a16??£??é??65536us
EA=0; //?è1?±?×ü?D??
TR0=0; //1?T0?D??
L1602_init(); //16023?ê??ˉ
while(1)
{
trig=0; //??á?
echo=0;
trig=1; //′¥·¢trig£?·¢?íD?o?
delay1(2); //26us
trig=0; //1?±?′¥·¢£?·¢?í?áê?
while(echo==0); //μè′yechoμ?μ???μ???D?o?£?·??òò??±μè′y
EA=1; //?a?D??
TR0=1; //?aT0?D??£??aê???ê±
while(echo==1); //μè′yecho?óê?μ?D?o?±??aμíμ???£?·??òò??±μè′y
EA=0; //1??D??
TR0=0; //1?T0?D??£?í£?1??ê±
t=TH0*256+TL0; //??°???TH0oíμí°???TL0?a??μ?μ?ê±??
L=t*0.17; //2????????èoí???1??éù2¨′?ê??ù?èμ?ó°?ì£?óéê±?????àà?£?μ?μ?μ?ê?L£?μ¥??ê?mm
TH0=0; //??á?
TL0=0;
trig=0;
echo=0;
m=L/1000; //?3?μ
dm=(L/100)%10;
cm=(L/10)%10;
mm=L%10;
L1602_string(1,1,"distance:"); //??ê?
L1602_char(2,10,m+0x30);delay1(20);
L1602_char(2,11,dm+0x30);delay1(20);
L1602_char(2,12,cm+0x30);delay1(20);
L1602_char(2,13,'.');delay1(20);
L1602_char(2,14,mm+0x30);delay1(20);
L1602_char(2,15,'c');delay1(20);
L1602_char(2,16,'m');delay1(20);
}
}
********************************************************************************
下面是1602.h的内容:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit E=P0^2;
sbit RW=P0^3;
sbit RS=P0^4;
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++;
}
}
***********************************************************************
大家可否帮我看看下面程序为什么不能运行在我的单片机上,这个程序是前辈写的,我只是改了一下端口而已,看在我一把老骨头的份上,请各位帮我耐心地看一下,谢谢了!
我的单片机结构图:
运行后效果:
************************************************************************************
下面是主程序:
#include<reg52.h>
#include<1602.h>
sbit trig = P2^0;
sbit echo = P2^1;
int m,dm,cm,mm,L,t;
void delay1(int q)
{
int i,j;
for(i=0;i<13;i++)
for(j=0;j<q;j++)
;
}
void Timer0 (void) interrupt 1 //T0?D????ê±
{
TH0=0;
TL0=0;
}
main()
{
TMOD=0x01; //?????£ê?1£??a16??£??é??65536us
EA=0; //?è1?±?×ü?D??
TR0=0; //1?T0?D??
L1602_init(); //16023?ê??ˉ
while(1)
{
trig=0; //??á?
echo=0;
trig=1; //′¥·¢trig£?·¢?íD?o?
delay1(2); //26us
trig=0; //1?±?′¥·¢£?·¢?í?áê?
while(echo==0); //μè′yechoμ?μ???μ???D?o?£?·??òò??±μè′y
EA=1; //?a?D??
TR0=1; //?aT0?D??£??aê???ê±
while(echo==1); //μè′yecho?óê?μ?D?o?±??aμíμ???£?·??òò??±μè′y
EA=0; //1??D??
TR0=0; //1?T0?D??£?í£?1??ê±
t=TH0*256+TL0; //??°???TH0oíμí°???TL0?a??μ?μ?ê±??
L=t*0.17; //2????????èoí???1??éù2¨′?ê??ù?èμ?ó°?ì£?óéê±?????àà?£?μ?μ?μ?ê?L£?μ¥??ê?mm
TH0=0; //??á?
TL0=0;
trig=0;
echo=0;
m=L/1000; //?3?μ
dm=(L/100)%10;
cm=(L/10)%10;
mm=L%10;
L1602_string(1,1,"distance:"); //??ê?
L1602_char(2,10,m+0x30);delay1(20);
L1602_char(2,11,dm+0x30);delay1(20);
L1602_char(2,12,cm+0x30);delay1(20);
L1602_char(2,13,'.');delay1(20);
L1602_char(2,14,mm+0x30);delay1(20);
L1602_char(2,15,'c');delay1(20);
L1602_char(2,16,'m');delay1(20);
}
}
********************************************************************************
下面是1602.h的内容:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit E=P0^2;
sbit RW=P0^3;
sbit RS=P0^4;
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++;
}
}
***********************************************************************
真的没有人回复啊,救救我吧
不能沉了,直接发我可靠的程序也行啊
你原理图用的是用的是P1口做的1602的数据口,而你程序里的用的是P0口做数据口,这能出来吗?
1602并行通信不对,原理图上是P2你的程序是用的P0
先看看1602的驱动程序能不能用?~再试下用串口将超声波的数据读出?如果都行~那再将两个结合起来 ?~
很好的文档,值得收藏
学习学习