Quote Originally Posted by HenrikOlsson View Post
Hi Rufinus,
Sadly Darrel has passed away and is no longer with us.

I'll try to answer your qeustion.
It's correct that with a prescaler of 1:256 the timer will only overflow every 65536 "tick" but that doesn't prevent Darrels code from measuring frequencies lower than 65536Hz. As I wrote in a previous post the two most significant bytes of the 4 byte result will contains the number of overflows (ie "units" of 65536Hz), the next byte is the TMR0 value which is in units of 256Hz and the least significant byte is what's stored in the prescaler, in units of 1Hz. All provided your "gate time" is precisely 1Hz of course.

So, if your input frequency is 125Hz then after the one second gate time there will be 0 overflows, 0 counts in TMR0 and 125 counts in the prescaler. The prescaler isn't readable so the code then "manually" pulses the counter input to find out how many pulses is needed before the prescaler overflows and TMR0 increases. In this example it would take 131 (125+131=256) which, when stored in a byte and negated "becomes" the value 125.

Changing the prescaler won't help you measure the low frequencies. The only way to gain resolution at the lower end is to increase the gate time, count for 2, 5, 10 or whatever seconds instead of 1 and adjust the calculations accordingly.

/Henrik.
Ok, now i think i got it. It was the last step (remainder in prescaler), which I could not understand.
But it would mean then, that the smallest frequency, which I could measure in one second is 256 Hz.
For lower frequencies i would have to increase sampling time.

Lets say, it is very rare, that one needs to measure frequency below 20 Hz in normal application.
That would mean, that in 1 second sampling time there will be 20 counts in prescaler, which in turn means we would have to provide 256-20=236 counts more to overflow it. So, 236 remaining counts divided by 20 gives us 11.8 seconds! We will need to increase sampling time to 11.8 seconds, in order to measure 20 Hz frequency, or ?!

I think I will have to think about some other algorithm to measure frequencies below 256 Hz, maybe with COUNT command or so. Precision might suffer, but that would be a reasonable compromise. Target would be to keep the one second sampling rate over whole range.

Thanks again Henrik, for explaining.