我总共只有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ÖжϼÆʱ
{
TH0=0;
TL0=0;
}
main()
{
TMOD=0x01; //Ñ¡Ôñģʽ1£¬Îª16룬¿É¼Æ65536us
EA=0; //ÏȹرÕ×ÜÖжÏ
TR0=0; //¹ØT0ÖжÏ
L1602_init(); //1602³õʼ»¯
while(1)
{
trig=0; //ÇåÁã
echo=0;
trig=1; //´¥·¢trig£¬·¢ËÍÐźÅ
delay1(2); //26us
trig=0; //¹Ø±Õ´¥·¢£¬·¢ËͽáÊø
while(echo==0); //µÈ´ýechoµÃµ½¸ßµçƽÐźţ¬·ñÔòÒ»Ö±µÈ´ý
EA=1; //¿ªÖжÏ
TR0=1; //¿ªT0Öжϣ¬¿ªÊ¼¼Æʱ
while(echo==1); //µÈ´ýecho½ÓÊÕµ½ÐźűäΪµÍµçƽ£¬·ñÔòÒ»Ö±µÈ´ý
EA=0; //¹ØÖжÏ
TR0=0; //¹ØT0Öжϣ¬Í£Ö¹¼Æʱ
t=TH0*256+TL0; //¸ß°ËλTH0ºÍµÍ°ËλTL0Ϊ¼ÆµÃµÄʱ¼ä
L=t*0.17; //²»¿¼ÂÇζȺÍÆøѹ¶ÔÉù²¨´«ÊäËٶȵÄÓ°Ï죬ÓÉʱ¼äËã¾àÀ룬µÃµ½µÄÊÇL£¬µ¥Î»ÊÇmm
TH0=0; //ÇåÁã
TL0=0;
trig=0;
echo=0;
m=L/1000; //¸³Öµ
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 == ' ')||(b==17)) break;
wdata(*p);
p++;
}
}
***********************************************************************
一周热门 更多>