Saturday, April 13, 2013

Welcome to my world of electronics and embedded systems

Frequency counter:

A simple frequency counter measures frequency by counting the number of edges of an input signal over a defined period of time (T).

A more complex method is  reciprocal counting.
Frequency is defined as (Number of events) / (time in seconds) and measured in Hz.
To make calculations trivial using a 1 second gate time (T) gives a direct reading of frequency from the edge counter.
frequency counter algorithm
Making a frequency counter for frequencies up to 65.536kHz is easy as the counters in a PIC chip can count up to 65535 without overflowing.
Up to 65.535kHz all you do is wait for 1 second while the count accumulates, read the value and display it. It will be the frequency in Hertz. Above 65.536kHz you have to monitor the overflow value while at the same time making an accurate delay time (T).
Note: Using a 1 second measurement period results in the frequency counter count value being a direct measurement of frequency requiring no further processing. It also means that the measurement is resolved to 1Hz. (Increasing T to 10s resolves to 0.1Hz while using T=0.1s gives a resolution of 10Hz).

Crystal oscillator

For the following projects the crystal oscillator (of the microcontroller) is used as the timebase. In these projects measurement of T (set at one second) is made by executing a delay that takes a set number of machine cycles.
Using a 4MHz oscillator gives a machine cycle of 1MHz (a period of 1us) which makes calculating and setting time delays fairly easy since most PIC instructions execute in one machine cycle. So executing 1,000,000 of these cycles gives a delay of 1 second.

Frequency counter accuracy

The accuracy of the frequency counter depends on the accuracy of the crystal driving the microcontroller.

ppm calculation

This is specified in ppm or parts per million. Its actually quite simple: taking an example of ±50ppm for a 4Mhz crystal. The error that the crystal could have (assuming that the crystal is loaded with the correct capacitance) will be in the range :
Maximum possible error 4MHz + (4MHz x 50 x 1e-6) = 4.0002e6
Maximum possible error 4MHz - (4MHz x 50 x 1e-6) = 39998e6
So the crystal could oscillate at any frequency between 4000200Hz and 3999800Hz. Note that this frequency can be changed by changing the loading capacitance on the crystal.
The delay time is the important measurement so for the above crystal at fosc/4) for the cycle time of the PIC chip (nominally 1MHz) we have:
Max cycle time : 1/(1.00005e6) or 1/(1MHz + 50ppm)
Min cycle time : 1/(0.99995e6) or 1/(1MHz - 50ppm)
Multiply by 1e6 to give a 1 second period gives the delay time
Min delay time : 1e6/(1.00005e6) 0.99995s or (1s - 1s x 50ppm) seconds.
Max delay time : 1e6/(9.9995e6) 1.00005s or (1s + 1s x 50ppm) seconds.
So you don't have to calculate all the intermediate steps just use the ppm value directly.
Note: If you have a reference oscillator that is more accurate than the crystal used in the frequency counter project then you can calibrate the project crystal. You can do this by adjusting a variable capacitor on one side of the crystal oscillator circuit while reading the output frequency displayed. If you don't have a reference then just use a fixed capacitor to give the correct parallel load capacitance for the crystal you use.

Common crystals

Commonly available crystals have a ppm specification of ±30ppm to ±50ppm (part per million error) but you can buy crystals with a ppm of ±20ppm. The smaller the ppm value (the smaller the error) the more accurately you can measure frequency.