新手上路,很多不足的地方請大神指點。
#include<AT89x51.H>
sbit In8=P1^0;
sbit In7=P1^1;
sbit In6=P1^2;
sbit In5=P1^3;
sbit In4=P1^4;
sbit In3=P1^5;
sbit In2=P1^6;
sbit In1=P1^7;
sbit out1=P3^4;
sbit out2=P3^5;
sbit out3=P3^6;
sbit out4=P3^7;
int q1,q,q2;
void dianji(int z)
{
if(z == 0)
{
out1=1;
out2=0;
out3=1;
out4=0;
}
if(z == 4)
{
q1 = 5; //轉(zhuǎn)向系數(shù) 轉(zhuǎn)彎的角度
while(q1--)
{
out1=1;
out2=0;
out3=0;
out4=0;
}
}
if(z == 5)
{
q1 = 5; //轉(zhuǎn)向系數(shù)
while(q1--)
{
out1=0;
out2=0;
out3=1;
out4=0;
}
}
if(z == 3 )
{
q1 = 15; //轉(zhuǎn)向系數(shù)
while(q1--)
{
out1=1;
out2=0;
out3=0;
out4=0;
}
}
if(z == 6)
{
q1 = 15; //轉(zhuǎn)向系數(shù)
while(q1--)
{
out1=0;
out2=0;
out3=1;
out4=0;
}
}
if(z == 2)
{
q1 = 25; //轉(zhuǎn)向系數(shù)
while(q1--)
{
out1=1;
out2=0;
out3=0;
out4=0;
}
}
if(z == 7)
{
q1 = 25; //轉(zhuǎn)向系數(shù)
while(q1--)
{
out1=0;
out2=0;
out3=1;
out4=0;
}
}
if(z == 1)
{
q1 = 1; //死循環(huán)
while(q1)
{
out1=1;
out2=0;
out3=0;
out4=0;
if(In2 == 1||In3 == 1||In4 == 1||In5 == 1||In6 == 1||In7 == 1) q1 = 0;
}
}
if(z == 8)
{
q1 = 1; //死循環(huán)
while(q1)
{
out1=0;
out2=0;
out3=1;
out4=0;
if(In2 == 1||In3 == 1||In4 == 1||In5 == 1||In6 == 1||In7 == 1) q1 = 0;
}
}
}
void main()
{
while(1)
{
q2 = 1; //可調(diào)速 與下方q2比值為速度 1:0為全速
while(q2--)
{
q=0;
if(In1 == 1) q = 1;
if(In2 == 1) q = 2;
if(In3 == 1) q = 3;
if(In4 == 1) q = 4;
if(In5 == 1) q = 5;
if(In6 == 1) q = 6;
if(In7 == 1) q = 7;
if(In8 == 1) q = 8;
dianji(q);
}
q2 = 0; //可調(diào)速
while(q2--)
{
out1=0;
out2=0;
out3=0;
out4=0;
}
} }
|