您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 汽车理论 > 飞思卡尔智能车程序汇总
1.流水灯程序:#includehidef.h/*commondefinesandmacros*/#includederivative.h/*derivative-specificdefinitions*/voiddelay(unsignedintms){unsignedinti;unsignedcharj;for(i=0;ims;i++){for(j=0;j100;j++);}}voidmain(void){/*putyourowncodehere*/EnableInterrupts;DDRA=0x00;DDRB_DDRB0=1;for(;;){delay(25000);PORTB_PB0=1;delay(25000);PORTB_PB0=0;}}2.液晶屏按键程序#includehidef.h/*commondefinesandmacros*/#includederivative.h/*derivative-specificdefinitions*/#includelcd5110.h#include24c02.h#includebutton.hvoiddelay(unsignedintms){unsignedinti;unsignedcharj;for(i=0;ims;i++){for(j=0;j100;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;i12;i++)setnum[i]=RdEEPROM(i);//读取24c02for(i=0;i12;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;i12;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_sel11)cur_sel=0;for(i=0;i12;i++)disply_listchar((i/6)*42,i%6,lcd_show[i],(cur_sel==i)?0:1);}if(btn_sav()){for(i=0;i12;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,);while(1);}}}DDRA=0x00;DDRB_DDRB0=1;for(;;){delay(setnum[0]*100);PORTB_PB0=1;delay(setnum[1]*100);PORTB_PB0=0;}}3.电机程序#includehidef.h/*commondefinesandmacros*/#includederivative.h/*derivative-specificdefinitions*/#includelcd5110.h#include24c02.h#includebutton.h#defineservo_period20//ms#definemotor_pwm_frequ1//khzvoiddelay(unsignedintms){unsignedinti;unsignedcharj;for(i=0;ims;i++){for(j=0;j100;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;i12;i++)setnum[i]=RdEEPROM(i);//读取24c02for(i=0;i12;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;i12;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_sel11)cur_sel=0;for(i=0;i12;i++)disply_listchar((i/6)*42,i%6,lcd_show[i],(cur_sel==i)?0:1);}if(btn_sav()){for(i=0;i12;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,);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.码盘控速#includehidef.h
本文标题:飞思卡尔智能车程序汇总
链接地址:https://www.777doc.com/doc-5558141 .html