/**********飞思卡尔智能汽车程序***/
//资源分配如下
//舵机用定时器0频率为50HZ
//直流用定时器1频率为10KHZ
//测速用外部中断0周期为50ms
#include<AT89X52.h>
#defineucharunsigned char
#defineuint unsigned int
sbit PWM_Steer=P1^0;
sbit PWM_IN1=P1^2;
sbit PWM_IN2=P1^3;
//****舵机控制量************
//====f=24MHz
ucharangle=8;
/*
uchar Low_Angle_L[]={0x6d,0xdc,0x4b,0xba,0x29,0x98,0x07,0x77,0xe6, 0x55,0xc4,0x34,0xa2,0x11,0x80};
uchar High_Angle_L[]={0x6c,0x6c,0x6d,0x6d,0x6e,0x6e,0x6f,0x6f,0x6f,0x70,0x70,0x71,0x71,0x72,0x72};
uchar Low_Angle_H[]={0x50,0xe1,0x72,0x03,0x94,0x25,0xb6,0x47,0xd7,0x68,0xf9,0x8a,0x1b,0xac,0x3d};
uchar High_Angle_H[]={0xf7,0xf6,0xf6,0xf6,0xf5,0xf5,0xf4,0xf4,0xf3,0xf3,0xf2,0xf2,0xf2,0xf1,0xf1};
*/
//===f=11.0592
uchar Low_Angle_L[]={0x00,0x33,0x66,0x99,0xcc,0x00,0x33,0x66,0x99,0xcc,0x00,0x33,0x66,0x99,0xcc};
uchar High_Angle_L[]={0xbc,0xbc,0xbc,0xbc,0xbc,0xbd,0xbd,0xbd,0xbd,0xbd,0xbe,0xbe,0xbe,0xbe,0xbe};
uchar Low_Angle_H[]={0x00,0xcc,0x99,0x66,0x33,0x00,0xcc,0x99,0x66,0x33,0x00,0xcc,0x99,0x66,0x33};
uchar High_Angle_H[]={0xfc,0xfb,0xfb,0xfb,0xfb,0xfa,0xfa,0xfa,0xfa,0xfa,0xf9,0xfb,0xf9,0xf9,0xf9};
/*
//****直流电机控制量********
uchar code speed_hope[]={52,78,104,130,130,156,182,208,182,156,130,130,104,78,52}; //期望的速度值
uchar PWM_H_D=0x00;
uchar PWM_L_D=0xff; //PID运算之后
uchar last_error=0,pre_error=0;
uchartimes_stopline=0;
ucharstop=0;
*/
//******子程序*************
voidInit();
voidRoute_Detect();
voidStart_Delay(uchar);
//******主程序*************
void main()
{
PWM_IN1=1;
PWM_IN2=0;
Init();
Start_Delay(2);
while(1)
{
Route_Detect();
}
}
//*********初始化************************
voidInit()
{
IE=0X8b;
TMOD=0X11;
TH0=0XF4;
TL0=0X48;
TH1=0X38;
PWM_IN1=1;
PWM_IN2=0;
TCON=0X53;
}
//********路径检测*****************
void Route_Detect()
{
uchar led1,led2;
led1=P0;
led2=P2;
if(((led1==0xf3)||(led1==0xfb)||(led1==0xfc)||(led1==0xfe)||(led1==0xfd)||(led1==0xf9))&&(led2==0xff))
angle=0; //-35
else if((led1==0xf7)&&(led2==0xff))
angle=1; //-30
else if((led1==0xe7)&&(led2==0xff))
angle=2; //-25
else if((led1==0xef)&&(led2==0xff))
angle=3; //-20
else if((led1==0xcf)&&(led2==0xff))
angle=4; //-15
else if((led1==0xdf)&&(led2==0xff))
angle=5; //-10
else if((led1==0x9f)&&(led2==0xff))
angle=6; //-5
else if((led1==0xbf)&&(led2==0xff))
angle=7; //0
else if((led1==0x3f)&&(led2==0xff))
angle=8; //5
else if((led1==0x7f)&&(led2==0xff))
angle=9; //10
else if((led1==0x7f)&&(led2==0xfe))
angle=10; //15
else if((led1==0xff)&&(led2==0xfe))
angle=11; //20
else if((led1==0xff)&&(led2==0xfc))
angle=12; //25
else if((led1==0xff)&&(led2==0xfd))
angle=13; //30
else if((led1==0xff)&&((led2==0xf9)||(led2==0xfb)||(led2==0xf3)||(led2==0xf7)||(led2==0xef)||(led2==0xe7)))
angle=14; //35
else
angle=9;
}
//********舵机控制*****************
voidSteer_Control() interrupt 1
{
if(PWM_Steer==1)
{
TL0=Low_Angle_L[angle];
TH0=High_Angle_L[angle];
PWM_Steer=0;
}
else
{
TL0=Low_Angle_H[angle];
TH0=High_Angle_H[angle];
PWM_Steer=1;
}
} /*
//*******速度检测***************
voidSpeed_Test()interrupt 0
{
speed_count++;
}
//********速度运算*****************
voidspeed_cal()
{
if(speed_t>=100)
{
EX0=0;
speed=26*speed_count;//(cm/s)
speed_count=0;
EX0=1;
}
}
//*********PID运算******************
void PID()
{
int PID;
int error;
int derror;
int pre_derror;
error=speed_hope[angle]-speed;
derror=error-last_error;
pre_derror=last_error-pre_error;
PID=PID_P*derror+PID_I*error+PID_D*(derror-pre_error);
PWM_H_D=256-(PWM_H_D+PID);
PWM_L_D=256-(PWM_L_D-PID);
pre_error=last_error;
last_error=error;
}
//*************************
void Motor_Contol() interrupt 3
{
speed_t++;
if(stop==1) //当停车标志为1时给直流电机制动脉冲
{
if(PWM_IN2==0)
{
TH1=trig_H_TH1;
TL1=trig_H_TL1;
PWM_IN2=1;
PWM_IN1=0;
}
else
{
TH1=trig_L_TH1;
TL1=trig_L_TL1;
PWM_IN2=0;
PWM_IN1=0;
}
if(speed<=0) //当速度减少到0时停车
{
TR1=0;
PWM_IN1=0;
PWM_IN2=0;
}
}
else
{
if(((angle>=11)||(angle<=3))&&(speed_hope[angle]<speed))
{
if(PWM_IN2==1)
{
TL1=speed_d_l;
TH1=0xff;
PWM_IN1=0;
PWM_IN2=0;
}
else
{
TL1=speed_d_h;
TH1=0xff;
PWM_IN1=0;
PWM_IN2=1;
}
}
else
{
if(PWM_IN1==1)
{
TH1=0XFF;
TL1=0x38;
PWM_IN1=0;
PWM_IN2=0;
}
else
{
TH1=0XFF;
TL1=0xff;
PWM_IN1=1;
PWM_IN2=0;
}
}
}
} */
//===========
void Start_Delay(uchar k)
{
int i=4000; //当K为1时定时1S
int j=1000;
for(;k>=0;k--)
for(;i>=0;i--)
for(;j>=0;j--)
;
}