Electronic – Frequency measurement using PIC

cpic

I am trying to write a program that measures the frequency of a signal. So I think a way to do it is to set the timer0 interrupt at every rising edge then record that value and subtract two successive values that would give the period. Can anyone tell me if this would work and help me write a code I am a beginner at PIC programming. I am using PIC16f17889

Best Answer

We're not going to spoon feed you code, because you won't learn anything that way. However, I hope we will be able to help you understand the underlying theories.

There's a number of different ways of measuring frequency. Briefly these are:

  • Period Measuring
  • Edge Counting
  • Gated Timing
  • FFT

Period measuring is basically what you are suggesting in your question. Measure the time between one leading (or falling) edge and the next. For this to be accurate you need to have a timer that is capable of running faster than the highest frequency you want to measure. Your system has to be able to react quick enough to time between the edges without missing an edge.

Edge counting is a simpler system to implement and can allow for measuring of higher frequencies than the period measuring, but only gives you an average frequency over a short period. This basically works by counting the number of incoming edges of the waveform over a pre-defined period. The simplest period to use is one second. Every time an edge is seen by the input pin you increment a counter. Every second you return that count and reset it to zero. It could be done by using an interrupt pin, or a timer with the clock source set to be an input pin. The latter is more accurate at higher frequencies if you have the ability on your chip.

Gated timing is probably the most fun, but requires specific facilities on your chip. Some timers are able to run in "gated" mode, whereby they count up at the system clock frequency (or some derivative of it) only when a certain input pin is in a HIGH state. Couple that with a second timer to measure the time between gate activations and you not only have the period of the wave but also the duty cycle.

FFT, or Fast Fourier Transform, is the most mathematically intensive. However it can give you frequency information about more complex waveforms. This is probably way beyond the scope of a PIC16 but worth mentioning anyway. With FFT you record a set of samples (analog) into memory then perform the FFT transformation. The result is a set of "buckets", each one representing the amplitude of a range of frequencies. Not really what you're looking for when you just want to measure the frequency of a square wave, but may be worth knowing it exists for a future project.

So which method should you use? Well, that is very subjective. It all depends on the frequency ranges you are looking to measure, the accuracy you want to achieve, whether you are interested in the duty cycle or not, and what facilities your chip provides. Now you know more about the options you can start looking in to which method is best for you.