关于return的用法,想调用完子函数后再次执行main函数
时间:10-02
整理:3721RD
点击:
源程序代码如下:请问调用完fire();子程序后怎么用return语句返回dao,或者还有其他的写法吗?
#include<stc12c5616ad.h>
#define uchar unsigned char
#define uint unsigned int
sbit L1=P1^0;
sbit L2=P1^1;
sbit Z=P1^2;
sbit R2=P1^3;
sbit R1=P1^4; //有墙0
sbit X1=P1^5; //黑线为1,遇白线为0
sbit HY11=P1^6; //无火为高,有火为低电平
sbit HY12=P1^7;
sbit HY13=P2^1;
sbit HY14=P2^7;
sbit HY21=P2^2;
sbit HY22=P2^3;
sbit HY23=P3^4;
sbit FENG=P2^5;
int i=0,a=0,b=0,c=0;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void PWM_zk0(float zk) //高电 平占得比例占空比为(Gao/255)
{
CCAP0H = CCAP0L =255*(100.0 - zk)/100.0;
CCAPM0 = 0x42;
CR=1;
}
void PWM_zk1(float zk)
{
CCAP1H = CCAP1L = 255*(100.0 - zk)/100.0;
CCAPM1 = 0x42;
CR=1;
}
void PWM_zk2(float zk)
{
CCAP2H = CCAP2L = 255*(100.0 - zk)/100.0;
CCAPM2 = 0x42;
CR=1;
}
void PWM_zk3(float zk)
{
CCAP3H = CCAP3L = 255*(100.0 - zk)/100.0;
CCAPM3 = 0x42;
CR=1;
}
void sgo() //进入房间,减速前进
{
PWM_zk0(47.0); //右 //38
PWM_zk3(0.0);
PWM_zk1(46.0); //左
PWM_zk2(0.0);
}
void sgo1() //进入4房间,减速前进
{
PWM_zk0(46.0); //右 //38
PWM_zk3(0.0);
PWM_zk1(54.0); //左
PWM_zk2(0.0);
}
void GO()
{
PWM_zk0(60.0); //右
PWM_zk3(0.0);
PWM_zk1(60.0); //左
PWM_zk2(0.0);
}
void BACK()
{
PWM_zk0(0); //右
PWM_zk3(50.0);
PWM_zk1(0); //左
PWM_zk2(50.0);
}
void STOP()
{
PWM_zk0(0);
PWM_zk3(0);
PWM_zk1(0);
PWM_zk2(0);
}
void Sright() //右拐小角度,快慢前进
{
PWM_zk0(39.0);
PWM_zk3(0.0);
PWM_zk1(55.0);
PWM_zk2(0.0);
}
void right()
{
PWM_zk0(0.0);
PWM_zk3(40.0);
PWM_zk1(65.0);
PWM_zk2(0.0);
}
void left()
{
PWM_zk0(60.0);
PWM_zk3(0.0);
PWM_zk1(39.0);
PWM_zk2(0.0);
}
void Dleft()
{
PWM_zk0(45.0);
PWM_zk3(0.0);
PWM_zk1(0.0);
PWM_zk2(51.5); //53
}
void fire1()
{
if(X1==1&&a==0)
{
{GO();delay(150);}
{if(R1==1)Dleft();if(L1==0)right();delay(100);} //对准吹灭
{ STOP();FENG=0;delay(25000);}
FENG=1;a=1;c=c+1;
}
else if(HY23==0) //右拐大
{right();delay(280);}
else if(HY11==0) //左拐大
{Dleft();delay(260);}
else if(HY22==0) //右拐小
{Sright();delay(140);}
else if(HY12==0) //左拐小
{left();delay(100);}
else if(HY14==0) //对准火焰时加速前进
{GO();}
else if(a==1&&HY14==1&&HY21==1&&HY12==1&&HY22==1) //风扇已经启动转身180度继续寻找
{right();delay(4000);return;} //能返回到main()函数不?
else
{sgo1();}
}
void migong2() //右手法则
{
if(Z==0)
{
BACK();delay(90);Dleft();delay(420);
}
else if( R2==0) //左转并前进
{
Dleft();
}
else if(R1==0)
{
GO();
}
else if(L1==0) //防止左右摆动大
{
Sright();delay(53);
}
else
{
right();sgo(); delay(10);
}
}
void fangjian4()
{
if (b==0)
{GO();delay(300);b=1;}
else if(R2==0&&R2==0)
{ {Dleft();delay(2400);}{while(X1==0)GO();}}
else if(X1==1)
{ {GO();delay(200);} {while(1)fire1();}}
else
{ migong2();}
}
void main()
{
CCON=0;
CL = 0;
CH = 0;
CMOD = 0x02;
CR = 1;
while(1)
{
if(X1==1&&a==1)
{
GO();delay(150);a=0;
}
else if(X1==1) //门口检测模式
{ i=i+1;
GO();delay(2500);{}delay(500); //进门 停顿一下
if(HY11==0||HY12==0||HY14==0||HY21==0||HY22==0||HY23==0) //有火则前进灭火
while(1)fire1(); //?return跳出 ?
{ BACK();delay(6300);}{Dleft();delay(3300);GO();delay(3500);} //无火则后退
}
else if(c==2)
{while(1)STOP();}
else if(i==3&&a==0)
{fangjian4();}
else //没有发现火焰,进入迷宫子程序
{
migong2();
}
}
}
#include<stc12c5616ad.h>
#define uchar unsigned char
#define uint unsigned int
sbit L1=P1^0;
sbit L2=P1^1;
sbit Z=P1^2;
sbit R2=P1^3;
sbit R1=P1^4; //有墙0
sbit X1=P1^5; //黑线为1,遇白线为0
sbit HY11=P1^6; //无火为高,有火为低电平
sbit HY12=P1^7;
sbit HY13=P2^1;
sbit HY14=P2^7;
sbit HY21=P2^2;
sbit HY22=P2^3;
sbit HY23=P3^4;
sbit FENG=P2^5;
int i=0,a=0,b=0,c=0;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void PWM_zk0(float zk) //高电 平占得比例占空比为(Gao/255)
{
CCAP0H = CCAP0L =255*(100.0 - zk)/100.0;
CCAPM0 = 0x42;
CR=1;
}
void PWM_zk1(float zk)
{
CCAP1H = CCAP1L = 255*(100.0 - zk)/100.0;
CCAPM1 = 0x42;
CR=1;
}
void PWM_zk2(float zk)
{
CCAP2H = CCAP2L = 255*(100.0 - zk)/100.0;
CCAPM2 = 0x42;
CR=1;
}
void PWM_zk3(float zk)
{
CCAP3H = CCAP3L = 255*(100.0 - zk)/100.0;
CCAPM3 = 0x42;
CR=1;
}
void sgo() //进入房间,减速前进
{
PWM_zk0(47.0); //右 //38
PWM_zk3(0.0);
PWM_zk1(46.0); //左
PWM_zk2(0.0);
}
void sgo1() //进入4房间,减速前进
{
PWM_zk0(46.0); //右 //38
PWM_zk3(0.0);
PWM_zk1(54.0); //左
PWM_zk2(0.0);
}
void GO()
{
PWM_zk0(60.0); //右
PWM_zk3(0.0);
PWM_zk1(60.0); //左
PWM_zk2(0.0);
}
void BACK()
{
PWM_zk0(0); //右
PWM_zk3(50.0);
PWM_zk1(0); //左
PWM_zk2(50.0);
}
void STOP()
{
PWM_zk0(0);
PWM_zk3(0);
PWM_zk1(0);
PWM_zk2(0);
}
void Sright() //右拐小角度,快慢前进
{
PWM_zk0(39.0);
PWM_zk3(0.0);
PWM_zk1(55.0);
PWM_zk2(0.0);
}
void right()
{
PWM_zk0(0.0);
PWM_zk3(40.0);
PWM_zk1(65.0);
PWM_zk2(0.0);
}
void left()
{
PWM_zk0(60.0);
PWM_zk3(0.0);
PWM_zk1(39.0);
PWM_zk2(0.0);
}
void Dleft()
{
PWM_zk0(45.0);
PWM_zk3(0.0);
PWM_zk1(0.0);
PWM_zk2(51.5); //53
}
void fire1()
{
if(X1==1&&a==0)
{
{GO();delay(150);}
{if(R1==1)Dleft();if(L1==0)right();delay(100);} //对准吹灭
{ STOP();FENG=0;delay(25000);}
FENG=1;a=1;c=c+1;
}
else if(HY23==0) //右拐大
{right();delay(280);}
else if(HY11==0) //左拐大
{Dleft();delay(260);}
else if(HY22==0) //右拐小
{Sright();delay(140);}
else if(HY12==0) //左拐小
{left();delay(100);}
else if(HY14==0) //对准火焰时加速前进
{GO();}
else if(a==1&&HY14==1&&HY21==1&&HY12==1&&HY22==1) //风扇已经启动转身180度继续寻找
{right();delay(4000);return;} //能返回到main()函数不?
else
{sgo1();}
}
void migong2() //右手法则
{
if(Z==0)
{
BACK();delay(90);Dleft();delay(420);
}
else if( R2==0) //左转并前进
{
Dleft();
}
else if(R1==0)
{
GO();
}
else if(L1==0) //防止左右摆动大
{
Sright();delay(53);
}
else
{
right();sgo(); delay(10);
}
}
void fangjian4()
{
if (b==0)
{GO();delay(300);b=1;}
else if(R2==0&&R2==0)
{ {Dleft();delay(2400);}{while(X1==0)GO();}}
else if(X1==1)
{ {GO();delay(200);} {while(1)fire1();}}
else
{ migong2();}
}
void main()
{
CCON=0;
CL = 0;
CH = 0;
CMOD = 0x02;
CR = 1;
while(1)
{
if(X1==1&&a==1)
{
GO();delay(150);a=0;
}
else if(X1==1) //门口检测模式
{ i=i+1;
GO();delay(2500);{}delay(500); //进门 停顿一下
if(HY11==0||HY12==0||HY14==0||HY21==0||HY22==0||HY23==0) //有火则前进灭火
while(1)fire1(); //?return跳出 ?
{ BACK();delay(6300);}{Dleft();delay(3300);GO();delay(3500);} //无火则后退
}
else if(c==2)
{while(1)STOP();}
else if(i==3&&a==0)
{fangjian4();}
else //没有发现火焰,进入迷宫子程序
{
migong2();
}
}
}
我建议在fire1()函数里使用while,而不是main中的 while(1)fire1例如
void fire1()
{
while(1)
{
……;
return; //调回main
}
}
如果还有什么不懂 请加我QQ:847704673
问题已经解决,谢谢回复