Other Parts Discussed in Thread: LAUNCHXL-F28379D, SN74LS14, AMC1200
Hello to everyone,
I'm looking to measure grid frequency using cmpss and ecap module of TMS320F28379D but I've some problem with the accuracy.
First of all I started with a function generator. I noticed that if the input signal is a square wave (50Hz) the measurement works (with about ±3mHz of error), if I change it to a sine wave signal the error increase a lot (form ±20 to ±50 mHz). I tried to measure also the grid frequency (maybe the connection with function generator wasn't enaough stable) and the results are almost the same. I also tried to change filter and hysteresis parameters but I didn't find a solution.
I think I'm doing something wrong because the peripherics are very fast and the frequency that I've to measure isn't so hight so I'm looking for some help. Or maybe it's necessary some external filter or other stuff (I dont' think so)?
Here's a brief descripton of the code:
The input signal arrives to ADCA4 pin used as a CMPSS (CMPSS2 hight) and hysteresis and digital filter are both configured. For the DAC it was configured a value different from 0 in order to avoid near-zero oscillation.
void initCMPSS(void) { CMPSS_enableModule(CMPSS2_BASE); CMPSS_configHighComparator(CMPSS2_BASE, CMPSS_INSRC_DAC); CMPSS_configDAC(CMPSS2_BASE, CMPSS_DACREF_VDDA | CMPSS_DACVAL_SYSCLK | CMPSS_DACSRC_SHDW); CMPSS_setDACValueHigh(CMPSS2_BASE, 2365); CMPSS_setHysteresis(CMPSS2_BASE,4); CMPSS_configFilterHigh(CMPSS2_BASE, 0x3FF, 32, 31); CMPSS_initFilterHigh(CMPSS2_BASE); CMPSS_configOutputsHigh(CMPSS2_BASE, CMPSS_TRIP_FILTER | CMPSS_TRIPOUT_FILTER); XBAR_setOutputMuxConfig(XBAR_OUTPUT3, XBAR_OUT_MUX02_CMPSS2_CTRIPOUTH); XBAR_enableOutputMux(XBAR_OUTPUT3, XBAR_MUX02); }
The out of CMPSS goes to xbar3 where a GPIO (14) is connected and, througth this one, is passed to the ECAP. Throught GPIO 14 I can also monitor the output square wave of the ecap (that seems actually a bit "disturbed" in case of sine wave).
The ECAP is configured in one shot mode and capture 2 event (one falling and one rising edge) and the ISR is connected to the second one. The ECAP is re-armed every ISR calling.
void initECAP() { 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)); ECAP_disableTimeStampCapture(ECAP1_BASE); ECAP_stopCounter(ECAP1_BASE); ECAP_enableCaptureMode(ECAP1_BASE); ECAP_setEventPrescaler(ECAP1_BASE,0); ECAP_setCaptureMode(ECAP1_BASE, ECAP_ONE_SHOT_CAPTURE_MODE, ECAP_EVENT_2); ECAP_setEventPolarity(ECAP1_BASE, ECAP_EVENT_1, ECAP_EVNT_FALLING_EDGE); ECAP_setEventPolarity(ECAP1_BASE, ECAP_EVENT_2, ECAP_EVNT_RISING_EDGE); ECAP_enableCounterResetOnEvent(ECAP1_BASE, ECAP_EVENT_2); XBAR_setInputPin(XBAR_INPUT7, 14); 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_2); }
__interrupt void ecap1ISR(void) { cap1Count = ECAP_getEventTimeStamp(ECAP1_BASE, ECAP_EVENT_1); cap2Count = ECAP_getEventTimeStamp(ECAP1_BASE, ECAP_EVENT_2); ECAP_clearInterrupt(ECAP1_BASE,ECAP_ISR_SOURCE_CAPTURE_EVENT_2); ECAP_clearGlobalInterrupt(ECAP1_BASE); ECAP_reArm(ECAP1_BASE); Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP4); }
After the ISR I calculate the frequency by dividing the clock (200MHz) by the cap2Count value. I checked in debug mode if it could be a calculation error but the ECAP counter where too much different from the excpected values (for the cap2Count I expect a value of 4000000 but it's different with about ±4000 that correspond to an error of ±50mHz).
Thank you in advance, Gaetano.
Ps. This is my first post, I hope I didn't some mistakes.