搜档网
当前位置:搜档网 › 飞思卡尔智能车摄像头代码

飞思卡尔智能车摄像头代码

#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
/*************************************************************
摄像头数据
***************************************************************/
unsigned int AbsoluteTime; // 系统时钟,用于闭环调速
unsigned int Picture1[10][4]={{600,600,0,40},{600,600,0,60},{600,600,0,80},
{600,600,0,100},{600,600,0,120},{600,600,0,140},
{600,600,0,160},{600,600,0,180},{600,600,0,200},
{600,600,0,220}};
unsigned int Picturewrflag[10]; //图像数据真伪判别标志
int PictureCoordinate[10]={0,0,0,0,0,0,0,0,0,0};//图像坐标
unsigned int FieldCNT;
int RowCNT;
int CapFlag;
long coordinate1,coordinate2,line_wideth;
int se = 0;
int PMCNT,NextPMCNT;
int curvature;//曲率
int ServoMotorPWM;
int i;
int TurningLine;
/*********************************************************************
PID数据
***********************************************************************/
static int CurrentSpeed=0,DstSpeed=90;
static int waittime=0;
int speed_0=0;
int e=0,e_1=0;
int ie=0;
int Kp=35;
int Kd=0;
int Ki=10;//5;
int PACN0_i;
int SpeedMin=90,SpeedMax=400;
char a;
/***********************************************************************
路径记忆数据
************************************************************************/
int length=0;
int bendstraightMemory[100];//直道弯道数据存储
/*********************************************************************
摄像头函数
*********************************************************************/
void ETCInit(void);
void CCDInit(void);
void PWMInit(void);
void SystemInit(void);
void PictureDispose(void);
/*********************************************************************
PID函数
**********************************************************************/
void SpeedPID(void);
static void RTIInit(void);
void SetSpeed(void);
/***********************************************************************
路径记忆函数
************************************************************************/
void JourneyMemory(void);
/***********************************************************************/
void main(void) {
DisableInterrupts;
SystemInit();
EnableInterrupts;
for(;;) {
EnableInterrupts;
JourneyMemory();
PictureDispose();
}
}
/************************************************************************
系统初始化区域
**************************************************************************/
void SystemInit(void)
{ AbsoluteTime = 0; // 初始化系统时钟
DDRB = 0xff; // 初始化PORTB,用于数据输出
FieldCNT = 0; // 场计数器初始化
ETCInit();
PWMInit();
RTIInit();
}
static void RTIInit(void) {
/* setup of the RTI interrupt frequency */


/* adjusted to get 1 millisecond (10.24 ms) with 16 MHz oscillator */
RTICTL = 0x64; /* set RTI prescaler */
CRGINT = 0x80; /* enable RTI interrupts */
}
void ETCInit(void)
{
TCTL4_EDG0B=0;
TCTL4_EDG0A=1; //0 channel Capture on rising edges only
TCTL3_EDG7B=1;
TCTL3_EDG7A=0; //7 channel Capture on falling edges only
TIOS_IOS0=0; //0 channel input compare
TIOS_IOS7=0; //7 channel input compare
TIOS_IOS4 = 0; // 视频输入捕获
TIOS_IOS5 = 0; // 行同步
TIOS_IOS6 = 0; // 场同步
TSCR1_TEN = 1; // 使能计数
TSCR1_TFFCA = 1; // 捕获中断、比较中断标志位通过读取数据和写入数据清除
PACTL=0x40; //16位累加器A使能 ,下降沿工作
PBCTL_PBEN=1;//16位累加器B使能
TC4 = TC4; // 清除中断标志位
TC5 = TC5;
TC6 = TC6;
CCDInit();
}
**********************************
函数名称:CCDInit
功能描述:图像空间初始化
********************************

void PWMInit(void) {
PWME = 0x00; // 禁止所有的PWM输出通道
PWMPOL = 0xFF; // 所有的PWM输出高电平有效
PWMCLK = 0x00; // Clock A && Clock B is the clock source for PWM channel 0~7
PWMPRCLK = 0x03; // bus clock:24M;Clock A--8分频,Clock B--0分频
PWMCAE = 0x00; // 所有的PWM输出为左对齐方式输出
PWMCTL = 0xFF; // 将两个8位的PWM输出通道组合成一个16位的PWM输出通
PWMCNT01 = 0xFF; // PWM计数器清零
PWMCNT23 = 0xFF;
PWMCNT45 = 0xFF;
PWMCNT67 = 0xFF;
PWMPER01 = 60000; // 驱动前轮舵机,周期20ms,频率50Hz
PWMPER23 = 2400; //周期0.1ms,频率10kHz
PWMPER67 = 2400;
// 设置占空比初始值
PWMDTY01 = 4200//舵机要求PWM高电平宽度0.5ms~2.5ms对应计数器数值为3800~4450~5150右
PWMDTY23 =1660;
PWMDTY67 =0;
PWME = 0xFF; // 所有PWM通道使能
}





EnableInterrupts;
}
/**************************************************************************
速度PID控制区域
***************************************************************************
interrupt void Real_Time_Interrupt(void)
{
AbsoluteTime++; // 运行系统时钟
SpeedPID();
PORTB=(char)(PACN10);
CRGFLG = 0x80; // 清中断标志
EnableInterrupts;
}
/*******************************************************************/
void SpeedPID(void)
{
PACN0_i=PACN10;
CurrentSpeed = PACN0_i;//码盘输入引脚PT0
PACN10=0;
e_1=e;
e=CurrentSpeed-DstSpeed;//-CurrentSpeed;
if(e<-20){
PWMDTY67 =0;
PWMDTY23 =2400;
}
else if(e>20){
PWMDTY23=0;
PWMDTY67=2400;
}
else
{
ie+=e;
speed_0=speed_0-(Kp*e/10)-(int)(Ki*ie/1000)+Kd*(e-e_1);
SetSpeed();
}
}
void SetSpeed(void)
{ if(speed_0>2400)
speed_0=2400;
PWMDTY67 =0;
PWMDTY23 =speed_0;
}


相关主题