qq_38944028 于 2017.09.08 11:09 提问

4个回答

qq_38944028   2017.09.08 11:10

#include#define uint unsigned int #define uchar unsigned charuchar num=10;uint distancez=0,distancey=0,t=0;sbit trigz=P1^2; //左边超声波模块sbit echoz=P1^3;//sbit zled=P1^6;//sbit zled_=P1^7;sbit trigy=P1^1; //右边超声波模块sbit echoy=P1^0;//sbit yled=P1^4;//sbit yled_=P1^5;sbit zq=P0^0 ; //左前sbit zh=P0^1; //左后sbit yq=P0^2; //右前sbit yh=P0^3; //右后void turnleft(){ zq=1;// zq=0; yq=0;}void turnright(){ yq=1;// yq=0; zq=0;}void run(){ zq=0; yq=0;}int initz() //左边离障碍物的距离{ trigz=0; echoz=0; t=0; //清楚计数器 trigz=1; //左边发出超声波 while(num>0)num--; trigz=0; while(!echoz); //发完 TR0=1; while(echoz); //计时，计算距离 ，关闭计数器 TR0=0; distancez=t/1.75 ; return distancez ;}int inity() //右边离障碍物的距离{ trigy=0; echoy=0; t=0; //清楚计数器 trigy=1; //右边发出超声波 while(num>0)num--; trigy=0; while(!echoy); //发完 TR0=1; while(echoy); //计时，计算距离 ,关闭计数器 TR0=0; distancey=t/1.75 ; return distancey ;}void timerinit(){ TH0=(65535-50)/256; TL1=(65535-50)%256; EA=1; ET0=1; } void main(){ while(1) { timerinit(); run(); initz(); inity(); while(distancez<20|distancey<20) { initz(); //障碍物在左边 if(distancez<20) { while(distancez<20) { turnleft(); initz(); } } run(); inity(); //障碍物在右边 if(distancey<20) { while(distancey<20) { turnright(); inity(); } } } run(); }}void Timer0(void) interrupt 1 //distance=t/1.75(cm){ TH0=(65535-50)/256; TL1=(65535-50)%256; t++;}

qq_38944028   2017.09.08 11:23

#include
#define uint unsigned int
#define uchar unsigned char
uchar num=10;
uint distancez=0,distancey=0,t=0;
sbit trigz=P1^2; //左边超声波模块
sbit echoz=P1^3;
//sbit zled=P1^6;
//sbit zled_=P1^7;

sbit trigy=P1^1; //右边超声波模块
sbit echoy=P1^0;
//sbit yled=P1^4;
//sbit yled_=P1^5;

sbit zq=P0^0 ; //左前
sbit zh=P0^1; //左后
sbit yq=P0^2; //右前
sbit yh=P0^3; //右后

void turnleft()
{
zq=1;
// zq=0;
yq=0;
}
void turnright()
{
yq=1;
// yq=0;
zq=0;
}

void run()
{
zq=0;
yq=0;
}

int initz() //左边离障碍物的距离
{
trigz=0;
echoz=0;
t=0; //清楚计数器
trigz=1; //左边发出超声波
while(num>0)num--;
trigz=0;

``````while(!echoz);                  //发完

TR0=1;
while(echoz);                  //计时，计算距离  ，关闭计数器
TR0=0;
distancez=t/1.75 ;
return distancez ;
``````

}
int inity() //右边离障碍物的距离
{
trigy=0;
echoy=0;
t=0; //清楚计数器

``````trigy=1;                     //右边发出超声波
while(num>0)num--;
trigy=0;

while(!echoy);                  //发完

TR0=1;
while(echoy);                  //计时，计算距离    ,关闭计数器
TR0=0;
distancey=t/1.75 ;
return distancey ;
``````

}

void timerinit()
{
TH0=(65535-50)/256;
TL1=(65535-50)%256;

``````EA=1;
ET0=1;
``````

}

void main()
{

``````while(1)
{
timerinit();
run();
initz();
inity();
while(distancez<20|distancey<20)
{
initz();                   //障碍物在左边
if(distancez<20)
{
while(distancez<20)
{
turnleft();
initz();
}
}
run();

inity();                   //障碍物在右边
if(distancey<20)
{
while(distancey<20)
{
turnright();
inity();

}
}
}
run();
}
``````

}

void Timer0(void) interrupt 1 //distance=t/1.75(cm)
{
TH0=(65535-50)/256;
TL1=(65535-50)%256;
t++;
}

qq_38944028   2017.09.08 11:23

#include
#define uint unsigned int
#define uchar unsigned char
uchar num=10;
uint distancez=0,distancey=0,t=0;
sbit trigz=P1^2; //左边超声波模块
sbit echoz=P1^3;
//sbit zled=P1^6;
//sbit zled_=P1^7;

sbit trigy=P1^1; //右边超声波模块
sbit echoy=P1^0;
//sbit yled=P1^4;
//sbit yled_=P1^5;

sbit zq=P0^0 ; //左前
sbit zh=P0^1; //左后
sbit yq=P0^2; //右前
sbit yh=P0^3; //右后

void turnleft()
{
zq=1;
// zq=0;
yq=0;
}
void turnright()
{
yq=1;
// yq=0;
zq=0;
}

void run()
{
zq=0;
yq=0;
}

int initz() //左边离障碍物的距离
{
trigz=0;
echoz=0;
t=0; //清楚计数器
trigz=1; //左边发出超声波
while(num>0)num--;
trigz=0;

``````while(!echoz);                  //发完

TR0=1;
while(echoz);                  //计时，计算距离  ，关闭计数器
TR0=0;
distancez=t/1.75 ;
return distancez ;
``````

}
int inity() //右边离障碍物的距离
{
trigy=0;
echoy=0;
t=0; //清楚计数器

``````trigy=1;                     //右边发出超声波
while(num>0)num--;
trigy=0;

while(!echoy);                  //发完

TR0=1;
while(echoy);                  //计时，计算距离    ,关闭计数器
TR0=0;
distancey=t/1.75 ;
return distancey ;
``````

}

void timerinit()
{
TH0=(65535-50)/256;
TL1=(65535-50)%256;

``````EA=1;
ET0=1;
``````

}

void main()
{

``````while(1)
{
timerinit();
run();
initz();
inity();
while(distancez<20|distancey<20)
{
initz();                   //障碍物在左边
if(distancez<20)
{
while(distancez<20)
{
turnleft();
initz();
}
}
run();

inity();                   //障碍物在右边
if(distancey<20)
{
while(distancey<20)
{
turnright();
inity();

}
}
}
run();
}
``````

}

void Timer0(void) interrupt 1 //distance=t/1.75(cm)
{
TH0=(65535-50)/256;
TL1=(65535-50)%256;
t++;
}

qq_38204686   2017.09.08 12:35

while(num>0)num--;
num全局变量 减完以后 需要重新赋值么