飞思卡尔智能车程序汇总_第1页
飞思卡尔智能车程序汇总_第2页
飞思卡尔智能车程序汇总_第3页
飞思卡尔智能车程序汇总_第4页
飞思卡尔智能车程序汇总_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

./1.流水灯程序:#include<hidef.h>/*commondefinesandmacros*/#include"derivative.h"/*derivative-specificdefinitions*/voiddelay<unsignedintms>{unsignedinti;unsignedcharj;for<i=0;i<ms;i++>{for<j=0;j<100;j++>;}}voidmain<void>{/*putyourowncodehere*/ EnableInterrupts; DDRA=0x00; DDRB_DDRB0=1;for<;;>{delay<25000>;PORTB_PB0=1;delay<25000>;PORTB_PB0=0;}}2.液晶屏按键程序#include<hidef.h>/*commondefinesandmacros*/#include"derivative.h"/*derivative-specificdefinitions*/#include"lcd5110.h"#include"24c02.h"#include"button.h"voiddelay<unsignedintms>{unsignedinti;unsignedcharj;for<i=0;i<ms;i++>{for<j=0;j<100;j++>;}}/******************************倍频*****************************************/voidvPLLInit<void>{unsignedcharrefdv;refdv=3;CLKSEL=0X00; //disengagePLLtosystemPLLCTL_PLLON=1; //turnonPLLSYNR=0xc0|<<80/<refdv+1>>-1>;REFDV=0x40|refdv;POSTDIV=0x00;//pllclock=2*osc*<1+SYNR>/<1+REFDV>=160MHz;_asm<nop>;//BUSCLOCK=80M_asm<nop>;while<!<CRGFLG_LOCK==1>>; //whenpllissteady,thenuseit;CLKSEL_PLLSEL=1; //engagePLLtosystem;}/******************************倍频*****************************************/voidmain<void>{/*putyourowncodehere*/unsignedchari;unsignedcharsetnum[12];unsignedcharcur_sel;constcharlcd_show[12][4]={"MID\0","LEF\0","RIG\0","Kp\0","Kd\0","K1\0","K2\0","STP\0","MAX\0","MIN\0","HTM\0","HTX\0"};vPLLInit<>;lcd_init<>;init_24c02<>;init_btn<>;//EnableInterrupts;for<i=0;i<12;i++>setnum[i]=RdEEPROM<i>;//读取24c02for<i=0;i<12;i++>//显示初值{disply_listchar<<i/6>*42,i%6,lcd_show[i],1>;disply_number<<i/6>*42+16,i%6,setnum[i],5>;}if<btn_change<>>{cur_sel=0;for<i=0;i<12;i++>{disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;disply_number<<i/6>*42+16,i%6,setnum[i],5>;}for<;;>{if<btn_add<>>{setnum[cur_sel]++;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}if<btn_sub<>>{setnum[cur_sel]--;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}if<btn_change<>>{cur_sel++;if<cur_sel>11>cur_sel=0;for<i=0;i<12;i++>disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;}if<btn_sav<>>{for<i=0;i<12;i++>WrEEPROM<i,setnum[i]>;disply_listchar<0,0,"SMARTCAR",1>;disply_listchar<0,1,"SAVEOK",1>;disply_listchar<0,2,"=================",1>;disply_listchar<0,3,"TURNPOWEROFF",1>;disply_listchar<0,4,"=================",1>;disply_listchar<0,5,".",1>;while<1>;}}}DDRA=0x00; DDRB_DDRB0=1;for<;;>{delay<setnum[0]*100>;PORTB_PB0=1;delay<setnum[1]*100>;PORTB_PB0=0;}}3.电机程序#include<hidef.h>/*commondefinesandmacros*/#include"derivative.h"/*derivative-specificdefinitions*/#include"lcd5110.h"#include"24c02.h"#include"button.h"#defineservo_period20//ms#definemotor_pwm_frequ1//khzvoiddelay<unsignedintms>{unsignedinti;unsignedcharj;for<i=0;i<ms;i++>{for<j=0;j<100;j++>;}}/******************************倍频*****************************************/voidvPLLInit<void>{unsignedcharrefdv;refdv=3;CLKSEL=0X00; //disengagePLLtosystemPLLCTL_PLLON=1; //turnonPLLSYNR=0xc0|<<80/<refdv+1>>-1>;REFDV=0x40|refdv;POSTDIV=0x00;//pllclock=2*osc*<1+SYNR>/<1+REFDV>=160MHz;_asm<nop>;//BUSCLOCK=80M_asm<nop>;while<!<CRGFLG_LOCK==1>>; //whenpllissteady,thenuseit;CLKSEL_PLLSEL=1; //engagePLLtosystem;}/******************************倍频*****************************************//**PWM初始化***/voidInit_PWMout<void>{PWME=0x00;//禁止输出PWMPOL=0xff;//极性寄存器,1先输出高电平后为低电平0先输出低电平后为高电平PWMCTL=0xf0;//01234567通道级联PWMPRCLK=0x55;//ClockA=BusClock/32;ClockB=BusClock/32;PWMCLK=0b00000000;//选择clock:7B,6B,5A,4A,3B,2B,1A,0APWME=0xaa;//pp1,pp3,pp5,pp7口输出PWMPER67=80/4*125/motor_pwm_frequ;PWMPER45=80/4*125/motor_pwm_frequ;PWMPER23=80/4*servo_period*125;PWMPER01=80/4*servo_period*125;PWMDTY67=0;PWMDTY45=0;PWMDTY23=0;PWMDTY01=0;}voidmain<void>{/*putyourowncodehere*/unsignedchari;unsignedcharsetnum[12];unsignedcharcur_sel;constcharlcd_show[12][4]={"MID\0","LEF\0","RIG\0","Kp\0","Kd\0","K1\0","K2\0","STP\0","MAX\0","MIN\0","HTM\0","HTX\0"};vPLLInit<>;lcd_init<>;init_24c02<>;init_btn<>; Init_PWMout<>;EnableInterrupts;for<i=0;i<12;i++>setnum[i]=RdEEPROM<i>;//读取24c02for<i=0;i<12;i++>//显示初值{disply_listchar<<i/6>*42,i%6,lcd_show[i],1>;disply_number<<i/6>*42+16,i%6,setnum[i],5>;}if<btn_change<>>{cur_sel=0;for<i=0;i<12;i++>{disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;disply_number<<i/6>*42+16,i%6,setnum[i],5>;}for<;;>{if<btn_add<>>{setnum[cur_sel]++;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}if<btn_sub<>>{setnum[cur_sel]--;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}if<btn_change<>>{cur_sel++;if<cur_sel>11>cur_sel=0;for<i=0;i<12;i++>disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;}if<btn_sav<>>{for<i=0;i<12;i++>WrEEPROM<i,setnum[i]>;disply_listchar<0,0,"SMARTCAR",1>;disply_listchar<0,1,"SAVEOK",1>;disply_listchar<0,2,"=================",1>;disply_listchar<0,3,"TURNPOWEROFF",1>;disply_listchar<0,4,"=================",1>;disply_listchar<0,5,".",1>;while<1>;}}}DDRA=0x00; DDRB_DDRB0=1; for<;;> { delay<setnum[0]*1000>; PORTB_PB0=1; PWMDTY67=0; PWMDTY45=1000; delay<setnum[0]*1000>; PORTB_PB0=0; PWMDTY67=0; PWMDTY45=0; delay<setnum[0]*1000>; PORTB_PB0=0; PWMDTY45=0;PWMDTY67=1000;delay<setnum[0]*1000>; PORTB_PB0=0; PWMDTY67=0; PWMDTY45=0; }}4.码盘控速#include<hidef.h>/*commondefinesandmacros*/#include"derivative.h"/*derivative-specificdefinitions*/#include"lcd5110.h"#include"24c02.h"#include"button.h"#defineservo_period20//ms#definemotor_pwm_frequ1//khzvoiddelay<unsignedintms>{unsignedinti;unsignedcharj;for<i=0;i<ms;i++>{for<j=0;j<100;j++>;}}/******************************倍频*****************************************/voidvPLLInit<void>{unsignedcharrefdv;refdv=3;CLKSEL=0X00; //disengagePLLtosystemPLLCTL_PLLON=1; //turnonPLLSYNR=0xc0|<<80/<refdv+1>>-1>;REFDV=0x40|refdv;POSTDIV=0x00;//pllclock=2*osc*<1+SYNR>/<1+REFDV>=160MHz;_asm<nop>;//BUSCLOCK=80M_asm<nop>;while<!<CRGFLG_LOCK==1>>; //whenpllissteady,thenuseit;CLKSEL_PLLSEL=1; //engagePLLtosystem;}/******************************倍频*****************************************//**PWM初始化***/voidInit_PWMout<void>{PWME=0x00;//禁止输出PWMPOL=0xff;//极性寄存器,1先输出高电平后为低电平0先输出低电平后为高电平PWMCTL=0xf0;//01234567通道级联PWMPRCLK=0x55;//ClockA=BusClock/32;ClockB=BusClock/32;PWMCLK=0b00000000;//选择clock:7B,6B,5A,4A,3B,2B,1A,0APWME=0xaa;//pp1,pp3,pp5,pp7口输出PWMPER67=80/4*125/motor_pwm_frequ;PWMPER45=80/4*125/motor_pwm_frequ;PWMPER23=80/4*servo_period*125;PWMPER01=80/4*servo_period*125;PWMDTY67=0;PWMDTY45=0;PWMDTY23=0;PWMDTY01=0;}/**PWM初始化***///////////////////////测速增加部分/////////////////////////intcur_speed;intcur_time;/****测速初始化****/voidspeed_Init<void>{PACTL_PAMOD=0;//0Eventcountermode.PACTL_PEDGE=1;//1RisingedgesonIOC7pincausethecounttobeincremented.PACTL_PAEN=1;//1PulseAccumulatorsystemenabled.}//////stl测速定时器中断/////////////voidInit_INT_Timer<void>{TSCR2_PR=5;TSCR2_TOI|=1;TSCR1_TEN=1;}#pragmaCODE_SEG__NEAR_SEGNON_BANKEDvoidinterrupt16INT_Timer0<void>{cur_speed=PACNT;PACNT=0;cur_time++;TFLG2_TOF=1;}//////////////////////测速增加部分/////////////////////////voidmain<void>{/*putyourowncodehere*/unsignedchari;unsignedcharsetnum[12];unsignedcharcur_sel;intpwm_motor;Boolopen=FALSE;constcharlcd_show[12][4]={"MID\0","LEF\0","RIG\0","Kp\0","Kd\0","K1\0","K2\0","STP\0","MAX\0","MIN\0","HTM\0","HTX\0"};vPLLInit<>;lcd_init<>;init_24c02<>;init_btn<>; Init_PWMout<>; speed_Init<>; cur_time=0; pwm_motor=0;PACNT=0;Init_INT_Timer<>;EnableInterrupts;for<i=0;i<12;i++>setnum[i]=RdEEPROM<i>;//读取24c02for<i=0;i<12;i++>//显示初值{disply_listchar<<i/6>*42,i%6,lcd_show[i],1>;disply_number<<i/6>*42+16,i%6,setnum[i],5>;}if<btn_change<>>{cur_sel=0;for<i=0;i<12;i++>{disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;disply_number<<i/6>*42+16,i%6,setnum[i],5>;}for<;;>{if<btn_add<>>{setnum[cur_sel]++;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}if<btn_sub<>>{setnum[cur_sel]--;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}if<btn_change<>>{cur_sel++;if<cur_sel>11>cur_sel=0;for<i=0;i<12;i++>disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;}if<btn_sav<>>{for<i=0;i<12;i++>WrEEPROM<i,setnum[i]>;disply_listchar<0,0,"SMARTCAR",1>;disply_listchar<0,1,"SAVEOK",1>;disply_listchar<0,2,"=================",1>;disply_listchar<0,3,"TURNPOWEROFF",1>;disply_listchar<0,4,"=================",1>;disply_listchar<0,5,".",1>;while<1>;}}}DDRA=0x00; DDRB_DDRB0=1; for<;;>{disply_number<16,5,cur_speed/3,5>;if<cur_time>=setnum[0]>{open=!open;cur_time=0;pwm_motor=0;}if<FALSE==open>{PORTB_PB0=1;PWMDTY67=0;pwm_motor=<setnum[4]-cur_speed/3>*20;if<pwm_motor>PWMPER45>PWMDTY45=PWMPER45;elseif<pwm_motor>0>PWMDTY45=pwm_motor;elsePWMDTY45=0;}elseif<TRUE==open>{PORTB_PB0=0;PWMDTY45=0;pwm_motor=<setnum[4]-cur_speed/3>*20;if<pwm_motor>PWMPER67>PWMDTY67=PWMPER45;elseif<pwm_motor>0>PWMDTY67=pwm_motor;elsePWMDTY67=0;}}}/* for<;;>{disply_number<16,5,cur_speed/5,5>;pwm_motor=<setnum[4]-cur_speed/5>*25;if<pwm_motor>PWMPER45>PWMDTY45=PWMPER45;elseif<pwm_motor>0>PWMDTY45=pwm_motor;elsePWMDTY45=0;}}*/5.电磁车程序#include<hidef.h>/*commondefinesandmacros*/#include<math.h>#include"derivative.h"/*derivative-specificdefinitions*/#include"lcd5110.h"#include"24c02.h"#include"button.h"///延迟函数///voiddelay<unsignedintms>{unsignedinti;unsignedcharj;for<i=0;i<ms;i++>{for<j=0;j<100;j++>;}}///延迟函数///#defineservo_period20//ms#definemotor_pwm_frequ1//khz//#definedistance10000unsignedintSERVO_MID;//=3078intSERVO_LIMIT=1000;///**PWM初始化***/voidInit_PWMout<void>{PWME=0x00;//禁止输出0b00000000PWMPOL=0xff;//极性寄存器,1先输出高电平后为低电平0先输出低电平后为高电平0b11111111PWMCTL=0xf0;//01234567通道级联0b11110000PWMPRCLK=0x55;//ClockA=BusClock/32;ClockB=BusClock/32;0b01010101PWMCLK=0b00000000;//选择clock:7B,6B,5A,4A,3B,2B,1A,0APWME=0xaa;//pp1,pp3,pp5,pp7口输出0b10101010PWMPER67=80/4*125/motor_pwm_frequ;PWMPER45=80/4*125/motor_pwm_frequ;PWMPER23=80/4*servo_period*125;PWMPER01=80/4*servo_period*125;PWMDTY67=0;PWMDTY45=0;PWMDTY23=0;PWMDTY01=0;}/**PWM初始化***////舵机函数///voidset_servo<intservo>{if<servo>SERVO_LIMIT>servo=SERVO_LIMIT;if<servo<0-SERVO_LIMIT>servo=0-SERVO_LIMIT;PWMDTY01=SERVO_MID-servo;}///舵机函数////******************************倍频*****************************************/voidvPLLInit<void>{unsignedcharrefdv;refdv=3;CLKSEL=0X00; //disengagePLLtosystemPLLCTL_PLLON=1; //turnonPLLSYNR=0xc0|<<80/<refdv+1>>-1>;REFDV=0x40|refdv;POSTDIV=0x00;//pllclock=2*osc*<1+SYNR>/<1+REFDV>=160MHz;_asm<nop>;//BUSCLOCK=80M_asm<nop>;while<!<CRGFLG_LOCK==1>>; //whenpllissteady,thenuseit;CLKSEL_PLLSEL=1; //engagePLLtosystem;}/******************************倍频*****************************************/unsignedintcur_speed;unsignedintcur_length;////////控速函数/////voidcontrolSpeed<intspeed>{intpwm_motor=0;pwm_motor=<speed-cur_speed>*20;//////////dianjiif<pwm_motor>PWMPER67>PWMDTY67=PWMPER67;elseif<pwm_motor>0>PWMDTY67=pwm_motor;elsePWMDTY67=0;}////////控速函数//////****测速初始化****/voidspeed_Init<void>{PACTL_PAMOD=0;//0Eventcountermode.PACTL_PEDGE=1;//1RisingedgesonIOC7pincausethecounttobeincremented.PACTL_PAEN=1;//1PulseAccumulatorsystemenabled.}//////stl测速定时器中断/////////////voidInit_INT_Timer<void>{TSCR2_PR=7;TSCR2_TOI|=1;TSCR1_TEN=1;}#pragmaCODE_SEG__NEAR_SEGNON_BANKEDvoidinterrupt16INT_Timer0<void>{cur_speed=PACNT/100;PACNT=0;cur_length+=cur_speed;TFLG2_TOF=1;}////SCI初始化////voidini_SCI<void>{SCI0BD=<80*1000000/115200/16>;//115200=<busclk*1000000/SCI0BD/16>;SCI0CR1=0x00;SCI0CR2=0x0C;}voidTERMIO_PutChar<unsignedcharch>{while<!<SCI0SR1&0x80>>;SCI0DRL=ch;}/****SCI初始化****/////////////////////////A/D初始化///////////////////////////////////voidini_AD<void>{ATD0DIEN=0x00; //禁止数字输入 ATD0CTL1=0x00; ATD0CTL2=0b10000000;//0b11000000;// ATD0CTL3=0b11000000;//A/D转换序列长度为8 ATD0CTL4=0b00010110;//8位精度,ATDclock=Busclock/<PRS+1>/2=BusClock/40=2MHz,要求ATDclock<=2MHz&&ATDclock>=500KHz //ATD0CTL4=0x81;//8位精度,2个时钟,ATDClock=[BusClock*0.5]/[PRS+1];PRS=1,divider=12 ATD0CTL5=0b00110000;//右对齐无符号数,连续转换序列模式,2通道采样,AN0-AN7模拟量通道输入 }///////////////////////A/D初始化///////////////////////////////////////*******************************读取转换结果******************************byteReadATD<bytech>{bytead=0;while<!ATD0STAT0_SCF>;switch<ch>{default:case0:ad=ATD0DR0L;break;case1:ad=ATD0DR1L;break;case2:ad=ATD0DR2L;break;case3:ad=ATD0DR3L;break;/*case4:ad=ATD0DR4L;break;case5:ad=ATD0DR5L;break;case6:ad=ATD0DR6L;break;case7:ad=ATD0DR7L;break;*/}returnad;}//*******************************读取转换结果******************************voidmain<void>{/*putyourowncodehere*/unsignedinti;unsignedintj;intpwm_motor;unsignedintsetnum[18];////////unsignedcharcur_sel;///新加///floatsumxy1,sumy1,sumxy2,sumy2;//新加floatKp,Kd,flag,Identify;//新加floatsetted_speed_n;//新加floatdif_old,dif,dif_old2,dif_old1;//新加unsignedcharminy,minx,miny2,minx2;//新加floatK1,K2,K3;//新加intservo_change;//新加///新加///intsped1=15,sped2=25;intspeed=0;//constcharad[4][4]={"A0\0","A1\0","A2\0","A3\0"};//constcharlcd_show[12][4]={"MID\0","LIM\0","RIG\0","Sp1\0","Sp2\0","K1\0","K2\0","Spd\0","MAX\0","MIN\0","HTM\0","HTX\0"};constcharlcd_show[12][4]={"A0\0","A1\0","A2\0","A3\0","MID\0","spd\0","K1\0","K2\0","MAX\0","MIN\0","HTM\0","HTX\0"};///新加///unsignedcharAD_wData[4];//全局变量存放AD结果//unsignedcharj;unsignedcharmax_v[4]={0,0,0,0};//intmin_v[8]={255,255,255,255,255,255,255,255};constunsignedcharmin_v[4]={0,0,0,0};//constfloatdif_x[4]={-12+0.45,-6.3+0.45,6.8-0.45,11.7-0.45};constfloatdif_x[4]={-11.3,-4.6,3.4,10.6};K1=4;K2=10;K3=0;///新加///vPLLInit<>;lcd_init<>;init_24c02<>;init_btn<>;Init_PWMout<>;speed_Init<>;PACNT=0;Init_INT_Timer<>;ini_AD<>;EnableInterrupts;//cur_length=0;for<i=3;i<18;i++>setnum[i]=RdEEPROM<i>;//读取24c02setnum[0]=setnum[12]*256+setnum[13];setnum[1]=setnum[14]*256+setnum[15];setnum[2]=setnum[16]*256+setnum[17];if<btn_change<>>{cur_sel=0;for<i=0;i<12;i++>{disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;disply_number<<i/6>*42+16,i%6,setnum[i],5>;}for<;;>{if<btn_add<>>{setnum[cur_sel]++;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;for<i=0;i<255;i++>{for<j=0;j<255;j++>{delay<2>;if<btn_add<>==0>break;}if<j<255>break;}while<btn_add<>>{delay<800>;setnum[cur_sel]++;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}}if<btn_sub<>>{setnum[cur_sel]--;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;for<i=0;i<255;i++>{for<j=0;j<255;j++>{delay<2>;if<btn_sub<>==0>break;}if<j<255>break;}while<btn_sub<>>{delay<800>;setnum[cur_sel]--;disply_number<<cur_sel/6>*42+16,cur_sel%6,setnum[cur_sel],5>;}}if<btn_change<>>{cur_sel++;if<cur_sel>11>cur_sel=0;for<i=0;i<12;i++>disply_listchar<<i/6>*42,i%6,lcd_show[i],<cur_sel==i>?0:1>;}if<btn_sav<>>{setnum[12]=setnum[0]/256;setnum[13]=setnum[0]%256;setnum[14]=setnum[1]/256;setnum[15]=setnum[1]%256;setnum[16]=setnum[2]/256;setnum[17]=setnum[2]%256;for<i=3;i<18;i++>WrEEPROM<i,setnum[i]>;//disply_listchar<0,0,"SMARTCAR",1>;//disply_listchar<0,1,"SAVEOK",1>;//disply_listchar<0,2,"=================",1>;//disply_listchar<0,3,"TURNPOWEROFF",1>;//disply_listchar<0,4,"=================",1>;//disply_listchar<0,5,"W

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论