#include <hidef.h> /* common defines and macros */ #include <MC9S12XS128.h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12xs128" void SetBusCLK_16M(void) { CLKSEL=0X00; PLLCTL_PLLON=1; SYNR=0x00 | 0x01; REFDV=0x80 | 0x01; POSTDIV=0x00; _asm(nop); _asm(nop); while(!(CRGFLG_LOCK==1)); CLKSEL_PLLSEL =1; } void PWM_45(void) { //舵机初始化 PWMCTL_CON45=1; //0和1联合成16位PWM; PWMCAE_CAE5=0; //选择输出模式为左对齐输出模式 PWMCNT45 = 0; //计数器清零; PWMPOL_PPOL5=1; //先输出高电平,计数到DTY时,反转电平 PWMPRCLK = 0X40; //clockA不频,clockA=busclock=16MHz;CLK B 16分频:1Mhz PWMSCLA = 0x08; //对clock SA 16分频,pwm clock=clockA/16=1MHz; PWMCLK_PCLK5 = 1; //选择clock SA做时钟源 PWMPER45 = 20000; //周期20ms; 50Hz; PWMDTY45 = 1510; //高电平时间为1.5ms; PWME_PWME5 = 1; } void PWM_2(void) { //驱动电机初始化 PWMPRCLK=0X60; PWMCLK_PCLK2=1; PWMSCLB=0X7D; PWMPOL_PPOL2=0XFF; PWMCAE_CAE2=0X00; PWMPER2=0X13; PWMDTY2=0X13; PWME_PWME2=1; } void main(void) { int i; int j; int k; SetBusCLK_16M(); PWM_45(); PWM_2(); while(1) { for(i=0;i<=200;i++) { for(j=0;j<=200;j++) { for(k=0;k<=1;k++) ;} } PWMDTY45=1280;//左转 PWMDTY2=0X03; for(i=0;i<=200;i++) { for(j=0;j<=200;j++) { for(k=0;k<=1;k++) ;} } PWMDTY45=1740; //右转 PWMDTY2=0X03; /* for(i=0;i<=200;i++) { for(j=0;j<=200;j++) { for(k=0;k<=1;k++) ;} } PWMDTY45=1850; //极右 PWMDTY2=0X09; for(i=0;i<=200;i++) { for(j=0;j<=200;j++) { for(k=0;k<=1;k++) ;} } PWMDTY45=1050; //极左 PWMDTY2=0X09; for(i=0;i<=200;i++) { for(j=0;j<=200;j++) { for(k=0;k<=1;k++) ;} } PWMDTY45=1510; //摆正 PWMDTY2=0X00; */ for(i=0;i<=200;i++) { for(j=0;j<=200;j++) { for(k=0;k<=1;k++) ;} } PWMDTY45=1510; } } |
欢迎光临 智能车制作 (http://dns.znczz.com/) | Powered by Discuz! X3.2 |