欢迎来到天天文库
浏览记录
ID:59356980
大小:29.00 KB
页数:7页
时间:2020-09-04
《小车直立程序.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include/*commondefinesandmacros*/#include/*derivativeinformation*/#include#include#include#pragmaLINK_INFODERIVATIVE"mc9s12xs128"uchari=0;chark,z=0;floatfP,fI;voiddelay(unsignedinth){unsignedintj,tk;f
2、or(tk=0;tk3、idinitPIT(void)//定时中断初始化函数50MS定时中断设置{PITCFLMT_PITE=0;//定时中断通道0关PITCE_PCE0=1;//定时器通道0使能PITMTLD0=160-1;//逼鞒踔瞪瓒ā?240分频,在24MHzBusClock下,为0.1M//即10us.PITLD0=2000-1;//16位定时器初值设定。PITTIME*0.01MSPITINTE_PINTE0=1;//定时器中断通道0中断使能PITCFLMT_PITE=1;//定时器通道0使能}//ad初始化v4、oidADC_Init(void){//printf("ADC_Init");ATD0CTL1=0x40;//7:1-外部触发,65:00-8位精度,4:放电,3210:chATD0CTL2=0x40;//禁止外部触发,中断禁止ATD0CTL3=0xC0;//右对齐无符号,每次转换8个序列,NoFIFO,Freeze模式下继续转ATD0CTL4=0x05;//765:采样时间为4个AD时钟周期,ATDClock=[BusClock*0.5]/[PRS+1]ATD0CTL5=0x20;//6:0特5、殊通道禁止,5:1连续转换,4:1多通道轮流采样ATD0DIEN=0x00;//禁止数字输入}//pwm初始化voidPWM(){PWMPRCLK=0X11;//对总线时钟进行预分频,总线时钟为8M,分频后为500KPWMCLK=0x0f;//设sAsb为其时钟源PWMSCLA=0X05;PWMSCLB=0X05;//A时钟为2000HzPWMPOL=0X00;//上升沿翻转PWMCAE=0X00;//左对齐输出PWMPER0=0XF0;PWMPER1=0XF0;PWMPER2=0XF0;PWMPE6、R3=0XF0;PWMCNT0=0X00;//0通道计数器清0PWMCNT2=0X00;}intgetad(charm){intad;ATD0CTL5=(0X20+m);while(!ATD0STAT0_SCF);switch(m){case0:ad=ATD0DR0;case1:ad=ATD0DR1;case2:ad=ATD0DR2;case3:ad=ATD0DR3;case4:ad=ATD0DR4;}returnad;}floatfilter(charn){floatsum=0;charcount7、;intjieguo;for(count=0;count<10;count++){jieguo=getad(n);sum=sum+jieguo;asmNOP;asmNOP;}sum=sum/10;returnsum;}voidSpeedControl(void){floatfDelta;intg_fCarSpeed;g_fCarSpeed=PACNT;PACNT=0X0000;g_fCarSpeed=g_fCarSpeed;fDelta=0-g_fCarSpeed;fP=fDelta*1.2;//S8、PEED_CONTROL_P;fI=fDelta*0.001;//SPEED_CONTROL_I;}voidPACAInit(){PACTL=0X50;PACNT=0X0000;}voidmain(){floatgry=0,gav=0,gry1,gry11;floatcarangle=2,gavangle,pouty,poutz;floatp,h;intdianjiy,dianjiz,g_nSpeedControlCount=0;intzuo,you,cha;DDRB
3、idinitPIT(void)//定时中断初始化函数50MS定时中断设置{PITCFLMT_PITE=0;//定时中断通道0关PITCE_PCE0=1;//定时器通道0使能PITMTLD0=160-1;//逼鞒踔瞪瓒ā?240分频,在24MHzBusClock下,为0.1M//即10us.PITLD0=2000-1;//16位定时器初值设定。PITTIME*0.01MSPITINTE_PINTE0=1;//定时器中断通道0中断使能PITCFLMT_PITE=1;//定时器通道0使能}//ad初始化v
4、oidADC_Init(void){//printf("ADC_Init");ATD0CTL1=0x40;//7:1-外部触发,65:00-8位精度,4:放电,3210:chATD0CTL2=0x40;//禁止外部触发,中断禁止ATD0CTL3=0xC0;//右对齐无符号,每次转换8个序列,NoFIFO,Freeze模式下继续转ATD0CTL4=0x05;//765:采样时间为4个AD时钟周期,ATDClock=[BusClock*0.5]/[PRS+1]ATD0CTL5=0x20;//6:0特
5、殊通道禁止,5:1连续转换,4:1多通道轮流采样ATD0DIEN=0x00;//禁止数字输入}//pwm初始化voidPWM(){PWMPRCLK=0X11;//对总线时钟进行预分频,总线时钟为8M,分频后为500KPWMCLK=0x0f;//设sAsb为其时钟源PWMSCLA=0X05;PWMSCLB=0X05;//A时钟为2000HzPWMPOL=0X00;//上升沿翻转PWMCAE=0X00;//左对齐输出PWMPER0=0XF0;PWMPER1=0XF0;PWMPER2=0XF0;PWMPE
6、R3=0XF0;PWMCNT0=0X00;//0通道计数器清0PWMCNT2=0X00;}intgetad(charm){intad;ATD0CTL5=(0X20+m);while(!ATD0STAT0_SCF);switch(m){case0:ad=ATD0DR0;case1:ad=ATD0DR1;case2:ad=ATD0DR2;case3:ad=ATD0DR3;case4:ad=ATD0DR4;}returnad;}floatfilter(charn){floatsum=0;charcount
7、;intjieguo;for(count=0;count<10;count++){jieguo=getad(n);sum=sum+jieguo;asmNOP;asmNOP;}sum=sum/10;returnsum;}voidSpeedControl(void){floatfDelta;intg_fCarSpeed;g_fCarSpeed=PACNT;PACNT=0X0000;g_fCarSpeed=g_fCarSpeed;fDelta=0-g_fCarSpeed;fP=fDelta*1.2;//S
8、PEED_CONTROL_P;fI=fDelta*0.001;//SPEED_CONTROL_I;}voidPACAInit(){PACTL=0X50;PACNT=0X0000;}voidmain(){floatgry=0,gav=0,gry1,gry11;floatcarangle=2,gavangle,pouty,poutz;floatp,h;intdianjiy,dianjiz,g_nSpeedControlCount=0;intzuo,you,cha;DDRB
此文档下载收益归作者所有