I'm trying to use the input capture module on a PIC32MX250F128B to measure the frequency of a square wave. The results I'm getting are both inconsistent and incorrect. All I'm trying to do here is measure the frequency of the input square wave by reading the capture value at every falling edge interrupt. Then I reset Timer2 and read the capture value again at the next interrupt.
Here's my ISR:
void __ISR(_INPUT_CAPTURE_4_VECTOR, ipl1) Capture4(void) {
mPORTBToggleBits(BIT_10);
capture_value = IC4BUF;
mIC4ClearIntFlag();
TMR2 = 0;
}
Here's my module configurations:
IC4Rbits.IC4R = 0; // Assign peripheral pin RPA0 to IC4
mPORTASetPinsDigitalIn(BIT_0);
IC4CONbits.C32 = 0; // 16-bit source timer
IC4CONbits.ICTMR = 1; // Use Timer2
IC4CONbits.ICI = 0; // Interrupt every capture event
IC4CONbits.ICM = 2; // Simple Capture Event mode - every falling edge
IFS0bits.IC4IF = 0; // Clear interrupt flag
IFS0bits.IC4IE = 1 // Enable IC interrupt
IFS0bits..IC4IP = 1; // Set interrupt priority to 1
IC4CONbits.ON = 1; // Enable IC module
T2CONbits.ON = 0; // Disable while config
TCONbits.SIDLE = 0; // Continue in idle mode
TCONbits.TCS = 0; // Use internal clock
TCONbits.TCKPS = 0; // Set 1:1 prescale
TCONbits.TGATE = 0; // Disable gated time accumulation
PR2 = 0xFFFF; // set period register
TMR2 = 0; // start counting at zero
T2CONbits.ON = 1; // enable module
You'll notice the first line of the ISR toggles a digital output pin; I added this so I could monitor the activity to see if the interrupt was ever called. The interrupt does get called and I've viewed it on an oscilloscope. I would expect the ISR to be called consistently in line with the falling edges of the input square wave but this is not the case. Instead, the ISR is called inconsistently at seemingly random intervals. Also, the values being read from IC4BUF don't correlate at all with any adjustments to the input signal's frequency.
Clearly I'm missing something here. If you have any suggestions or can point out any obvious errors that I've made, that would be greatly appreciated. Also, I'm open to alternative methods for accomplishing the task of measuring the input frequency. I chose this route with the Input Capture because it seemed to make the most sense. Thanks!