51单片机延时不起作用
做了个超声波避障小车,可是无论怎么延时都不起作用,求指教。。。。#include<reg52.h>#defineucharunsignedchar#defineuintu...
做了个超声波避障小车,可是无论怎么延时都不起作用,求指教。。。。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit Trig=P1^0;
sbit Echo=P1^1;
uchar time_H,time_L,z,time;
void delay(uint i)
{
uchar x,y;
for(x=i;x>0;x--)
for(y=110;y>0;y--);
}
void qian()
{
P0=0XFA;
}
void zuo()
{
P0=0xfB;
delay(9);
P0=0xff;
delay(40);
P0=0XFF;
delay(20000);
}
void main()
{
uint distance;
Trig=0;
EA=1;
TMOD=0x10;
while(1)
{
EA=0;
Trig=1;
delay(8);
Trig=0;
while(Echo==0);
EA=1;
TH1=0;
TL1=0;
TR1=1;
while(Echo==1);
TR1=0;
EX0=0;
time_H=TH1;
time_L=TL1;
time=time_H;
time=time<<8;
time=time|time_L;
distance=time/58;
if(distance<50)
{
zuo();
}
if(distance>=50)
{
qian();
}
}
}
void timer1() interrupt 3
{
TH1=0;
TL1=0;
} 展开
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit Trig=P1^0;
sbit Echo=P1^1;
uchar time_H,time_L,z,time;
void delay(uint i)
{
uchar x,y;
for(x=i;x>0;x--)
for(y=110;y>0;y--);
}
void qian()
{
P0=0XFA;
}
void zuo()
{
P0=0xfB;
delay(9);
P0=0xff;
delay(40);
P0=0XFF;
delay(20000);
}
void main()
{
uint distance;
Trig=0;
EA=1;
TMOD=0x10;
while(1)
{
EA=0;
Trig=1;
delay(8);
Trig=0;
while(Echo==0);
EA=1;
TH1=0;
TL1=0;
TR1=1;
while(Echo==1);
TR1=0;
EX0=0;
time_H=TH1;
time_L=TL1;
time=time_H;
time=time<<8;
time=time|time_L;
distance=time/58;
if(distance<50)
{
zuo();
}
if(distance>=50)
{
qian();
}
}
}
void timer1() interrupt 3
{
TH1=0;
TL1=0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询