# Frequency Counter Using a PIC16F648A

I wonder if someone could help me with some general suggestions on a frequency counter project.

I'm trying to modify a 6 digit LED frequency counter that uses an integral PIC16F648A, a 13 Mhz oscillator and an I2C coupled digital display. This hardwired design uses TMR0. I've been able to get it reprogrammed by using modified software that was written for 6 discrete LED's running at either 4 or 20 Mhz. I've tweaked the code here and there but I can't get the kind of consistency I need - 100 hz, or less. The apparent problem is the 13 MHz oscillator.

I've googled the web and found that 13 Mhz is a very odd crystal frequency for a counter. Any thoughts as to how it may have been used in this design??

I'd like it to work up to 60 Mhz as this is the upper limit of the unit when it was equipped with its original code (don't have a copy). Auto ranging is not required.

Any and all suggestions would be most appreciated.

This is a pretty difficult accuracy/precision to hit.

First of all, according to the datasheet, you can't have a period on that pin of less than 20 ns, so that is a max frequency of 50 MHz. This is from Table 17-8 of the datasheet. So, 60 MHz is out.

But more importantly, you are trying to get a timing accuracy of 100 Hz / 60 MHz, this is 1.7 ppm accuracy. Or if you just want consistency, it's 1.7 ppm frequency stability. Most crystals are at least +/- 20 ppm stability. The smallest I could find immediately was a +/- 9. So that is a tight timing control.

However, let's say you could get a crystal and 50 MHz is good enough. Here is a quick clock path drawing based on the datasheet Figure 6-1 that shows how to configure the timer

Read 6.2.1 to see the limits that you must adhere to. In summary, it just says you need to have on the block that says "SYNC 2 cycles" a high time of at least 2*Tosc + 20ns, and a low time of the same. For a 13 MHz clock, this gives a max frequency of about 350 kHz. Note that this is after the prescaler, so the real limit is when you use the max prescaler setting: 256 * 350 kHz = 89 MHz. This is easily over the 50 MHz limit by the pin so you are good.

The required accuracy (within 100 Hz at 50 MHz) suggests being able to differentiate period differences of $$\frac{1}{50MHz} - \frac{1}{50MHz + 100 Hz} = 40 femtoseconds!$$

The way to be able to get this small of a differentiation is to measure how long a large number of counts takes. This is perhaps overdoing it, but if this is all the processor is doing, I would want something like 10 us to make sure I had plenty of time to sample the data. This allows some error for the time the code takes. You could do it in less, but you would have to be more careful with your timing (count instruction cycles, etc). If you did about 0.5 billion counts, then a 10 us delay would only give a 50 Hz error. This means you would have to sample for nominally 10 seconds. The tighter your code timing, the more easily you can reduce the sample time.

I'd just swap the crystal. 20 MHz in HC49 can can be salvaged from any old Ethernet board. That's the fastest you can get to anyway and you can use the code you have for this frequency.