This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F280025: Measurement error with eCAP

Part Number: TMS320F280025

We are using eCAP module to measure the frequency of the grid voltage. The grid voltage is converted to a square waveform using comparator with hysteresis. In the initial testing we are providing clean square wave from a function generator. The square wave has a frequency of 50 Hz i.e time period of 20000 uSec. As the controller is running at 100 MHz with 20 MHz external crystal so we assume eCAP resolution of 10 nSec. As such the eCAP counter measurement should be around 2000000. However the actual reading is coming around 20000230. This is a big error of about 2.3 u Sec. Please guide me about the possible source of this error and how to improve it. While coding, I have taken reference from example project in C2000 ware.

The configuration code for the eCAP module is as below:

void eCAP_init()
{
    //
        // Disable ,clear all capture flags and interrupts
        //
        ECAP_disableInterrupt(ECAP1_BASE,
                              (ECAP_ISR_SOURCE_CAPTURE_EVENT_1  |
                               ECAP_ISR_SOURCE_CAPTURE_EVENT_2  |
                               ECAP_ISR_SOURCE_CAPTURE_EVENT_3  |
                               ECAP_ISR_SOURCE_CAPTURE_EVENT_4  |
                               ECAP_ISR_SOURCE_COUNTER_OVERFLOW |
                               ECAP_ISR_SOURCE_COUNTER_PERIOD   |
                               ECAP_ISR_SOURCE_COUNTER_COMPARE));
        ECAP_clearInterrupt(ECAP1_BASE,
                            (ECAP_ISR_SOURCE_CAPTURE_EVENT_1  |
                             ECAP_ISR_SOURCE_CAPTURE_EVENT_2  |
                             ECAP_ISR_SOURCE_CAPTURE_EVENT_3  |
                             ECAP_ISR_SOURCE_CAPTURE_EVENT_4  |
                             ECAP_ISR_SOURCE_COUNTER_OVERFLOW |
                             ECAP_ISR_SOURCE_COUNTER_PERIOD   |
                             ECAP_ISR_SOURCE_COUNTER_COMPARE));

        //
        // Disable CAP1-CAP4 register loads
        //
        ECAP_disableTimeStampCapture(ECAP1_BASE);

        ECAP_stopCounter(ECAP1_BASE);         //Counter is stopped
        ECAP_enableCaptureMode(ECAP1_BASE);   //Capture mode enabled

        ECAP_setCaptureMode(ECAP1_BASE, ECAP_CONTINUOUS_CAPTURE_MODE, ECAP_EVENT_1);

        ECAP_setEventPolarity(ECAP1_BASE, ECAP_EVENT_1, ECAP_EVNT_RISING_EDGE);
       



        ECAP_enableCounterResetOnEvent(ECAP1_BASE, ECAP_EVENT_1);
      
        ECAP_selectECAPInput(ECAP1_BASE,ECAP_INPUT_INPUTXBAR4);

        ECAP_enableLoadCounter(ECAP1_BASE);
        ECAP_setSyncOutMode(ECAP1_BASE, ECAP_SYNC_OUT_DISABLED);
        ECAP_startCounter(ECAP1_BASE);
        ECAP_enableTimeStampCapture(ECAP1_BASE);
        ECAP_reArm(ECAP1_BASE);

        ECAP_enableInterrupt(ECAP1_BASE, ECAP_ISR_SOURCE_CAPTURE_EVENT_1);
        

}

The counter value is being measured in the capture event 1 ISR as below:

__interrupt void
 ecap1ISR(void)
 {

             GPIO_togglePin(6);

             Freq_Count = ECAP_getEventTimeStamp(ECAP1_BASE, ECAP_EVENT_1); //Counter value at next falling edge



             //
             // Clear interrupt flags for more interrupts.
             //
             ECAP_clearInterrupt(ECAP1_BASE,
                                                          (ECAP_ISR_SOURCE_CAPTURE_EVENT_1  |
                                                           ECAP_ISR_SOURCE_CAPTURE_EVENT_2  |
                                                           ECAP_ISR_SOURCE_CAPTURE_EVENT_3  |
                                                           ECAP_ISR_SOURCE_CAPTURE_EVENT_4  |
                                                           ECAP_ISR_SOURCE_COUNTER_OVERFLOW |
                                                           ECAP_ISR_SOURCE_COUNTER_PERIOD   |
                                                           ECAP_ISR_SOURCE_COUNTER_COMPARE));
             ECAP_clearGlobalInterrupt(ECAP1_BASE);


             // Start eCAP

             ECAP_reArm(ECAP1_BASE);                  //CHECK IF RE-ARMING IS REQUIRED

             // Acknowledge the group interrupt for more interrupts.

             Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP4);




 }

The GPIO is configured as below:

GPIO_setDirectionMode(JEM_FREQ_MEASURE, GPIO_DIR_MODE_IN);
GPIO_setPadConfig(JEM_FREQ_MEASURE, GPIO_PIN_TYPE_PULLUP);
GPIO_setQualificationMode(JEM_FREQ_MEASURE, GPIO_QUAL_6SAMPLE);

Kindly let me know if more information is required to diagnose the issue.

  • Hi Asad,

    You will need to capture 2 events instead of 1. Since you are just looking to measure frequency, you can setup the 2 events to capture 2 rising edges or 2 falling edges. Use delta mode so the capture of your 2nd event will be the frequency. Let us know if this is not clear.

  • Dear Frank, 

    I will try using two events as advised by you. In the eCap configuration, I have set counter value to be cleared on rising edge so I was expecting that the next rising edge should contain time period of the last cycle. Please let me know why this approach didn't work.

    Kindly advise on one more aspect of frequency measurement: I am updating the value of frequency on every eCap rising event. If there is sudden disconnection then there should be no more eCap rising event. So the frequency measurement freezes to the last measured value. Should I look for some counter overflow to make the frequency zero or any better approach? 

  • Dear Frank,

       I have tried using two events but the results remain the same. I think that my original configurations are already set for differential measurement as the counter is being reset in the rising event. I have made some observations with two external signal as shown below:

    Readings on external 50 Hz signal:
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000220
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:1999791
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:1999791
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219
    Freq. Counter:2000219

    Readings on external 60 Hz signal:
    Freq. Counter:1666811
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666811
    Freq. Counter:1666811
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666811
    Freq. Counter:1666812
    Freq. Counter:1666811
    Freq. Counter:1666811
    Freq. Counter:1666811
    Freq. Counter:1666811
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666811
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666811
    Freq. Counter:1666812
    Freq. Counter:1666811
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666812
    Freq. Counter:1666811
    Freq. Counter:1666812

    So there is a fix error coming from the two external measurements. To verify if my configurations are correct, I generated a 20 KHz waveform from the controller and captured its time period.  The readings are given below.

    Readings on self generated 20 KHz PWM
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999
    Freq. Counter:4999

    The above readings are very accurate i.e. 49.99 uSec as compared to error free value of 50.00 uSec. It appears that there is some error in the clock of the external square wave generator ( Digilent Analog Discovery Module Rev. C) or my TMS320F280025C launch pad clock. Please share your thoughts on this and way forward.

  • Hi Asad,

    Kindly advise on one more aspect of frequency measurement: I am updating the value of frequency on every eCap rising event. If there is sudden disconnection then there should be no more eCap rising event. So the frequency measurement freezes to the last measured value. Should I look for some counter overflow to make the frequency zero or any better approach? 

    Yes, the eCAP has an overflow flag you can use to check this.

    So there is a fix error coming from the two external measurements. To verify if my configurations are correct, I generated a 20 KHz waveform from the controller and captured its time period.  The readings are given below.

    Have you verified these external measurements with something else? Maybe a frequency counter or scope?

    The above readings are very accurate i.e. 49.99 uSec as compared to error free value of 50.00 uSec. It appears that there is some error in the clock of the external square wave generator ( Digilent Analog Discovery Module Rev. C) or my TMS320F280025C launch pad clock. Please share your thoughts on this and way forward.

    Please verify the 2 independently. It can be that your external square wave generator is out of calibration. You should verify it with a scope or frequency counter. The launchpad uses a crystal and shouldn't have this much frequency variation but you can verify the launchpad clock by bringing out the system clock on XCLKOUT and measuring it.

  • Dear Frank,

        I understand that overflow will occur when the counter reaches 2^32. This is a big time lapse 2^32 * 10 nSec= 42.949 seconds. We have requirement to detect grid loss in two cycles at most i.e. max 40 mSec @ 50Hz. To achieve this I have used a timer set to overflow at 25 mSec. In eCap ISR I am clearing the timer so that overflow does not occur. However in case of sudden loss of frequency signal the eCap ISR will not occur so that timer overflows. When the timer overflows i set frequency equal to zero. Kindly guide if using the eCAP overflow will be more efficient approach and how to achieve 40 mSec time constraint with it.

    I have already worked on your suggestion to compare two sources independently. I have tried measuring the output from signal generator and also time period of PWM from the launchpad with an external oscilloscope. Both signal appear to be accurately timed. Let me also check the XCLKOUT signal as suggested by you.

  •     I understand that overflow will occur when the counter reaches 2^32. This is a big time lapse 2^32 * 10 nSec= 42.949 seconds. We have requirement to detect grid loss in two cycles at most i.e. max 40 mSec @ 50Hz. To achieve this I have used a timer set to overflow at 25 mSec. In eCap ISR I am clearing the timer so that overflow does not occur. However in case of sudden loss of frequency signal the eCap ISR will not occur so that timer overflows. When the timer overflows i set frequency equal to zero. Kindly guide if using the eCAP overflow will be more efficient approach and how to achieve 40 mSec time constraint with it.

    I understand. In that case, your solution is good.

    I have already worked on your suggestion to compare two sources independently. I have tried measuring the output from signal generator and also time period of PWM from the launchpad with an external oscilloscope. Both signal appear to be accurately timed. Let me also check the XCLKOUT signal as suggested by you.

    Since you have verified the PWM frequency, that indirectly verifies the system clock so there is no need to check the XCLKOUT. Try one experiment: Generate a 50Hz square wave with the PWM and feed it to the ECAP to see if you get the correct result.