Hello Bob,
Some further comments assuming your use of input capture facility -
If you are able to vary the prescale setting (without affecting the operation of other sections of code), you can avoid multiple timer overflows, with resultant simplification of frequency calculation. For example, with a bus frequency of 4MHz and a prescale setting of 64, you can measure down to 1Hz without the input period exceeding the timer overflow period.
To check for multiple timer overflows (under-range), set a flag during timer overflow ISR, and clear the flag during input capture ISR. On entry to the timer overflow ISR, if the flag is already set, the frequency is too low, or there is no input signal.
Using this technique, you could start a measurement with maximum prescale value, and then progressively reduce the value until the period reading for a single input cycle was equal or greater than a value of $8000. This would achieve maximum resolution without the need to take overflow into account. If a prescale value of 1 was reached, and the period reading was still below $8000, you could then continue to totalise the period of multiple input cycles until the criterion was reached. For a frequency of 10kHz, this would occur after 82 cycles.
To calculate frequency to say 3 decimal places using integer arithmetic -
If single cycle measurement -
freq = fbus * 1000 / period / prescale;
If multiple cycle measurement -
freq = fbus * cycles * 1000 / period; // prescale = 1
Regards,
Mac