#include<reg52.h>
sbit trig = P2^0;
sbit echo = P2^1;
sbit LED0 = P1^0;
sbit LED1 = P1^1;
void Delay(unsigned int n)
{
unsigned char y;
unsigned int x;
for(x=0;x<n;x++)
{
for(y=0;y<100;y++)
{}
}
}
main()
{
unsigned long load;
TMOD = 0x01; //定时器0 工作方式1 16位
TH0 = 0x0;
TL0 = 0x0;
TR0 = 0;
trig = 0;
while(1)
{
echo = 1;
Delay(1);
trig = 1;
Delay(20);
trig = 0;
while(!echo)
TR0 = 1;
while(echo)
TR0 = 0;
load = TH0 * 256 + TL0;
if(load >500)
{
LED0 = 0; //时间大于500 * 12 /(11059200 * 2)LED0亮
Delay(2000);
LED0 = 1;
}
else
{
LED1 = 0; //时间小于,LED1亮
Delay(2000);
LED1 = 1;
}
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0;
TL0 = 0;
}
TH0 = 0;
TL0 = 0;
}
想实现的功能写出来了,烧好后,LED1一直亮,LED2却不亮,而且距离不同闪的频率不同,好像延时不管用,怎么回事啊
51的IO口做输入口不是应该要保证为1才能读数?
追答要不你试试把echo=1删掉,下载一下看看还行不行
追问不行啊,原来说错了,不是LED2是LED0,。还和原来现象一样