I am measuring a frequency of a square wave using the input capture (I'm using an ATMEGA128RFA1).

Basically for the first falling edge of the waveform I set the timer to 0, and then for the second I record the input capture value. Now in order to be able to use at low frequencies I need to count how many times I overflow. That way my actual period is:

overflowcount*65535 + "capture value"

right? For some reason though this yields half the frequency, so by dividing my overflow count by 2 I get the correct frequency in my program. Why is this? Does this mean my overflow interrupt is firing twice? This is the ISRs I am using:

ISR(TIMER1_CAPT_vect) { capture = ICR1; // Read input capture value if(trans == 0) // First falling edge { TCNT1 = 0; // Set the timer equal to 0 } else // Second falling edge { samples[reading] = capture; // Store the capture value samples[reading + 1] = overflowcount / 2; // Store the amount of times we have overflowed reading = reading + 2; // Increment index overflowcount = 0; // Reset overflow count } trans ^= 1; } // Is triggered on overflow ISR(TIMER1_OVF_vect) { overflowcount++; }

"samples" holds the capture value in the first position, and overflow count in the next, then in my program I calculate the period as I mentioned above, then the frequency from that. Why do I need to divide by 2?