#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
uchar receiveDate,time1,time2,time3,time4;
uint e;
sbit ena=P1^0;
sbit enb=P1^5;
sbit enc=P0^5;
sbit end=P0^0;
sbit a1=P1^1;
sbit a2=P1^2; //右前轮
sbit b1=P1^3;
sbit b2=P1^4; //左前轮
sbit c1=P0^4;
sbit c2=P0^3;//左后轮
sbit d1=P0^2;
sbit d2=P0^1; //右后轮
void Delay_1ms(uint t)
{
uchar i,j;
for(i=0;i<t;i++)
for(j=0;j<=148;j++);
}
void Com_Int(void) interrupt 4
{
receiveDate=SBUF;
e=SBUF;
RI=0;
SBUF=receiveDate;
while(!TI);
TI=0;
}
void Init(void)
{
TMOD=0x21;
SCON=0X50;
PCON=0X80;
TH1=0XFA;
TL1=0XFA;
TR1=1;
ES=1;
EA=1;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
ET0=1;
}
void PWM1(uint a)
{
if(time1==100){time1=0;}
if(time1<a){ena=1;}
else{ena=0;}
}
void PWM2(uint b)
{
if(time2==100){time2=0;}
if(time2<b){enb=1;}
else{enb=0;}
}
void PWM3(uint c)
{
if(time3==100){time3=0;}
if(time3<c){enc=1;}
else{enc=0;}
}
void PWM4(uint d)
{
if(time4==100){time4=0;}
if(time4<d){end=1;}
else{end=0;}
}
void go()
{
a1=1;a2=0;PWM1(80);//右前轮
b1=1;b2=0;PWM2(80);//左前轮
c1=1;c2=0;PWM3(80);//左后轮
d1=1;d2=0;PWM4(80);//右后轮
}
void brack()
{
a1=0;a2=1;PWM1(20);
b1=0;b2=1;PWM2(20);
c1=0;c2=1;PWM3(20);
d1=0;d2=1;PWM4(20);
}
void right()
{
a1=1;a2=0;PWM1(50);
b1=1;b2=0;PWM2(50);
c1=0;c2=1;PWM3(50);
d1=1;d2=0;PWM4(50);
}
void left()
{
a1=1;a2=0;PWM1(80);
b1=1;b2=0;PWM2(70);
c1=1;c2=0;PWM3(80);
d1=0;d2=1;PWM4(50);
}
void main()
{
Delay_1ms(100);
Init();
while(1)
{
switch(e)
{
case 0x00:
a1=0;a2=0;b1=0;b2=0;c1=0;c2=0;d1=0;d2=0; //停止
break;
case 0x01:
go();
break;
case 0x02:
brack();
break;
case 0x03:
right();
break;
case 0x04:
left();
break;
}
}
}
void time0() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
time1++;
time2++;
time3++;
time4++;
}
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
uchar receiveDate,time1,time2,time3,time4;
uint e;
sbit ena=P1^0;
sbit enb=P1^5;
sbit enc=P0^5;
sbit end=P0^0;
sbit a1=P1^1;
sbit a2=P1^2; //右前轮
sbit b1=P1^3;
sbit b2=P1^4; //左前轮
sbit c1=P0^4;
sbit c2=P0^3;//左后轮
sbit d1=P0^2;
sbit d2=P0^1; //右后轮
void Delay_1ms(uint t)
{
uchar i,j;
for(i=0;i<t;i++)
for(j=0;j<=148;j++);
}
void Com_Int(void) interrupt 4
{
receiveDate=SBUF;
e=SBUF;
RI=0;
SBUF=receiveDate;
while(!TI);
TI=0;
}
void Init(void)
{
TMOD=0x21;
SCON=0X50;
PCON=0X80;
TH1=0XFA;
TL1=0XFA;
TR1=1;
ES=1;
EA=1;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
ET0=1;
}
void PWM1(uint a)
{
if(time1==100){time1=0;}
if(time1<a){ena=1;}
else{ena=0;}
}
void PWM2(uint b)
{
if(time2==100){time2=0;}
if(time2<b){enb=1;}
else{enb=0;}
}
void PWM3(uint c)
{
if(time3==100){time3=0;}
if(time3<c){enc=1;}
else{enc=0;}
}
void PWM4(uint d)
{
if(time4==100){time4=0;}
if(time4<d){end=1;}
else{end=0;}
}
void go()
{
a1=1;a2=0;PWM1(80);//右前轮
b1=1;b2=0;PWM2(80);//左前轮
c1=1;c2=0;PWM3(80);//左后轮
d1=1;d2=0;PWM4(80);//右后轮
}
void brack()
{
a1=0;a2=1;PWM1(20);
b1=0;b2=1;PWM2(20);
c1=0;c2=1;PWM3(20);
d1=0;d2=1;PWM4(20);
}
void right()
{
a1=1;a2=0;PWM1(50);
b1=1;b2=0;PWM2(50);
c1=0;c2=1;PWM3(50);
d1=1;d2=0;PWM4(50);
}
void left()
{
a1=1;a2=0;PWM1(80);
b1=1;b2=0;PWM2(70);
c1=1;c2=0;PWM3(80);
d1=0;d2=1;PWM4(50);
}
void main()
{
Delay_1ms(100);
Init();
while(1)
{
switch(e)
{
case 0x00:
a1=0;a2=0;b1=0;b2=0;c1=0;c2=0;d1=0;d2=0; //停止
break;
case 0x01:
go();
break;
case 0x02:
brack();
break;
case 0x03:
right();
break;
case 0x04:
left();
break;
}
}
}
void time0() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
time1++;
time2++;
time3++;
time4++;
}