#include <reg51.h>
/*************** 宏定义区 *************/
#define PWM_Left_2 20 //左转速度 //左右两侧驱动
#define PWM_Left_1 15 //小角度左转速度 //右侧单独驱动
#define PWM_Right_1 15 //小角度右转速度 //左侧单独驱动
#define PWM_Right_2 20 //右转速 //左右两侧驱动
#define PWM_straight 40 //直行速度
/************* 电机驱动管脚定义 *************/
sbit ENA = P1^0;
sbit IN1 = P1^1;
sbit IN2 = P1^2;
sbit IN3 = P1^3;
sbit IN4 = P1^4;
sbit ENB = P1^5;
/************* 蜂鸣器及灯光管脚定义 *************/
sbit buzz = P3^4;
sbit rgb_red = P3^5;
sbit rgb_green = P3^6;
/************* 红外传感器管脚定义 *************/
sbit LEFT_IR = P2^2;
sbit RIGHT_IR = P2^1;
sbit RIGHT_IR2 = P2^0;
sbit LEFT_IR2 = P2^3;
/************* RGB流水灯管脚定义 *************/
sbit p1 = P2^4;
sbit p2 = P2^5;
sbit p3 = P2^6;
sbit p4 = P2^7;
unsigned char PWMA,PWMB;
unsigned int b=2;
void xunji();
void turnleft();
void turnleft2();
void turnright();
void turnright2();
void stop();
void straight();
void turnleft_1();
void liushuideng();
void delay(unsigned int xms)
{
unsigned int x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void Timer0Init()
{
TMOD |= 0x01;
TL0 = (65536-100)%256;
TH0 = (65536-100)/256;
TR0 = 1;
ET0 = 1;
EA = 1;
}
/*************** main函数区 ******************/
void main()
{
rgb_green=0;
Timer0Init();
while(1)
{
xunji();
liushuideng();
}
}
/**************** 子函数区 ******************/
void T0isp() interrupt 1//脉宽调制(PWM)
{
unsigned char i,j;
TL0 = (65536-100)%256;
TH0 = (65536-100)/256;
i++;
j++;
if(i < PWMA)
{
ENA = 1;
}
else
{
ENA = 0;
if(i >= 100)
{
i = 0;
}
}
if(j < PWMB)
{
ENB = 1;
}
else
{
ENB = 0;
if(j >= 100)
{
j = 0;
}
}
}
void liushuideng() //RGB流水灯
{
p1 = 0;
delay(100);
p1 = 1;
p2 = 0;
delay(100);
p2 = 1;
p3 = 0;
delay(100);
p3 = 1;
p4 = 0;
delay(100);
p4 = 1;
}
void xunji() //循迹
{
unsigned char jiaodu;
if(LEFT_IR == 0 && RIGHT_IR == 0 && LEFT_IR2 == 0 && RIGHT_IR2 == 0)
{
switch(jiaodu)
{
case 0:straight();break;
case 1:turnleft2();break;
case 2:turnright2();break;
default:stop();break;
}
}
if(LEFT_IR == 1 && RIGHT_IR == 0 && LEFT_IR2 == 0 && RIGHT_IR2 == 0)
{
if(b==2)
{
turnleft_1();
b=0;
}
else
{
turnleft();
}
jiaodu = 0;
}
if(LEFT_IR == 0 && RIGHT_IR == 0 && LEFT_IR2 == 1 && RIGHT_IR2 == 0)
{
turnleft2();
jiaodu = 1;
}
if(LEFT_IR == 1 && RIGHT_IR == 0 && LEFT_IR2 == 1 && RIGHT_IR2 == 0)
{
turnleft2();
jiaodu = 1;
}
if(LEFT_IR == 0 && RIGHT_IR == 1 && LEFT_IR2 == 0 && RIGHT_IR2 == 0)
{
turnright();
jiaodu = 0;
}
if(LEFT_IR == 0 && RIGHT_IR == 0 && LEFT_IR2 == 0 && RIGHT_IR2 == 1)
{
turnright2();
jiaodu = 2;
}
if(LEFT_IR == 0 && RIGHT_IR == 1 && LEFT_IR2 == 0 && RIGHT_IR2 == 1)
{
turnleft2();
jiaodu = 2;
}
if(LEFT_IR == 1 && RIGHT_IR == 1 && LEFT_IR2 == 1 && RIGHT_IR2 == 1)
{
stop();
jiaodu = 3;
}
}
void straight() //直行速度配置
{
IN1 = 1;
IN2 = 0;
PWMA = PWM_straight;
IN3 = 1;
IN4 = 0;
PWMB = PWM_straight;
}
void turnleft() //左转速度配置
{
IN1 = 0;
IN2 = 0;
PWMA = 0;
IN3 = 1;
IN4 = 0;
PWMB = PWM_Left_1;
}
void turnleft2() //大角度左转速度配置
{
IN1 = 0;
IN2 = 1;
PWMA = PWM_Left_2;
IN3 = 1;
IN4 = 0;
PWMB = PWM_Left_2;
}
void turnright() //右转速度配置
{
IN1 = 1;
IN2 = 0;
PWMA = PWM_Right_1;
IN3 = 0;
IN4 = 0;
PWMB = 0;
}
void turnright2() //大角度右转速度配置
{
IN1 = 1;
IN2 = 0;
PWMA = PWM_Right_2;
IN3 = 0;
IN4 = 1;
PWMB = PWM_Right_2;
}
void turnleft_1() //内圈临时处理
{
IN1 = 0;
IN2 = 1;
PWMA = 25;
IN3 = 1;
IN4 = 0;
PWMB = 25;
delay(300);
}
void stop() //停车函数
{
IN1 = 0;
IN2 = 0;
PWMA = 0;
IN3 = 0;
IN4 = 0;
PWMB = 0;
buzz=0;
rgb_green=1;
rgb_red=0;
delay(1000);
}