#include <hidef.h> /* common defines and macros */ #include <mc9s12dg128.h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12dg128b" unsigned int tim0,timer_num0; unsigned char impulse_num=0; byte speedget_sign1=0; void IC_init(void){ TIOS=0X00; //input capture TCTL4=0X03; //capture on both edge TSCR1=0x90; //timer enable TSCR2=0X07; // Prescale Factor 128 ICPAR_PA0EN=1; }
#pragma CODE_SEG __NEAR_SEG NON_BANKED void interrupt MDC_ISR(void){//定时器中断函数 MCFLG = 0x80; tim0++; if(tim0==20000){ //transmit the value to PC tim0=0; PORTK_BIT0=~PORTK_BIT0; } /***********Speed measure*****************/ timer_num0++; if(timer_num0==2000){ timer_num0=0; speedget_sign1=1; } } #pragma CODE_SEG DEFAULT void MDCInit(void) //定时器初始化 { MCCTL = MCCTL&0Xfb; MCCTL = 0Xe1; //4分频 MCCTL = MCCTL|0X04; MCCNT = 80; // (4/16M)*80=10us MCCTL = MCCTL|0X08 ; //loads the load register into the modulus counter count register } void PWM_init(void){ PWMCTL_CON01=1; PWMCTL_CON23=1; //两个通道联合产生PWM波 PWMCAE_CAE1=0; PWMCAE_CAE3=0; //0=left,1=center PWMPOL_PPOL1=1; PWMPOL_PPOL3=1; //PWM波的极性控制 PWMCLK_PCLK1=1; //选择SA作为时钟源 PWMCLK_PCLK3=1; //选择SB作为时钟源 PWMCNT01=0; PWMCNT23=0; //计数器清零 PWMPRCLK=0x10; //BUS CLOCK=32M CLOCKA=BUSCLOCK,CLOCKB=BUSCLOCK/4 PWMSCLA=1; PWMSCLB=4; //对PRCLK进行n*2分频 PWMDTY01=1000; PWMPER01=2500; PWMDTY23=2640; PWMPER23=40000; PWME_PWME1=1; PWME_PWME3=1; //PWM通道使能 } /*****************pll倍频********************/ void ini_PLL(void) { //REFDV=3; REFDV=3; //SYNR=5;//倍频24M SYNR=7;//倍频32M //SYNR=3; //16M while(0==CRGFLG_LOCK); CLKSEL=0X80;} void main(void) { DDRK=0XFF; PORTK=0X00; DDRA=0XFF; DDRB=0XFF; PORTAB=0X0000; //high 8 bit is porta,low 8 bit is portb //INITEE=0x21; ini_PLL(); //IC_init(); MDCInit(); PWM_init(); EnableInterrupts; for(;;) { if(speedget_sign1){ //impulse_num=PACN0; //PACN0=0; speedget_sign1=0; PORTK_BIT1=~PORTK_BIT1; } } /* wait forever */ /* please make sure that you never leave this function */ } |