Part Number: TMS320F2800157-Q1
Other Parts Discussed in Thread: C2000WARE, SYSCONFIG
I need to measure the duty cycle of an external PWM signal from an ABZ+PWM encoder using the F2800157. The system clock frequency is 120 MHz. I’m using GPIO22 as the input to the eCAP module and have confirmed that the external PWM signal is correctly reaching this pin.
I configured the eCAP module as shown below. However, the issue is that the ISR triggers only once, and the variable onTimeCount holds an unexpectedly large value during that single trigger. Could you please help me troubleshoot this issue?
void initECAP1(void)
{
// Configure GPIO5 as ECAP1 input
ECAP_selectECAPInput(ECAP1_BASE,ECAP_INPUT_GPIO22);
// Disable time-stamp counter
ECAP_disableTimeStampCapture(ECAP1_BASE);
// Configure eCAP for capture mode
ECAP_stopCounter(ECAP1_BASE);
ECAP_enableCaptureMode(ECAP1_BASE);
// Capture on rising edge first, then falling edge
ECAP_setEventPolarity(ECAP1_BASE, ECAP_EVENT_1, ECAP_EVNT_RISING_EDGE);
ECAP_setEventPolarity(ECAP1_BASE, ECAP_EVENT_2, ECAP_EVNT_FALLING_EDGE);
// Reset counter on event 2 (so we can measure high pulse width)
ECAP_enableCounterResetOnEvent(ECAP1_BASE, ECAP_EVENT_2);
// Divide by 1 (no prescaler)
ECAP_setEventPrescaler(ECAP1_BASE, 0);
// Stop after capture event 2
ECAP_setCaptureMode(ECAP1_BASE, ECAP_CONTINUOUS_CAPTURE_MODE, ECAP_EVENT_2);
// Enable sync-in if needed (optional)
ECAP_disableLoadCounter(ECAP1_BASE);
// Clear pending interrupts
ECAP_clearInterrupt(ECAP1_BASE, ECAP_ISR_SOURCE_ALL);
ECAP_clearGlobalInterrupt(ECAP1_BASE);
// Enable interrupt for event 2
ECAP_enableInterrupt(ECAP1_BASE, ECAP_ISR_SOURCE_CAPTURE_EVENT_2);
// Enable counter and timestamp capture
ECAP_enableTimeStampCapture(ECAP1_BASE);
ECAP_startCounter(ECAP1_BASE);
// Arm the first capture
ECAP_reArm(ECAP1_BASE);
}
// eCAP ISR
__interrupt void ecap1ISR(void)
{
uint32_t cap1, cap2;
// Clear interrupt flags
ECAP_clearInterrupt(ECAP1_BASE, ECAP_ISR_SOURCE_CAPTURE_EVENT_2);
ECAP_clearGlobalInterrupt(ECAP1_BASE);
// Read captured values
cap1 = ECAP_getEventTimeStamp(ECAP1_BASE, ECAP_EVENT_1);
cap2 = ECAP_getEventTimeStamp(ECAP1_BASE, ECAP_EVENT_2);
// Compute ON time (assuming Event1 = rising, Event2 = falling)
onTimeCount = cap2 - cap1;
captureDone = true;
// Re-arm for next capture
ECAP_reArm(ECAP1_BASE);
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP4);
}