中级会员
- 积分
- 226
- 威望
- 188
- 贡献
- 26
- 兑换币
- 4
- 注册时间
- 2010-5-13
- 在线时间
- 6 小时
|
2#
楼主 |
发表于 2010-6-5 22:06:51
|
只看该作者
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> / * derivative information */
#pragma LI NK_INFO DERIVATIVE "mc9s12xs128"
#pragma CODE_ SEG __NEAR_SEG NON_BANKED
//*********************宏定义*******************//
#define DLT1 21 20000
#define DLT2 15 0
#define INTETIME 1536 // 中断时间常数
#define N 10
#define NSP 11
#define MAXANGLE 1 560 // 舵机最大右偏角对应的PWM 值
#define MINANGLE 1 220 // 舵机最大左偏角对应的PWM 值
#define P1 3 //舵机PD 控制P 常量
#define D1 7 //舵机PD 控制D 常量
#define P2 80 //速度PD 控制P 常量
#define I2 5
#define D2 60 //速度PD 控制D 常量
#define MAXSUME 600 / /积分饱和上限
#define MINSUME -60 0 // 积分饱和下限
//********************定义结构体*****************//
struct PIDROUTE //路径控制结构体
{ int derror; //微分变量
int sume; //积分变量
}; struct PIDV //速度控制结构体
{
float set; //设定值
float now; //当前值
float error; //当前偏差
float laste; //上次偏差
float derror; //微分变量
float sume; //积分变量
};
struct str1 //路径信息结构体1
{
int rout_ero; //路径偏差
unsigned char sensors,change;
}; struct str2 //路径信息结构体1
{ unsigned char change,blarnum;
unsi gned char area[5][3];
unsi gned char blmiddle;
}; struct filter //滤波结构体
{ unsi gned char senava[12];
I
unsigned char blamef,blnmef,blmef;
}; struct speed //速度结构体
{ unsi gned int vwl,vwb,vwo;
unsigned int vnl,vnb,vno;
}; //*******************定义全局变量*****************//
unsigned char state=0;
unsigned char sensor19,sensor0;
unsigned char bend=100;
unsigned char inte=0;
unsigned int stopt=0;
unsigned char stop=0;
unsigned int eng=0;
unsigned char startln;
unsigned char enterln=0; //识别进直道延时
unsigned char enterna,enterwi; //识别进窄道、宽道延时
unsigned char chantim=0,chanti=0;
unsigned char spn1=0,spn2;
struct filter sensta; //定义滤波结构体变量
struct PIDROUTE routec;
unsigned char rout_o[12]; //路径原始信息
int arr0[12]={-60,-50,-45,-30,-10,-5,5,10,30,45,50,60};
unsigned int rou_out; //舵机PWM 输出
struct PIDV speedc;
float sp_out=0;
unsigned int v_arr[NSP];
struct speed spsel[7];
struct speed spset; //设定速度
void MCUInit(void)
{ CLKSEL &= 0X7F; //暂选OSCCLK 为系统时钟源
PLLCTL &= 0XBF;
SYNR = 0X01; //设定倍频因子
REFDV = 0X00;
PLLCTL |= (1<<6); //打开PLL 电路
w hile ((CRGFLG&0X08) == 0X00); //等待PLLCLK 稳定
CLKSEL |= (1<<7);
COPCTL = 0X00; //关闭看门狗
} //******************IO 端口初始化*******************//
void IOInit(void)
{ DDRA =0X00; //设置A 口为输入口 读光电管信息
DDRH =0X00;
DDRB =0X00;
PUCR |=0X02; //设置B 口上拉
PTM &=0XF8; //M 口低三位初始为低
PTP |=(1<<5); //*** //P 口第五位初始为高
} //******************PWM 模块初始化******************//
void PWMInit(void)
{
PWMCTL |=0XB0;
PWMPRCLK =0X05;
PWMCLK =0X00; //分频因子
PWMCAE &=0X75; //PWM 输出左对齐
PWMPER01 =20000;
PWMDTY01 =1390; //设定01 通道初始占空比 摆正舵机
PWMPER23 =3200;
PWMDTY23 =0; //设定23 通道初始占空比
PWMPER67 =3200;
PWMDTY67 =0; //设定67 通道初始占空比
PWME |=0X8A; //开启所选PWM 通道
} |
|