检查了一下,运行时到while(echo);停止了,能确定引脚没有接错。

下面是全部代码:
#include <REGX52.H>
sbit ENB=P2^0;
sbit IN4=P2^1;
sbit IN3=P2^2;
sbit IN1=P2^3;
sbit IN2=P2^4;
sbit ENA=P2^6; //
sbit echo=P3^4;
sbit trig=P3^3; //
unsigned long Time;
unsigned int Distance;
void Timer0Init(void) //由0开始计时@11.0592MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TF0 = 0; //清除TF0标志
EA=1;
}
void Delay15us() //@11.0592MHz
{
unsigned char i;
i = 4;
while (--i);
}
void Delay10ms(xms) //@11.0592MHz
{
while(xms)
{
unsigned char i, j;
i = 18;
j = 235;
do
{
while (--j);
} while (--i);
xms--;
}
}
//void Measure()
//{
//// unsigned int Timeout = 0;
// trig=1;
// Delay15us();
// trig=0;
//// while(!echo && Timeout++ < 50000);
//// if(Timeout >= 50000) return;
////
//// TR0 = 1;
//// Timeout = 0;
//// while(echo && Timeout++ < 50000);
//// TR0 = 0;
//
// while(echo)
// {
// TR0=1;
// break;
// }
//// while(echo);
//// TR0=0;
//// Time=(256*TH0+TL0)*120000/110592;
//// TH0=TL0=0;
//// Distance=Time*0.17; //mm
// if(echo)
// {
// TR0=0;
// Time=(256*TH0+TL0)*120000/110592;
// TH0=TL0=0;
// Distance=Time*0.17; //mm
// }
//// SendData[0]=Distance/1000 +'0';
//// SendData[1]=Distance%1000/100 +'0';
//// SendData[2]=Distance%100/10 +'0';
//// SendData[3]='.';
//// SendData[4]=Distance%10 +'0';
//// SendData[5]='c';
//// SendData[6]='m';
//// TxStr(SendData);
//// TxStr("\r\n");
//}
unsigned char SendData[10]={0x00};
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
TMOD &= 0x0F; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
TL1 = 0xFD; //设定定时初值
TH1 = 0xFD; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES=1;
}
void TxStr(unsigned char *Str)
{
ES=0;
while(*Str!=0)
{
SBUF = *Str;
while(TI==0);
TI=0;
Str++;
}
ES=1;
}
void Measure()
{
unsigned int Timeout = 0;
trig=1;
Delay15us();
trig=0;
while(!echo && Timeout++ < 50000);
if(Timeout >= 50000) return;
TR0 = 1;
Timeout = 0;
while(echo && Timeout++ < 50000);
TR0 = 0;
while(!echo);
TR0=1;
while(echo);
TR0=0;
Time=(256*TH0+TL0)*120000/110592;
TH0=TL0=0;
Distance=Time*0.17; //mm
SendData[0]=Distance/1000 +'0';
SendData[1]=Distance%1000/100 +'0';
SendData[2]=Distance%100/10 +'0';
SendData[3]='.';
SendData[4]=Distance%10 +'0';
SendData[5]='c';
SendData[6]='m';
TxStr(SendData);
TxStr("\r\n");
}
void main()
{
Timer0Init();
echo=1;
trig=0;
Distance=0;
P2_7=1;
ENB=1; //M轮 目前正转
IN4=1;
IN3=0;
ENA=1; //N轮 目前正转
IN2=0;
IN1=1;
while(1)
{
if(Distance<=1)
{
P2_7=~P2_7;
IN4=0;
IN3=1;
}
else
{
P2_5=1;
IN4=1;
IN3=0;
}
Measure();
}
}
希望大家有时间能帮忙看一下到底那里出问题了,以及要怎么处理