#include "STC12C5A60S2.H"
#include "intrins.h"
#include "uart.h"
#include "AD.h"
#define uchar unsigned char
#define uint unsigned int
#define Motor_Speed1 350 //电机速度模式
#define Motor_Speed2 350
#define Motor_Speed3 450
#define Motor_Speed4 499
sbit PWM_z_q = P2^0; //电机控制线接口
sbit PWM_z_h = P2^1;
sbit PWM_y_q = P2^3;
sbit PWM_y_h = P2^2;
sbit Servo = P1^0; //舵机接口
sbit Stop_Car = P1^7;
bit Second_Flag = 0;
bit Direct_Flag = 0;
bit Stop_Delay = 0;
bit Stop_Car_Flag = 0;
bit Time_Flag = 0;
bit Tlag = 0;
bit Lag = 1;
int middle = 150; //舵机中值116
int Servo_Pwm = 0; //舵机占空比
uint Servo_Cycle = 2000; //舵机周期(20mS)
uint flag = 0;
uint countt = 0;
uchar dianji_run = 2; //停止状态
uint dianji_duty = 0; //电机速度控制 (0-500)
uint dianji_cycle = 500; //电机周期
void Tracking(void);
void Servo_Control(int difference);
void Timer0Init(void);
void Timer1Init(void); //100微秒@11.0592MHz
extern uint adshow[4] = {0}; //待发送的数
void Runing(void)
{
if((countt == 0) && (Stop_Car == 0)) //确保第一次进入检测
{
flag++;
countt = 1;
}
if(countt != 0)
{
countt++;
if(countt > 500)
{
countt = 0; //保证能第二次进入
}
}
if(flag == 2)
{
dianji_run = 2;
}
else
{
Tracking();
}
}
void main(void)
{
Timer0Init(); //定时器初始化
InitADC(); //ADC初始化
while(1)
{
adshow[0] = GetADCResult(0); //采集P1^0处的AD值
adshow[1] = GetADCResult(1);
adshow[2] = GetADCResult(2);
adshow[3] = GetADCResult(3);
Runing();
}
}
void Tracking(void)
{
if(adshow[1] <= 10 && adshow[2] >=100 && adshow[3] <= 10) //直走
{
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(0);
}
if(adshow[1] >= 10 && adshow[3] <= 10) //左转
{
if(adshow[1] > 10 && adshow[1] <= 20 )
{
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(-1);
}
else if(adshow[1] > 20 && adshow[1] <= 30 )
{ Direct_Flag = 1;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(-2);
}
else if(adshow[1] > 30 && adshow[1] <= 40 )
{ Direct_Flag = 1;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(-3);
}
else if(adshow[1] > 40 && adshow[1] <= 80)
{ Direct_Flag = 1;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(-5);
}
else if(adshow[1] > 80 && adshow[1] <= 85 )
{ Direct_Flag = 1;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(-7);
}
else if(adshow[1] > 85 && adshow[1] <= 100)
{ Direct_Flag = 1;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(-9);
}
else if(adshow[1] > 100 && adshow[1] <= 110)
{ Direct_Flag = 1;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(-11);
}
if(adshow[1] > 110 && adshow[2] <= 15)
{ Direct_Flag = 1;
dianji_run = 1;
dianji_duty = Motor_Speed3;
Servo_Control(-20);
}
}
if(adshow[1] <= 10 && adshow[3] >= 10) //右转
{
if( adshow[3] > 10 && adshow[3] <= 20)
{
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(1);
}
else if( adshow[3] > 20 && adshow[3] <= 30)
{ Direct_Flag = 0;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(2);
}
else if( adshow[3] > 30 && adshow[3] <= 40)
{ Direct_Flag = 0;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(4);
}
else if( adshow[3] > 40 && adshow[3] <= 80)
{ Direct_Flag = 0;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(5);
}
else if( adshow[3] > 80 && adshow[3] <= 85)
{ Direct_Flag = 0;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(6);
}
else if( adshow[3] > 85 && adshow[3] <= 100)
{ Direct_Flag = 0;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(7);
}
else if( adshow[3] > 100 && adshow[3] <= 110)
{ Direct_Flag = 0;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(9);
}
else if( adshow[3] >= 110 && adshow[2] <= 15)
{ Direct_Flag = 0;
dianji_run = 1;
dianji_duty = Motor_Speed4;
Servo_Control(14);
}
}
if (adshow[1] < 8 && adshow[2] < 8 && adshow[3] < 8) //圆环处理
{
if(Direct_Flag)
{
dianji_run = 1;
dianji_duty = Motor_Speed2;
Servo_Control(-23);
}
else
{
dianji_run = 1;
dianji_duty = Motor_Speed2;
Servo_Control(21);
}
}
}
void Servo_Control(int difference) //舵机控制
{
if(difference != 0)
{
Servo_Pwm = middle + difference;
}
else
{
Servo_Pwm = middle;
}
}
void Timer0Init(void) //10微秒@11.0592MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0xF7; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //打开定时器0中断
EA = 1; //打开总中断
}
void Timer0(void) interrupt 1 //定时器0中断服务函数
{
static int count1 = 0;
static int count2 = 0;
TL0 = 0xF7; //设置定时初值
TH0 = 0xFF; //设置定时初值
count1++;
count2++;
/****电机****/
if (count1 <= dianji_duty)
{
if (dianji_run == 1)
{
PWM_z_q = 1;
PWM_y_q = 1;
PWM_z_h = 0;
PWM_y_h = 0;
}
else if (dianji_run == 0)
{
PWM_z_q = 0;
PWM_y_q = 0;
PWM_z_h = 1;
PWM_y_h = 1;
}
}
else
{
PWM_z_q = 0;
PWM_y_q = 0;
PWM_z_h = 0;
PWM_y_h = 0;
}
if (count1 >= dianji_cycle) /******电机周期保证******/
{
count1 = 0; /*****重置计数*****/
}
/****舵机****/
if (count2 <= Servo_Pwm)
{
Servo = 1;
}
else
{
Servo = 0;
}
if (count2 == Servo_Cycle)
{
count2 = 0; //重置计数
}
}