为什么我按键后只能正转 再按一下并不能像程序里面一样反转呢
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uint x;
uint y;
uint z;
uchar flag;
void delay(z){
for(x=z;x>0;x--)
for(y=110;y>0;y--); }
sbit s2=P3^4;
sbit s3=P3^5;
sbit s4=P3^6;
sbit s5=P3^7;
sbit a=P1^0;
sbit b=P1^1;
sbit c=P1^2;
sbit d=P1^3;
void start() {
while(1)
{a=1;
d=0;
delay(10);
a=0;
b=1;
delay(10);
b=0;
c=1;
delay(10);
c=0;
d=1;
delay(10);
}
}
void reverse()
{while(1){
d=1;
a=0;
delay(10);
d=0;
c=1;
delay(10);
c=0;
b=1;
delay(10);
b=0;
a=1;
delay(10);}}
void ok()
{if(s2==0)
{ delay(5);
if(s2==0)
{while(!s2);
{flag=~flag;
if(flag)
{start();}
else {
reverse();}
s2=1;
}
}
}
}
void main()
{
ok();
}
#define uchar unsigned char
#define uint unsigned int
uint x;
uint y;
uint z;
uchar flag;
void delay(z){
for(x=z;x>0;x--)
for(y=110;y>0;y--); }
sbit s2=P3^4;
sbit s3=P3^5;
sbit s4=P3^6;
sbit s5=P3^7;
sbit a=P1^0;
sbit b=P1^1;
sbit c=P1^2;
sbit d=P1^3;
void start() {
while(1)
{a=1;
d=0;
delay(10);
a=0;
b=1;
delay(10);
b=0;
c=1;
delay(10);
c=0;
d=1;
delay(10);
}
}
void reverse()
{while(1){
d=1;
a=0;
delay(10);
d=0;
c=1;
delay(10);
c=0;
b=1;
delay(10);
b=0;
a=1;
delay(10);}}
void ok()
{if(s2==0)
{ delay(5);
if(s2==0)
{while(!s2);
{flag=~flag;
if(flag)
{start();}
else {
reverse();}
s2=1;
}
}
}
}
void main()
{
ok();
}
因为你的start和severse函数都是死循环,一但进入其中一个程序就再也无法回到OK这个函数了,
楼上正解,感觉延时也要改一下
循环跳不出来啊
while(1)死循环呀!没跳出循环的检测