#include<reg52.h>
sbit IN1 = P0^0;
sbit IN2 = P0^1;
sbit IN3 = P0^2;
sbit IN4 = P0^3;
sbit PWM1 = P0^5;
sbit PWM2 = P0^4;
sbit L1 = P3^5;
sbit G1 = P3^4;
sbit G2 = P3^3;
sbit R1 = P3^2;
unsigned char count1 = 0;
unsigned char count2 = 0;
void left1()//左前
{
IN1 = 1;IN2 = 0;
}
void left2()//左退
{
IN1 = 0;IN2 = 1;
}
void right1()//右前
{
IN3 = 1;IN4 = 0;
}
void right2()//右退
{
IN3 = 0;IN4 = 1;
}
void sleep1(int c,int d)
{ if(c<=d)
PWM1 = 1;
else PWM1 = 0;
}
void sleep2(int c,int d)
{ if(c<=d)
PWM2 = 1;
else PWM2 = 0;
}
void str(int c1 ,int d1,int c2, int d2)
{
right1();
left1();
sleep1(c1,d1);
sleep2(c2,d2);
}
void left(int c1, int d1,int c2 ,int d2)
{
right1();
left2();
sleep1(c1,d1);
sleep2(c2,d2);
}
void right(int c1, int d1,int c2, int d2)
{
right2();
left1();
sleep1(c1,d1);
sleep2(c2,d2);
}
void main()
{
TMOD = 0x11;
TH0 = (65536-1000)/256;
TL0 = (65536-1000)%256;
EA = 1;
TR0 = 1;
ET0 = 1;
TH1 = (65536-1000)/256;
TL1 = (65536-1000)%256;
TR1 = 1;
ET1 = 1;
while(1)
{{ if(L1==0&&G1==0&&G2==0&&R1==0)
str(count1, 500,count2 ,500);
}
{ if(L1==0&&G1==0&&G2==0&&R1==1)
left(count1, 200,count2 ,700);
}
{ if(L1==0&&G1==1&&G2==1&&R1==0)
str(count1 ,500,count2 ,500);
}
{ if(L1==1&&G1==0&&G2==0&&R1==0)
right(count1, 700,count2 ,200);
}
}
}
void time0() interrupt 1
{ TH0 = (65536-1000)/256;
TL0 = (65536-1000)%256;
count1++;
if(count1 >= 1000)
count1 = 0;
}
void time1() interrupt 1
{ TH1 = (65536-1000)/256;
TL1 = (65536-1000)%256;
count2++;
if(count2 >= 1000)
count2 = 0;
}