This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Having a problem with ecap

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.

 

 

  • Your setup uses the reset command both for event 1 and 2 : 

    ECap1Regs.ECCTL1.bit.CTRRST1 = 1;          // Difference operation         

    ECap1Regs.ECCTL1.bit.CTRRST2 = 1;          // Difference operation      

    In this setup CAP1 will capture a first value, which is the time between the start of the program and the first falling edge. Then it will reset reset the timer. Next, CAP2 will capture the time between 0 and the event of the ridsing edge.

    I would recommend (a) to use the reset only after the 2nd event - you have to subtract CAP2 - CAP1 to get the pulse width  or(b) to reset with event1 - in this case CAP2 should give you the pulse width directly.

    Hope this helps.

     

  • That seems to have done the trick.  Thanks Frank.