Dear ALL,
I used eTimer0_ch0 of MPC5744P to measure the frequency of a sensor which produces a PWM wave ,frequency(10KHz),Duty(53%). But I get the wrong frequency.Can anyone help me? Thank you ! My code is below:
void eTimer_Init(void)
{
MC_ME.PCTL247.B.RUN_CFG = 0x00; // enable group RUN_PC0 for enable clock
MC_ME.PCTL247.B.LP_CFG = 0x00; // enable group LP_PC0 for enable clock
SIUL2.MSCR[PortA0].B.IBE = 1; // PA0: Enable pad for input - eTimer0 ch0
SIUL2.IMCR[59].B.SSS = 2; // eTimer0 ch0: connected to pad PA0
ETIMER_0.ENBL.R = 0x0; // disable Timer0 channels
ETIMER_0.CH[0].CTRL1.R = 0x3F00; // Counts only rising edge of the MC_CLK (10MHz in RUN0), divide by 128, count up, count repeatedly, rollover
ETIMER_0.CH[0].COMP1.R = 0xFFFF;
ETIMER_0.CH[0].CCCTRL.R = 0x0264; // compare on COMP1 when counting up, COMP2 when counting down
// CAPT2 on falling edge, CAPT1 on rising edge, 2 entries
// free-running mode
ETIMER_0.CH[0].CTRL3.R = 1;
ETIMER_0.ENBL.R = 0x0001; // Enable Timer0 channel 2
ETIMER_0.CH[0].CCCTRL.B.ARM = 1; // starts the input capture process
}
uint16_t capture_ch0[4] = {0};
uint16_t edge1 = 0;
uint16_t edge2 = 0;
uint16_t edge3 = 0;
uint16_t edge4 = 0;
uint16_t counts1 = 0;
uint16_t counts2 = 0;
float pulseH = 0;
float pulseL = 0;
float period = 0;
float freq = 0;
float duty = 0;
void eTimer0_ch0_pro(void)
{
if(ETIMER_0.CH[0].STS.R & 0x0080)
{
capture_ch0[0] = ETIMER_0.CH[0].CAPT1.R;
capture_ch0[1] = ETIMER_0.CH[0].CAPT2.R;
capture_ch0[2] = ETIMER_0.CH[0].CAPT1.R;
capture_ch0[3] = ETIMER_0.CH[0].CAPT2.R;
edge1 = capture_ch0[0];// save 1st rising edge
edge2 = capture_ch0[1];// save 1st falling edge
edge3 = capture_ch0[2]; // save 2nd rising edge
edge4 = capture_ch0[3];// save 2nd falling edge
// calculate period, pulseH, pulseL, freq and duty
if(edge3>edge1)
{
counts1 = edge3 - edge1;
}
else
{
counts1 = (0xFFFF - edge1 +1) + edge3;
}
freq = (float)78125.0/counts1; //10M/128 = 78125
ETIMER_0.CH[0].STS.R = 0x00C0;
}
}
I run eTimer0_ch0_pro() in the while(1) of main(),
Why do I get the right frequcy?
Hi,
what is the result you got? What is the counts1 value?
You are measuring 10kHz input using 78.125 KHz reference, this way you lost a resolution a lot.
You should increase reference clock and deal with counter overflows. The overflow can be solved using cascaded channels as it is shown in https://community.nxp.com/docs/DOC-104285
BR, Petr
When MC_CLK = 10MHz, I get freq = 831.117004, period = 0.009399999,duty = 15.957448,counts1 = 94, counts2 = 15;
When MC_CLK = 120MHz,I get freq = 840.053772, period = 0.00930000003,duty = 15.0537624,counts1 = 93, counts2 = 14;
the data I get amost the same.
Hi,
if changing MC_CLK you should get different counts1 value. Try to just change the channel prescaler.
Are the capture_ch0 values changing when eTimer0_ch0_pro() is called? Try to disarm capture logic before CAPT are read (ARM=0) and finally start it back.
BR, Petr