/*********************************************************************** * Function Name :ECAPRegsInit * Arguments : * Returns : * Description : Configure ECap Register * Updated ID : ***********************************************************************/ void ECAPRegsInit(void) { EALLOW; CpuSysRegs.PCLKCR3.bit.ECAP1 = 1; //Open EMIF1 clock EDIS; EALLOW; ECap1Regs.ECCTL1.bit.CAP1POL=0; //rising edge cap1 ECap1Regs.ECCTL1.bit.CTRRST1=1; // reset ctr ECap1Regs.ECCTL1.bit.CAPLDEN=1; //enable load on cap; ECap1Regs.ECCTL1.bit.PRESCALE=0; //no prescale ECap1Regs.ECCTL2.bit.CAP_APWM=0; //cap mode ECap1Regs.ECCTL2.bit.CONT_ONESHT=0;//continous mode ECap1Regs.ECCTL2.bit.STOP_WRAP=0;// wrap after cap1 ECap1Regs.ECCTL2.bit.SYNCI_EN=0; ECap1Regs.ECCTL2.bit.SYNCO_SEL=2; ECap1Regs.ECCLR.all=0xff; ECap1Regs.ECCTL2.bit.TSCTRSTOP=1;//ctr runing EDIS; } /*********************************************************************** * Function Name£º FreqCap * Arguments : * Returns : * Description : Cap Frequency * Updated ID : ***********************************************************************/ void FreqCap(void) { unsigned long temp1=0; static Uint16 u16EcapRstDelayCnt = 0; if((ECap1Regs.ECFLG.bit.CEVT1) && (ZERO == u16EcapRstDelayCnt)) // New Ecap Occur { uwFreCapFlag = 1; u16EcapRstDelayCnt++; // cnt++ uwCapfreqms=0; ulGridInperOld=ulGridInperNew; // load Ecap counter and calculate period ulGridInperNew = ECap1Regs.CAP1; ulGridInPeriod=ulGridInperNew+ulGridInperOld; if(ulGridInPeriod >=3076923 && ulGridInPeriod<=5000000) //the value is selected for robust to noise ,40Hz~70Hz { ubCaptureLostCnt=0; temp1=__divf32(200000000000,ulGridInPeriod); uwGrdCapFreq = (unsigned int)temp1;//+4; //5000=50.00Hz //0.01Hz } else if(ulGridInPeriod <3076923)//2857143) { uwGrdCapFreq=65000; } else { uwGrdCapFreq=40000; } } else if((ECap1Regs.ECFLG.bit.CEVT1) && (ZERO != u16EcapRstDelayCnt)) // Software Dead band { u16EcapRstDelayCnt++; if(u16EcapRstDelayCnt >= 22) //dead band time = (22-1)*Ts/2 = 3ms { if(ECap1Regs.ECCTL1.bit.CAP1POL) { EALLOW; ECap1Regs.ECCTL1.bit.CAP1POL = 0; // If falling edge then rising edge next time EDIS; } else { EALLOW; ECap1Regs.ECCTL1.bit.CAP1POL = 1; // otherwise, falling edge next time EDIS; } u16EcapRstDelayCnt = ZERO; // reset Delay cnt; EALLOW; ECap1Regs.ECCLR.bit.CEVT1 = 1; //clear CaptureEvent1 flag ECap1Regs.ECCTL2.bit.REARM = 1; // Mod Counter is frozen after Cap1, unfreeze it EDIS; } } else { uwFreCapFlag = 0; } }