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.