Hi,
I'm trying to use ecap1 to measure the width of a low pulse.
I'm trying to setup the first event to be the falling edge and the second event to be the rising edge. I then want to interrupt on the second event and measure the difference between event 2 and event 1. I want the capture to happen once and then I want to rearm it after I've made my measurement. Here's how I'm configuring the capture:
void InitECapture()
{
ECap1Regs.ECEINT.all = 0x0000; // Disable all capture interrupts
ECap1Regs.ECCLR.all = 0xFFFF; // Clear all CAP interrupt flags
ECap1Regs.ECCTL1.bit.CAPLDEN = 0; // Disable CAP1-CAP4 register loads
ECap1Regs.ECCTL2.bit.TSCTRSTOP = 0; // Make sure the counter is stopped
ECap1Regs.ECCTL1.bit.PRESCALE = 0;
// Configure peripheral registers
ECap1Regs.ECCTL2.bit.CONT_ONESHT = 1; // One-shot
ECap1Regs.ECCTL2.bit.STOP_WRAP = 1; // Stop at 2 events
ECap1Regs.ECCTL1.bit.CAP1POL = 1; // Falling edge
ECap1Regs.ECCTL1.bit.CAP2POL = 0; // Rising edge
ECap1Regs.ECCTL1.bit.CTRRST1 = 1; // Difference operation
ECap1Regs.ECCTL1.bit.CTRRST2 = 1; // Difference operation
ECap1Regs.ECCTL2.bit.SYNCI_EN = 1; // Enable sync in
ECap1Regs.ECCTL2.bit.SYNCO_SEL = 0; // Pass through
ECap1Regs.ECCTL1.bit.CAPLDEN = 1; // Enable capture units
ECap1Regs.ECCTL2.bit.TSCTRSTOP = 1; // Start Counter
ECap1Regs.ECCTL2.bit.REARM = 1; // arm one-shot
ECap1Regs.ECCTL1.bit.CAPLDEN = 1; // Enable CAP1-CAP4 register loads
// ECap1Regs.ECEINT.bit.CEVT4 = 1; // 4 events = interrupt
ECap1Regs.ECEINT.bit.CEVT2 = 1; //enable cap2 (rising) as interrupt source
}
Here's what my interrupt does:
interrupt void ecap1_isr(void)
{
ProxObj[ProxCh].regReading(ECap1Regs.CAP2 - ECap1Regs.CAP1); //puts a reading in the buffer
temp=ECap1Regs.CAP1 - ECap1Regs.CAP2; //test variable
//clear event 1 and 2 interrupt flags
ECap1Regs.ECCLR.bit.CEVT2 = 1;
ECap1Regs.ECCLR.bit.CEVT1 = 1;
ECap1Regs.ECCLR.bit.INT = 1;
ECap1Regs.ECCTL2.bit.REARM = 1;
// Acknowledge this interrupt to receive more interrupts from group 4
PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;
}
My interrupt fires correctly but when I look at the capture registers, (CAP1 and CAP2), CAP1 is greater than CAP2. Since CAP1 is the falling edge of the low pulse I'm trying to measure and CAP2 is the rising edge, things are reversed.
I've waisted an entire day playing with the capture setup and I cannot get it to work the way I want. Any thoughts are greatly appreciated. Thanks.