|
写了一个关于超声波测距的程序,但是程序烧录到单片机里面后,一直没反应,有哪位大神知道原因的啊!
#include<reg52.h>
sbit Trig = P1^6;
sbit Echo = P1^7;
sbit LED = P2^0;
#define LED_PORT P0
typedef unsigned short int u8;
int Distance;
void inti_time()
{
TMOD = 0x20;//采用定时器1定时方式
TH1 = 0;
TL1 = 0;
}
void delay_us(u8* temp_data)
{
for(;*temp_data > 0; *temp_data- -);
}
void ultrasonic ()
{
u8 time = 5;
inti_time();
Trig = 1;
delay_us(&time);
Trig = 0;
while (!Echo);
TR1 = 1;
while (Echo)
{
TR1 = 0;
if (TH1 > 0x80)
break;
}
Distance = ((TH1 << 8) | TL1); // 计算脉冲的高电平时间,然后换算成距离
Distance = Distance * 1.72;
if (Distance < 400) // 假设400是一个合理的距离阈值
LED = 1; // 点亮LED指示灯
else
LED = 0;
}
void main ()
{
while(1)
{
void ultrasonic ();
}
} |
|