本文详细介绍了我们为首届全国智能车大赛而准备的智能车系统方案。该 系统以Freescale16 位单片机MC9S12DG128 作为系统控制处理器

源代码在线查看: 000.txt

软件大小: 3 K
上传用户: flp123456
关键词: Freescale 128 MC9 S12
下载地址: 免注册下载 普通下载 VIP

相关代码

				选用单片机的PACN 口,程序初始化如下: 
				//初始化ECT 
				TIOS = 0x00; 
				TSCR1 = 0x80; 
				TCTL4 = 0x01; //设置PBCN为上升沿捕获 
				TSCR2 = 0x07; 
				PBCTL = 0x40; //设置PBCN计数器为16级联 
				PACTL = 0x40; //设置PACN 16级联,上升捕获 
				TCTL3 = 0x01; //设置PT4 输入捕获 
				
				
				
				附录C: 程序原代码
				#include  /* common defines and macros */
				#include  /* derivative information */
				#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
				#define hs TFLG1_C0F
				#define clr_hs TFLG1_C0F
				#define video ATD0DR0
				#define odd_even PTIS_PTIS2
				/********************************************************\
				Declaration of Valuebales
				\********************************************************/
				//CCD variables//
				#define r_max 40
				#define l_max 40
				char ccd_d[r_max+2][l_max];
				char ccd[l_max];
				char r_chg;
				char row;
				char line;
				char interval=7;
				char oeflag=0;
				char y_up[r_max+2];
				char temp_up[r_max+2];
				#define latch 20
				#define latch_neg -20
				char w_flg[r_max+2];
				int dif;
				//-------------//
				//SCI variables//
				char sci_d;
				//-------------//
				//PWM variables//
				#define mid_P 20
				int ctl;
				第十一章 总结
				第 V I I页
				int err[r_max+2];
				unsigned char m_speed;
				int str;
				int turn;
				unsigned char pulse[6];
				unsigned char speed;
				unsigned char turn_S;
				unsigned char str_S;
				//-------------//
				//general variables//
				char x,y,z;
				//-----------------//
				/********************************************************\
				Declaration of Functions
				\********************************************************/
				//overall initiation
				void ini_orall(void){
				DDRS_DDRS2=0;
				DDRS_DDRS7=1;
				TIOS_IOS0=0;
				TIOS_IOS1 = 0;
				TCTL4=0b00001101;
				TSCR1_TEN = 1;
				ICPAR_PA1EN = 1;
				DDRH=0xff;
				}
				//initiate bus clock
				void ini_PLL(void){
				REFDV=3;
				SYNR=7;
				while(0==CRGFLG_LOCK);
				CLKSEL=0x80;
				第VIII 页
				}
				/****SCI****/
				//initiate SCI
				void ini_SCI(void){
				SCI0BD=208;
				SCI0CR1=0x00;
				SCI0CR2=0x0C;
				}
				//SCI receive
				void re_sci(void){
				while(SCI0SR1_RDRF!=1);
				sci_d=SCI0DRL;
				}
				//SCI transmit
				void tx_sci(char tx_d){
				while(SCI0SR1_TC!=1);
				while(SCI0SR1_TDRE!=1);
				SCI0DRL=tx_d;
				}
				/***********/
				/****PWM****/
				//PWM & 33886 initialization
				void ini_PWM(void){
				PTS_PTS7 = 0;
				PWME = 0x00;
				PWMCTL_CON01= 1;
				PWMPRCLK = 0x33 ;
				PWMSCLA = 100 ;
				PWMSCLB = 1 ;
				PWMCLK = 0b00011100;
				PWMPOL = 0xff ;
				PWMCAE = 0x00 ;
				PWMPER0 = 0x4e ;
				第十一章 总结
				第 I X 页
				PWMPER1 = 0x20 ;
				PWMDTY0 = 0x18 ;
				PWMDTY1 = 0x38 ;
				PWME_PWME1 = 1 ;
				PWMDTY2 = 120 ;
				PWMPER2 = 200 ;
				PWME_PWME2 = 1 ;
				}
				//Set the angle of steer
				void pwm_set(int dutycycle)
				{
				int high8;
				high8=dutycycle&0xFF00;
				PWMDTY0=high8>>8;
				PWMDTY1=dutycycle&0x00FF;
				}
				/*************/
				/****SPEED****/
				//Initiate the Average speed
				void speed_init(void) {
				DDRM_DDRM0=0;
				DDRM_DDRM1=0;
				DDRM_DDRM2=0;
				DDRM_DDRM3=0;
				DDRM_DDRM4=0;
				DDRM_DDRM5=0;
				DDRJ_DDRJ6=0;
				DDRJ_DDRJ7=0;
				if(1==PTIM_PTIM0) {
				m_speed = 120 +
				2*(PTIM_PTIM1*10+PTIM_PTIM2*5+PTIM_PTIM3*2+PTIM_PTIM4*2+PTIM
				_PTIM5+PTIJ_PTIJ6+PTIJ_PTIJ7);
				}else{
				m_speed = 120 -
				2*(PTIM_PTIM1*10+PTIM_PTIM2*5+PTIM_PTIM3*2+PTIM_PTIM4*2+PTIM
				_PTIM5+PTIJ_PTIJ6+PTIJ_PTIJ7);
				第X 页
				}
				PWMDTY2=m_speed;
				str_S=m_speed/2-10;
				}
				//Get the current speed
				void get_speed(){
				char k;
				for(k=0;k				pulse[k]=pulse[k+1];
				}
				pulse[5]=PACN1;
				speed=pulse[5]-pulse[0];
				}
				//Set the current speed
				void set_speed(unsigned char a){
				if(speed				PWMDTY2=200;
				}
				if(speed>a){
				PWMDTY2=0;
				}
				}
				/*************/
				/****CONTROL****/
				void vhcl_pid(void){
				get_speed();
				if(err[8]>0){
				ctl=6250+42*err[8];
				}else{
				ctl=6250+38*err[8];
				}
				//------------------------
				dif=err[2]+err[10]-2*err[6];
				if((dif>-7)&&(dif-15)&&(err[6]				str++;
				第十一章 总结
				第 X I 页
				turn=0;
				PTH=0xff;
				}else{
				turn++;
				if(turn>20){
				str=0;
				}
				PTH=0x00;
				}
				//------------------------
				if(turn>0){
				set_speed(29);
				}else{
				set_speed(str_S);
				}
				//-----------------------------
				if(ctl				if(ctl>7400) ctl=7400;
				pwm_set(ctl);
				if(1==odd_even) oeflag=1;
				if(0==odd_even) oeflag=0;
				}
				/***********/
				/****CCD****/
				//Initiate A/D registers
				void ini_AD(void)
				{
				ATD0CTL2=0xC0;
				ATD0CTL3=0x08;
				ATD0CTL4=0x81;
				ATD0CTL5=0xA0;
				ATD0DIEN=0x00;
				}
				第XII 页
				//Scan the CCD image
				void ccd_sc(void){
				r_chg=0;
				while(r_chg				if(1==hs){
				clr_hs=1;
				r_chg++;
				}
				}
				line=0;
				r_chg=0;
				for(row=1;row				if(1==hs){
				clr_hs=1;
				r_chg++;
				}
				if(r_chg>=interval){
				ini_AD();
				for(line=0;line				while(!ATD0STAT1_CCF0);
				ccd[line]=video;
				}
				line=6;
				row++;
				w_flg[row-1]=0;
				r_chg=0;
				ATD0CTL2=0x00;
				}
				if((r_chg				ccd_d[row-1][line]=ccd[line];
				dif=ccd[line]-ccd[line+3];
				if(0==w_flg[row-1]){
				if(dif>latch){
				temp_up[row-1]=line+3;
				w_flg[row-1]=1;
				}
				}else{			

相关资源