#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char zkb1=0; //**左前边电机的占空比**//
unsigned char zkb2=0; //**右前边电机的占空比**//
unsigned char zkb3=0; //**左后边电机的占空比**//
unsigned char zkb4=0; //**右后边电机的占空比**//
unsigned char t=0; //**定时器中断计数器**//
//***传感器***//
sbit LSEN1=P2^0;
sbit LSEN2=P2^1;
sbit LSEN3=P2^2;
sbit RSEN1=P2^3;
sbit RSEN2=P2^4;
sbit RSEN3=P2^5;
//***电机***//IN1,2,3,4前轮,IN5,6,7,8后轮//
sbit IN1=P1^0;//左前//
sbit IN2=P1^1;
sbit IN3=P1^2;//右前//
sbit IN4=P1^3;
sbit IN5=P1^4;//左后//
sbit IN6=P1^5;
sbit IN7=P1^6;//右后//
sbit IN8=P1^7;
sbit QENA=P0^0;//QENA,B前驱动使能,HENA,B后驱动使能//
sbit QENB=P0^1;
sbit HENA=P0^2;
sbit HENB=P0^3;
int a;
a=1;
int i,j;//延时函数//
void delay(int z)
{
for(i=0;i<z;i++)
for(j=0;j<110;j++);
}
void Timer0() interrupt 1
{
TH0=(65536-100)/256; // 装入定时器的初值 10ms定时 //
TL0=(65536-100)%256;
if(t<zkb1) QENA=1;
else QENA=0;
if(t<zkb2) QENB=1;
else QENB=0;
if(t<zkb3) HENA=1;
else HENA=0;
if(t<zkb4) HENB=1;
else HENB=0;
t++;
if(t>=100)
{
t=0;
}
}
main ()
{
//******初始化定时器******//
TMOD=0x01; // 设定T0的工作模式为1 //
TH0=(65536-100)/256; // 装入定时器的初值 10ms定时 //
TL0=(65536-100)%256;
EA=1; // 开中断 //
ET0=1; //定时器0允许中断 //
TR0=1; // 启动定时器0 //
delay(100);//直行//
zkb1=60;
zkb2=60;
zkb3=60;
zkb4=60;
IN1=0;
IN2=1;
IN3=0;
IN4=1;
IN5=0;
IN6=1;
IN7=0;
IN8=1;
delay(1000);
while (1)
if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==1))//直行//
{
zkb1=60;
zkb2=60;
zkb3=60;
zkb4=60;
IN1=0;
IN2=1;
IN3=0;
IN4=1;
IN5=0;
IN6=1;
IN7=0;
IN8=1;
}
else if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==0))//左转1//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
IN5=0;
IN6=1;
IN7=0;
IN8=0;
//delay(30);
}
else if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==1)&&(RSEN2==0)&&(RSEN3==0))//左转2//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
IN5=0;
IN6=1;
IN7=0;
IN8=0;
//delay(10);
}
else if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==1)&&(RSEN2==0)&&(RSEN3==1))//左转3//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
IN5=0;
IN6=1;
IN7=0;
IN8=0;
//delay(10);
}
else if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==0)&&(RSEN2==0)&&(RSEN3==1))//左转4//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
IN5=0;
IN6=1;
IN7=0;
IN8=0;
delay(50);
}
else if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==0)&&(RSEN2==1)&&(RSEN3==1))//左转5//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
IN5=0;
IN6=1;
IN7=0;
IN8=0;
delay(80);
//delay(30);
}
else if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==0)&&(RSEN2==0)&&(RSEN3==0))//左转6//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=1;
IN3=0;
IN4=0;
IN5=0;
IN6=1;
IN7=0;
IN8=0;
delay(120);
//delay(30);
}
else if ((LSEN1==0)&&(LSEN2==1)&&(LSEN3==1)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==1))//右转1//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
IN5=0;
IN6=0;
IN7=0;
IN8=1;
//delay(30);
}
else if ((LSEN1==0)&&(LSEN2==0)&&(LSEN3==1)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==1))//右转2//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
IN5=0;
IN6=0;
IN7=0;
IN8=1;
//delay(10);
}
else if ((LSEN1==1)&&(LSEN2==0)&&(LSEN3==1)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==1))//右转3//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
IN5=0;
IN6=0;
IN7=0;
IN8=1;
//delay(10);
}
else if ((LSEN1==1)&&(LSEN2==0)&&(LSEN3==0)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==1))//右转4//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
IN5=0;
IN6=0;
IN7=0;
IN8=1;
delay(50);
}
else if ((LSEN1==1)&&(LSEN2==1)&&(LSEN3==0)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==1))//右转5//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
IN5=0;
IN6=0;
IN7=0;
IN8=1;
delay(80);
//delay(30);
}
else if ((LSEN1==0)&&(LSEN2==0)&&(LSEN3==0)&&(RSEN1==1)&&(RSEN2==1)&&(RSEN3==1))//右转6//
{
zkb1=30;
zkb2=30;
zkb3=30;
zkb4=30;
IN1=0;
IN2=0;
IN3=0;
IN4=1;
IN5=0;
IN6=0;
IN7=0;
IN8=1;
delay(120);
//delay(30);
}
else if ((LSEN1==0)&&(RSEN1==0)||(LSEN1==0)&&(RSEN2==0)||(LSEN1==0)&&(RSEN3==0))//停止1//
{
zkb1=50;
zkb2=50;
zkb3=50;
zkb4=50;
IN1=0;
IN2=1;
IN3=0;
IN4=1;
IN5=0;
IN6=1;
IN7=0;
IN8=1;
delay(500);
zkb1=0;
zkb2=0;
zkb3=0;
zkb4=0;
delay(3000);
}
else if ((LSEN2==0)&&(RSEN1==0)||(LSEN2==0)&&(RSEN2==0)||(LSEN2==0)&&(RSEN3==0))//停止2//
{
zkb1=50;
zkb2=50;
zkb3=50;
zkb4=50;
IN1=0;
IN2=1;
IN3=0;
IN4=1;
IN5=0;
IN6=1;
IN7=0;
IN8=1;
delay(500);
zkb1=0;
zkb2=0;
zkb3=0;
zkb4=0;
delay(3000);
}
else if ((LSEN3==0)&&(RSEN1==0)||(LSEN3==0)&&(RSEN2==0)||(LSEN3==0)&&(RSEN3==0))//停止3//
{
zkb1=50;
zkb2=50;
zkb3=50;
zkb4=50;
IN1=0;
IN2=1;
IN3=0;
IN4=1;
IN5=0;
IN6=1;
IN7=0;
IN8=1;
delay(500);
zkb1=0;
zkb2=0;
zkb3=0;
zkb4=0;
delay(3000);
}
}
|