TMS320F28P550SJ: Need an example code using comparator and ADC at same pin.

Part Number: TMS320F28P550SJ
Other Parts Discussed in Thread: C2000WARE

Hello Team , 

I am using using TMS320F28P550 MCU for my solar application .This is 80 pin pakage. I wish to use ADC and Comparator together : I am using ADC A4 , for measuring AC voltage . Please share example code that is using  ADC with COMP for frequency measurement.

 

Regards,

Atul kaushik

  • Hello Atul,

    Apologies for the delayed response over the holiday weekend

    I need more information on what you are looking for here. Frequency measurement could be achieved with just the ADC or the CMPSS. We provide example codes for both ADC and CMPSS peripherals to showcase specific use-cases or peripheral features. The analog channels of the F28P550x device are implemented in such a way that the analog signal on the pin is always hardwire connected to all the associated analog peripherals also available on that pin. It is just a matter of whether the associated analog peripherals are configured to used that associated analog pin as the source that they are used or not.

    Looking forward to assisting you further once additional details are available.

    Best Regards,

    Zackary Fleenor

  • Hey Atul,

    Just checking in, do you have any additional updates from your side?

    Best Regards,

    Zackary Fleenor

  • Hi Fleenor,

    Thank you for your inputs.

    The grid AC signal is connected at A4 adc pin. The same signal i am using for the measurement of RMS voltage. Now i need to measure frequency of grid signal connected at A4. For this I want to use internal comparator for generating ZCD. Now i have few questions 

    1. Can it be possible to generate ZCD from internal comparator ?

    2. Can it be possible to do internal connection between comparator and ECAP. if yes, please share how it can be possible because I know how to connect it using GPIOs but it will not work for my use case. 

    So, my use case would be like this

    A4 ADC===>>CMPSS===>>ECAP.

    All connections will be internal, no external connection is required .

    Regards,

    Atul kaushik

  • Hi Atul,

    Yes, your desired signal path A4 → CMPSS → ECAP (all internal) is achievable on the TMS320F28P550SJ. Here's how each piece works:

    1. A4 Pin → ADC + CMPSS Simultaneously

    The analog signal on A4 is hardwired to both the ADC and CMPSS inputs — no configuration is needed to "share" the pin [1][2]. You can sample A4 with the ADC for RMS voltage measurement while simultaneously using CMPSS for zero-crossing detection. Specifically, per Table 14-4 in the TRM, CMPSS-2 HP input mux (HPMXSEL0) selects channel A4 [1].

    2. CMPSS for Zero-Cross Detection (ZCD)

    Yes, the internal comparator can generate ZCD. Configure CMPSS-2 as follows:

    • Set the positive input mux to select A4 (your grid signal)
    • Configure the internal 12-bit DAC as the negative reference, set to your zero-crossing threshold (the DC bias point of your scaled AC signal)
    • Enable the digital filter to debounce noise near the zero crossing [3][4]
    • The comparator outputs CTRIPOUTH/CTRIPOUTL signals that toggle at each zero crossing

    3. Internal CMPSS → ECAP Routing via X-BAR

    This is the key to avoiding external GPIO connections. The TMS320F28P55x includes an X-BAR (crossbar) interconnect system that routes CMPSS outputs internally to other peripherals including eCAP [5]:

    • CMPSS outputs (CTRIPOUTH, CTRIPOUTL, CTRIPH, CTRIPL) feed into the X-BAR as input sources [5]
    • The Input X-BAR can route these signals to eCAP inputs without going through a physical GPIO pin

    The configuration flow:

    1. Configure CMPSS-2 with A4 as input and DAC at your zero-crossing threshold
    2. Use XBAR_setInputPin() or the equivalent Input X-BAR register to route the CMPSS-2 trip output to the eCAP input
    3. Configure eCAP in capture mode to measure the period between successive zero crossings → this gives you grid frequency

    Example Code Starting Points

    We don't provide a single combined ADC+CMPSS+ECAP example for this exact use case, but C2000Ware includes relevant building blocks [3][4]:

    • cmpss_ex1_asynch.c — Demonstrates CMPSS configuration with internal DAC as reference and routing CTRIPOUTH to Output X-BAR [3]
    • cmpss_ex2_digital_filter.c — Shows digital filter configuration for noise rejection on the comparator output [4]

    You'll need to adapt these by:

    • Changing the CMPSS input mux to select A4
    • Routing the CMPSS output through Input X-BAR to your eCAP module instead of to Output X-BAR/GPIO
    • Configuring eCAP to capture rising/falling edges for period measurement

    Additionally, the POWER_MEAS_SINE_ANALYZER library in C2000Ware DigitalPower SDK provides a software-based approach to grid frequency measurement using ADC samples with threshold-based zero-crossing detection, which could serve as a complementary or alternative method [6].


    To help refine this recommendation, it would be helpful to know:

    • Which specific Input X-BAR instance and eCAP module you plan to use (this determines the exact register configuration)
    • Whether you need the high-side comparator (CTRIPH) or low-side (CTRIPL) for your zero-crossing — this depends on whether your AC signal crosses the DAC threshold going up or down
    • Your grid frequency (50Hz or 60Hz) — this affects recommended digital filter settings
    • The DC bias and amplitude of your scaled grid signal at A4, to properly set the DAC threshold value

    Resources:

    1. TMS320F28P559SJ-Q1: Analog Subsystem Connection Configuration (E2E)
    2. TMS320F28P55x Technical Reference Manual - Analog Subsystem Block Diagram
    3. TMS320F28P55x TRM - CMPSS Asynchronous Trip Example
    4. TMS320F28P55x TRM - CMPSS Digital Filter Example
    5. TMS320F28P55x TRM - X-BAR Input Sources
    6. POWER_MEAS_SINE_ANALYZER Library Documentation

    Best Regards,

    Zackary Fleenor

  • Hi Fleenor,

    Thank you for your inputs.

    I am able to do it.

    Regards,

    Atul Kaushik

  • Hi Fleenor,

    Sorry for reopening of this thread again.

    Now i have following configurations 

    A4 >> CMPSS2>>ECAP1

    The ADC pin A4 is connected to CMPSS2 . The output of CMPSS2 to input of ECAP1. I am following code for CMPSS and ECAP. I am not using OUTXBAR and INPUTXBAR. The problem the i am facing is 

    1. I am continuously receiving interrupt on ecap without giving signal at A4 Pin.

    Please check below the configurations of ECAP and CMPSS and suggest .

    Also check my response to you queries . Please help how we can able to connect CMPSS output with input xbar

    To help refine this recommendation, it would be helpful to know:

    • Which specific Input X-BAR instance and eCAP module you plan to use (this determines the exact register configuration)  : 

                 input xbar 7 , Ecap 1

    • Whether you need the high-side comparator (CTRIPH) or low-side (CTRIPL) for your zero-crossing — this depends on whether your AC signal crosses the DAC threshold going up or down

                           CTRIPH

    • Your grid frequency (50Hz or 60Hz) — this affects recommended digital filter settings                       50 HZ
    • The DC bias and amplitude of your scaled grid signal at A4, to properly set the DAC threshold value

     

    void myECAP0_init(){

    //
    // Disable ,clear all capture flags and interrupts
    //
    ECAP_disableInterrupt(myECAP0_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(myECAP0_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));
    //
    // Disables time stamp capture.
    //
    ECAP_disableTimeStampCapture(myECAP0_BASE);
    //
    // Stops Time stamp counter.
    //
    ECAP_stopCounter(myECAP0_BASE);
    //
    // Sets eCAP in Capture mode.
    //
    ECAP_enableCaptureMode(myECAP0_BASE);
    //
    // Sets the capture mode.
    //
    ECAP_setCaptureMode(myECAP0_BASE,ECAP_ONE_SHOT_CAPTURE_MODE,ECAP_EVENT_4);
    //
    // Sets the Capture event prescaler.
    //
    ECAP_setEventPrescaler(myECAP0_BASE, 0U);
    //
    // Sets the Capture event polarity.
    //
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_1,ECAP_EVNT_FALLING_EDGE);
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_2,ECAP_EVNT_RISING_EDGE);
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_3,ECAP_EVNT_FALLING_EDGE);
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_4,ECAP_EVNT_RISING_EDGE);
    //
    // Configure counter reset on events
    //
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_1);
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_2);
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_3);
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_4);
    //
    // Select eCAP input.
    //
    ECAP_selectECAPInput(myECAP0_BASE,ECAP_INPUT_OUTPUTXBAR3);//ECAP_INPUT_CMPSS2_CTRIP_HIGH);
    //
    // Sets a phase shift value count.
    //
    ECAP_setPhaseShiftCount(myECAP0_BASE,0U);
    //
    // Enable counter loading with phase shift value.
    //
    ECAP_enableLoadCounter(myECAP0_BASE);
    //
    // Configures Sync out signal mode.
    //
    ECAP_setSyncOutMode(myECAP0_BASE,ECAP_SYNC_OUT_SYNCI);
    //
    // Configures emulation mode.
    //
    ECAP_setEmulationMode(myECAP0_BASE,ECAP_EMULATION_STOP);
    //
    // Set up the source for sync-in pulse..
    //
    ECAP_setSyncInPulseSource(myECAP0_BASE,ECAP_SYNC_IN_PULSE_SRC_DISABLE);
    //
    // Starts Time stamp counter for myECAP0.
    //
    ECAP_startCounter(myECAP0_BASE);
    //
    // Enables time stamp capture for myECAP0.
    //
    ECAP_enableTimeStampCapture(myECAP0_BASE);
    //
    // Re-arms the eCAP module for myECAP0.
    //
    ECAP_reArm(myECAP0_BASE);
    //
    // Enables interrupt source for myECAP0.
    //
    ECAP_enableInterrupt(myECAP0_BASE,(ECAP_ISR_SOURCE_CAPTURE_EVENT_4));

    void myECAP0_init(){

    //
    // Disable ,clear all capture flags and interrupts
    //
    ECAP_disableInterrupt(myECAP0_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(myECAP0_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));
    //
    // Disables time stamp capture.
    //
    ECAP_disableTimeStampCapture(myECAP0_BASE);
    //
    // Stops Time stamp counter.
    //
    ECAP_stopCounter(myECAP0_BASE);
    //
    // Sets eCAP in Capture mode.
    //
    ECAP_enableCaptureMode(myECAP0_BASE);
    //
    // Sets the capture mode.
    //
    ECAP_setCaptureMode(myECAP0_BASE,ECAP_ONE_SHOT_CAPTURE_MODE,ECAP_EVENT_4);
    //
    // Sets the Capture event prescaler.
    //
    ECAP_setEventPrescaler(myECAP0_BASE, 0U);
    //
    // Sets the Capture event polarity.
    //
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_1,ECAP_EVNT_FALLING_EDGE);
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_2,ECAP_EVNT_RISING_EDGE);
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_3,ECAP_EVNT_FALLING_EDGE);
    ECAP_setEventPolarity(myECAP0_BASE,ECAP_EVENT_4,ECAP_EVNT_RISING_EDGE);
    //
    // Configure counter reset on events
    //
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_1);
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_2);
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_3);
    ECAP_enableCounterResetOnEvent(myECAP0_BASE,ECAP_EVENT_4);
    //
    // Select eCAP input.
    //
    ECAP_selectECAPInput(myECAP0_BASE,ECAP_INPUT_OUTPUTXBAR3);//ECAP_INPUT_CMPSS2_CTRIP_HIGH);
    //
    // Sets a phase shift value count.
    //
    ECAP_setPhaseShiftCount(myECAP0_BASE,0U);
    //
    // Enable counter loading with phase shift value.
    //
    ECAP_enableLoadCounter(myECAP0_BASE);
    //
    // Configures Sync out signal mode.
    //
    ECAP_setSyncOutMode(myECAP0_BASE,ECAP_SYNC_OUT_SYNCI);
    //
    // Configures emulation mode.
    //
    ECAP_setEmulationMode(myECAP0_BASE,ECAP_EMULATION_STOP);
    //
    // Set up the source for sync-in pulse..
    //
    ECAP_setSyncInPulseSource(myECAP0_BASE,ECAP_SYNC_IN_PULSE_SRC_DISABLE);
    //
    // Starts Time stamp counter for myECAP0.
    //
    ECAP_startCounter(myECAP0_BASE);
    //
    // Enables time stamp capture for myECAP0.
    //
    ECAP_enableTimeStampCapture(myECAP0_BASE);
    //
    // Re-arms the eCAP module for myECAP0.
    //
    ECAP_reArm(myECAP0_BASE);
    //
    // Enables interrupt source for myECAP0.
    //
    ECAP_enableInterrupt(myECAP0_BASE,(ECAP_ISR_SOURCE_CAPTURE_EVENT_4));

    }

  • Hi Atul,

    Your main issues are:

    1. You're using ECAP_INPUT_OUTPUTXBAR3 instead of routing through Input X-BAR — the Output X-BAR is floating/toggling, causing spurious interrupts
    2. CMPSS2 is not properly configured — missing DAC threshold and filter settings
    3. Missing Input X-BAR configuration to connect CMPSS2 CTRIPH → INPUTXBAR7 → ECAP1


    Complete Working Configuration

    Signal Path

    A4 Pin → CMPSS2 (CTRIPH) → INPUT XBAR 7 → ECAP1


    Step 1: CMPSS2 Configuration

    void myCMPSS2_init(void)
    {
        //
        // Enable CMPSS2
        //
        CMPSS_enableModule(myCMPSS2_BASE);
    
        //
        // Use VDDA as high reference for internal DAC
        // Configure high comparator to use internal DAC as negative input
        //
        CMPSS_configHighComparator(myCMPSS2_BASE,
                                   (CMPSS_INSRC_PIN));
        // CMPSS_INSRC_PIN selects the external pin (A4) as positive input
        // Internal DAC is automatically used as negative reference
    
        //
        // Set DAC value for zero-crossing threshold
        // If your AC signal is biased at 1.65V (mid-rail of 3.3V VDDA):
        // DAC value = (threshold_voltage / VDDA) * 4095
        // For 1.65V threshold: (1.65 / 3.3) * 4095 = 2047
        // *** ADJUST THIS VALUE based on your actual DC bias point ***
        //
        CMPSS_setDACValueHigh(myCMPSS2_BASE, 2047U);
    
        //
        // Configure DAC to use VDDA as reference, updated on sysclock
        //
        CMPSS_configDAC(myCMPSS2_BASE,
                        (CMPSS_DACVAL_SYSCLK  |
                         CMPSS_DACSRC_SHDW));
    
        //
        // Configure digital filter on high comparator output
        // For 50Hz grid, filter should reject noise but pass 50Hz transitions
        // Filter window: sets number of samples that must agree
        // At 200MHz sysclk:
        //   CLKPRESCALE = 200  → filter clock = 1MHz
        //   SAMPWIN     = 30   → 30 samples window
        //   THRESH      = 18   → 18 of 30 must agree (majority)
        //
        CMPSS_configFilterHigh(myCMPSS2_BASE,
                               200U,   // CLKPRESCALE: filter clock prescaler
                               30U,    // SAMPWIN:     sample window size
                               18U);   // THRESH:      majority threshold
    
        //
        // Initialize filter logic and start filtering
        //
        CMPSS_initFilterHigh(myCMPSS2_BASE);
    
        //
        // Configure CTRIPOUTH (filtered output) to be used
        // Set output to be non-inverted (active high when A4 > DAC)
        //
        CMPSS_configOutputsHigh(myCMPSS2_BASE,
                                (CMPSS_TRIP_FILTER |
                                 CMPSS_TRIPOUT_FILTER));
        // CMPSS_TRIP_FILTER    = filtered signal drives CTRIPH (to X-BAR)
        // CMPSS_TRIPOUT_FILTER = filtered signal drives CTRIPOUTH (to Output X-BAR)
    
        //
        // Latch mode: use asynchronous (no latch) for ZCD
        //
        CMPSS_configLatchOnPWMSYNC(myCMPSS2_BASE, false, false);
    }


    Step 2: Input X-BAR Configuration (CRITICAL MISSING PIECE)

    void myINPUTXBAR_init(void)
    {
        //
        // Route CMPSS2 CTRIPH (filtered high comparator output)
        // to INPUT XBAR 7, which feeds ECAP1 input
        //
        // XBAR_INPUT7 corresponds to eCAP1 input when selected via
        // ECAP_selectECAPInput(base, ECAP_INPUT_INPUTXBAR7)
        //
        XBAR_setInputPin(INPUTXBAR_BASE,
                         XBAR_INPUT7,
                         XBAR_IN_MUX_CMPSS2_CTRIPH);
        //
        // NOTE: XBAR_IN_MUX_CMPSS2_CTRIPH selects the filtered
        // CTRIPH output from CMPSS2 as the source for INPUT7
        //
    }


    Step 3: ECAP1 Configuration (Corrected)

    void myECAP1_init(void)
    {
        //
        // Disable and clear all capture flags and interrupts first
        //
        ECAP_disableInterrupt(myECAP0_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(myECAP0_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));
    
        //
        // Stop counter and disable capture during configuration
        //
        ECAP_stopCounter(myECAP0_BASE);
        ECAP_disableTimeStampCapture(myECAP0_BASE);
    
        //
        // Set eCAP in Capture mode (not APWM mode)
        //
        ECAP_enableCaptureMode(myECAP0_BASE);
    
        //
        // For 50Hz frequency measurement:
        // Use CONTINUOUS mode with EVENT_2 to capture one full period
        // EVENT_1: Rising edge  (positive zero crossing)
        // EVENT_2: Rising edge  (next positive zero crossing = one period)
        //
        // OR use ONE_SHOT with EVENT_2 for single measurement then re-arm
        //
        ECAP_setCaptureMode(myECAP0_BASE,
                            ECAP_CONTINUOUS_CAPTURE_MODE,
                            ECAP_EVENT_2);
    
        //
        // Event polarity configuration for zero-crossing detection:
        // EVENT_1: Rising edge  = CMPSS output goes HIGH (A4 crosses DAC upward)
        // EVENT_2: Rising edge  = next rising edge = one full period
        //
        ECAP_setEventPolarity(myECAP0_BASE,
                              ECAP_EVENT_1, ECAP_EVNT_RISING_EDGE);
        ECAP_setEventPolarity(myECAP0_BASE,
                              ECAP_EVENT_2, ECAP_EVNT_RISING_EDGE);
    
        //
        // Reset counter on EVENT_1 so EVENT_2 captures period directly
        // Do NOT reset on EVENT_2 (so next EVENT_1 starts fresh period)
        //
        ECAP_enableCounterResetOnEvent(myECAP0_BASE,  ECAP_EVENT_1);
        ECAP_disableCounterResetOnEvent(myECAP0_BASE, ECAP_EVENT_2);
    
        //
        // Set prescaler (no prescaling needed for 50Hz)
        //
        ECAP_setEventPrescaler(myECAP0_BASE, 0U);
    
        //
        // *** KEY FIX: Select INPUTXBAR7 as eCAP input source ***
        // This connects the CMPSS2 output routed through Input X-BAR 7
        //
        ECAP_selectECAPInput(myECAP0_BASE, ECAP_INPUT_INPUTXBAR7);
    
        //
        // Disable sync (not needed for frequency measurement)
        //
        ECAP_setSyncInPulseSource(myECAP0_BASE,
                                  ECAP_SYNC_IN_PULSE_SRC_DISABLE);
        ECAP_setSyncOutMode(myECAP0_BASE, ECAP_SYNC_OUT_SYNCI);
        ECAP_disableLoadCounter(myECAP0_BASE);
    
        //
        // Set emulation mode - stop counter on emulation suspend
        //
        ECAP_setEmulationMode(myECAP0_BASE, ECAP_EMULATION_STOP);
    
        //
        // Enable interrupt on EVENT_2 (full period captured)
        //
        ECAP_enableInterrupt(myECAP0_BASE,
                             ECAP_ISR_SOURCE_CAPTURE_EVENT_2);
    
        //
        // Start counter and enable capture
        //
        ECAP_startCounter(myECAP0_BASE);
        ECAP_enableTimeStampCapture(myECAP0_BASE);
        ECAP_reArm(myECAP0_BASE);
    }


    Step 4: ISR for Frequency Calculation

    //
    // Globals for frequency measurement
    //
    volatile float32_t gridFrequency_Hz   = 0.0f;
    volatile uint32_t  capEvent1_count    = 0U;
    volatile uint32_t  capEvent2_count    = 0U;
    
    //
    // System clock frequency in Hz (adjust to your device config)
    //
    #define DEVICE_SYSCLK_FREQ  200000000UL   // 200 MHz
    
    __interrupt void ecap1ISR(void)
    {
        uint32_t periodCount;
    
        //
        // Read captured timestamps
        // CAP1 = timestamp of EVENT_1 (first rising edge, counter reset here)
        // CAP2 = timestamp of EVENT_2 (second rising edge = period count)
        //
        capEvent1_count = ECAP_getEventTimeStamp(myECAP0_BASE, ECAP_EVENT_1);
        capEvent2_count = ECAP_getEventTimeStamp(myECAP0_BASE, ECAP_EVENT_2);
    
        //
        // Since counter resets on EVENT_1, CAP2 directly holds the period
        // in system clock counts
        //
        periodCount = capEvent2_count;
    
        //
        // Calculate frequency: f = sysclk / period_counts
        //
        if(periodCount > 0U)
        {
            gridFrequency_Hz = (float32_t)DEVICE_SYSCLK_FREQ /
                               (float32_t)periodCount;
        }
    
        //
        // Clear interrupt flag
        //
        ECAP_clearInterrupt(myECAP0_BASE,
                            ECAP_ISR_SOURCE_CAPTURE_EVENT_2);
        ECAP_clearGlobalInterrupt(myECAP0_BASE);
    
        //
        // Re-arm for next capture (only needed in ONE_SHOT mode)
        // In CONTINUOUS mode, this is optional but harmless
        //
        // ECAP_reArm(myECAP0_BASE);
    
        //
        // Acknowledge PIE group for eCAP1 interrupt
        // eCAP1 is in PIE group 4 (check your device PIE table)
        //
        Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP4);
    }


    Step 5: Main Initialization Order

    void main(void)
    {
        //
        // Initialize device, clocks, PIE
        //
        Device_init();
        Interrupt_initModule();
        Interrupt_initVectorTable();
    
        //
        // Initialize peripherals IN THIS ORDER:
        // 1. CMPSS first
        // 2. Input X-BAR second
        // 3. eCAP last
        //
        myCMPSS2_init();
        myINPUTXBAR_init();
        myECAP1_init();
    
        //
        // Register ISR and enable interrupt
        //
        Interrupt_register(INT_ECAP1, &ecap1ISR);
        Interrupt_enable(INT_ECAP1);
    
        //
        // Enable global interrupts
        //
        EINT;
        ERTM;
    
        while(1)
        {
            // gridFrequency_Hz is updated in ISR
            // Add your RMS calculation using ADC here
        }
    }


    Summary of Key Fixes

    Issue
    Wrong
    Correct
    eCAP input source
    ECAP_INPUT_OUTPUTXBAR3
    ECAP_INPUT_INPUTXBAR7
    X-BAR configuration
    Missing entirely
    XBAR_setInputPin(INPUTXBAR_BASE, XBAR_INPUT7, XBAR_IN_MUX_CMPSS2_CTRIPH)
    CMPSS DAC threshold
    Not configured
    Set to your DC bias value (e.g., 2047 for 1.65V)
    Capture mode
    4-event one-shot
    2-event continuous for period measurement
    Counter reset
    Reset on all 4 events
    Reset only on EVENT_1


    DAC Threshold Guidance

    If your grid signal at A4 is:
    ┌─ Biased at 1.65V (half of 3.3V VDDA) → DAC = 2047
    ├─ Biased at 1.5V → DAC = 1861
    └─ Formula: DAC = (V_bias / V_DDA) × 4095
    Verify XBAR_IN_MUX_CMPSS2_CTRIPH enum name in your C2000Ware version — it may appear as XBAR_IN_MUX_CMPSS2H or similar. Check xbar.h in your SDK installation.
    Best Regards,
    Zackary Fleenor
  • Hi Fleenor,

    I am not able to find XBAR_IN_MUX_CMPSS2_CTRIPH in my xbar.h.

    I sdk that i have have following options , please suggest which one i have to use 

    XBAR_EPWM_MUX02_CMPSS2_CTRIPH = 0x0400, //!EPWM MUX02 CMPSS2 CTRIPH

     XBAR_INPUT_FLG_CMPSS2_CTRIPH    = 0x0003,

    XBAR_CLB_MUX02_CMPSS2_CTRIPH = 0x0400, //!CLB MUX02 CMPSS2 CTRIPH

    Please suggest what is the hex value of this in your SDK. and which SDK you are using.

    Regards,

    Atul 

  • Hi Atul,

    You should use the INPUTXBAR selection here (XBAR_INPUT_FLG_CMPSS2_CTRIPH). Apologies for the previous incorrect nomenclature.

    For reference: 

    XBAR_EPWM_MUX02_CMPSS2_CTRIPH = PWMXBAR Selection

    XBAR_CLB_MUX02_CMPSS2_CTRIPH = CLB INPUTXBAR Selection

    Best Regards,

    Zackary Fleenor