I'm trying to use the eCAP function to create a rectified sine wave that is synchronized to a square wave that is applied to the eCAP input. It works, however there is some unwanted jitter and I've found something that is strange to me. To understand the situation, here's a little background: I'm using the sine wave in the lookup table provided. When the eCAP interrupt occurs, the period of the square wave is calculated and the lookup table is zeroed. It seems to work, but when I trigger on the eCAP ISR, I notice that the ISR is executed twice and the sine wave lookup index is only zeroed on the second execution. I'm a hardware guy, so I set a bit when the ISR is entered, clear it after same executions, set it again after some executions, and clear it upon exit. I observe this bit on a scope.
To make the double execution more evident I added a dummy delay in the ISR of about 600uS (see ISR code below). When I enter the ISR, I set the trigger bit. My intention is to next zero the lookup table index, but it doesn't happen. The trigger bit stays set for 600uS, it is then cleared and held in that state for 600uS. The trigger bit is then set, the interrupts are re-enabled, the trigger bit is cleared, and the trigger bit is set again as the ISR is executed again. Why is it executed again?
Anyway, the lookup table index is zeroed the second time as evidenced by the yellow trace below. The slow decay of the yellow trace is due to an RC filter placed on the PWM representation of the rectified sine wave. After the 2 600uS dummy delays, we finally exit the eCAP1_isr and the lookup table generates the rectified sine wave until the next eCAP interrupt.
void Setup_eCAP1(void)
{
//---------------------------------------------------------------------
//--- Configure eCAP1 unit for capture
//---------------------------------------------------------------------
ECap1Regs.ECEINT.all = 0; // Disable all eCAP interrupts
ECap1Regs.ECCTL1.bit.CAPLDEN = 0; // Disabled loading of capture results
ECap1Regs.ECCTL2.bit.TSCTRSTOP = 0; // Stop the counter
ECap1Regs.TSCTR = 0; // Clear the counter
ECap1Regs.CTRPHS = 0; // Clear the counter phase register
ECap1Regs.ECCTL2.all = 0x0096; // ECAP control register 2
ECap1Regs.ECCTL1.all = 0x01C4; // ECAP control register 1
ECap1Regs.ECEINT.all = 0x0008; // Enable desired eCAP interrupts
}
interrupt void eCAP1_isr(void)
{
int i;
int k;
GpioDataRegs.GPBSET.bit.GPIO49 = 1; // Turn on LED4
linear_idx=0; // reset table index to syncronize to input
for (k = 0; k <10000; k++);
GpioDataRegs.GPBCLEAR.bit.GPIO49 = 1; // Turn off LED4
for (i = 0; i <10000; i++);
GpioDataRegs.GPBSET.bit.GPIO49 = 1; // Turn on LED4
ECap1Regs.ECCLR.bit.INT = 1; // Clear the ECAP1 interrupt flag
ECap1Regs.ECCLR.bit.CEVT3 = 1; // Clear the CEVT3 flag
// Calculate the input frequency period (rising edge to rising edge)
Freq_Period = (unsigned long)ECap1Regs.CAP3 - (unsigned long)ECap1Regs.CAP1;
//linear_idx=0; // reset table index to syncronize to input
PieCtrlRegs.PIEACK.all = PIEACK_GROUP4; // Must acknowledge the PIE group 4
GpioDataRegs.GPBCLEAR.bit.GPIO49 = 1; // Turn off LED4
}
The blue trace is the square wave that I'm trying to synchronize to. The green trace is the trigger bit. The second rising edge of the trigger bit is actually a rising edge after the second dummy delay, then a falling edge after the interrupts are re-enabled and then a rising edge as the interrupt is executed the second time. This just looks like one edge on this time base setting since all of these edges occur in a couple of hundred nanaseconds. The last narrow pulse of the trigger bit is the program exiting the ISR where it is free to generate the rectified sine wave.
I can set the qualification on the eCAP input for maximum filtering and it behaves identically to no filtering.
Please help.
