#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit Z0 = P3^0;
sbit Z1 = P3^1;
sbit Y0 = P3^2;
sbit Y1 = P3^3;
sbit r2=P1^0;
sbit r1=P1^1;
sbit m=P1^2;
sbit l1=P1^3;
sbit l2=P1^4;
int a;
int b;
void delay10ms (uchar time) //10msÑÓʱ
{ int i;
int j;
for(i=0;i<time;i++)
for(j=0;j<0x390;j++);
}
unsigned char PWM(int x)
{
if(++a == 100)
{ a = 0;}
if(a<x)
{return 1;}
else
{return 0;}
}
void right()
{
Z0=0;
Z1=1;
Y0=0;
Y1=0;
}
void bigright()
{
Z0=0;
Z1=1;
Y0=1;
Y1=0;
}
void left()
{
Z0=0;
Z1=0;
Y0=0;
Y1=PWM(98);
}
void bigleft()
{
Z0=1;
Z1=0;
Y0=0;
Y1=PWM(98);
}
void ahead()
{
Z0=0;
Z1=1;
Y0=0;
Y1=PWM(98);
}
void back()
{
Z0=1;
Z1=0;
Y0=PWM(98);
Y1=0;
}
void stop()
{
Y0=0;
Y1=0;
Z0=0;
Z1=0;
}
void xunji()
{
if(l2==0&&l1==0&&m==1&&r2==0&&r1==0) //Ç°½ø
{ ahead(); }
else if((l2==0&&l1==0&&r2==1&&m==1)||(l2==0&&l1==0&&r2==1&&m==0)||(r1==1&&l2==0&&l1==0)||(r1==1&&r2==1&&l2==0&&l1==0)) //ÓÒת
{ right(); }
else if((r2==0&&r1==0&&l2==1&&m==1)||(r2==0&&r1==0&&l2==1&&m==0)||(l1==1&&r2==0&&r1==0)||(l1==1&&l2==1&&r2==0&&r1==0)) //×óת
{ left(); }
else if ((l1==0&&l2==1&&m==0&&r2==0&&r1==0)||(l1==1&&l2==0&&m==0&&r2==0&&r1==0))
{ left(); }
// else if((l1==0&&l2==0&&m==0&&r2==1&&r1==0)||(l1==0&&l2==0&&m==0&&r2==0&&r1==1))
// {right(); }
else if((m==1&&r1==1&&r2==1&&l1==0&&l2==0)||(m==1&&r1==1&&r2==1&&l1==1&&l2==0))
{
bigright(); }
else if((m==1&&l1==1&&l2==1&&r1==0&&r2==0)||(m==1&&r1==1&&r2==0&&l1==1&&l2==1))
{
bigleft();}
// else if(m==0&&r1==0&&r2==0&&l1==0&&l2==0)
// { stop();}
}
void main()
{ EA = 1;
TMOD = 0X01;
TH0 =( 65535 - 1000)/256;
TL0 =( 65535 - 1000)%256;
ET0 = 1;
TR0 = 1;
while (1)
{
xunji();
}
}
void t_40ms() interrupt 1
{
TH0 =( 65535 - 1000)/256;
TL0 =( 65535 - 1000)%256;
a++;
} |