chipKIT® Development Platform

Inspired by Arduino™

frequecy count

Created Thu, 11 Aug 2011 07:55:13 +0000 by Daven


Thu, 11 Aug 2011 07:55:13 +0000

Hi, I am a newbie. Just wandering can the chipkit read or measure frequency :?: . Dave


Thu, 11 Aug 2011 15:39:55 +0000

It depends on the range of frequencies that you are interested in measuring. If the frequency is too high, you won't be able to get a very accurate measurement.

The signal that you want to examine would have to be turned into a digital signal if it isn't already, so that it can be reliably read by an input pin.

One way to do it would be to set up one of the timers to count at a particular frequncy (such as 10Mhz), poll the input pin looking for a 0 to 1 transition. At that point, read the counter and stick the value in a variable. then start polling again for another rising edge. When you see the next rising edge read the timer again, and subtract the previous reading. This gives the period of the signal. The frequency is the reciprocal of the period.

This description is a little too simplistic, as you also have to taken into account the fact that the timer may have overflowed between the two readings, but that isn't hard to deal with. The other consideration is that for low enough frequencies, the range of the counter isn't long enough for the entire period. The PIC32 helps here as it allows two 16 bit timers to be combined into a 32 bit timer. This would allow measuring very long periods.

It is also possible to set up an input capture to do the waiting for a rising edge on the input signal so that the MCU doesn't have to poll the input pin. The input captures are tied to specific pins in the MCU. An input capture will sample the timer on the rising edge (or falling edge) automatically. The MCU could then be doing other things while waiting for the edge to be detected.

To get a more accurate measurement, you would then do this process several times and average the result.

Gene Apperson Digilent


Thu, 11 Aug 2011 19:57:53 +0000

Provided that the input signal is digital and of acceptable levels, can you use the input frequency as an external clock of one timer, which would be used as a counter, use another timer based on internal clock to measure a fixed period of time (e.g. 1s, or 1ms, depending, could be adaptive) and then divide the period by the value of the counter? I think it's more precise than trying to measure individual pulses.


Fri, 12 Aug 2011 01:28:38 +0000

You're right. That is how a frequency counter works. It would give a more accurate result than the method I suggested if you don't need the result quickly. I think that the crystal on the chipKIT boards is a 50ppm crystal, so it should be able to produce about that level of precision for frequencies much higher than my method would. I think that the other method would produce better results for very low frequencies.

Gene Apperson Digilent


Fri, 12 Aug 2011 04:54:36 +0000

Thanks Guys , but I was in the assumption that there was a frequency counter in the PIC 32 chip cause I notice this was available on maximaite board?


Fri, 12 Aug 2011 10:24:49 +0000

I think that the other method would produce better results for very low frequencies.

Agreed. So a good frequency counter should probably use both methods depending on frequency :)

@Daven: there is a counter. To make specifically a frequency counter, you need to make it count over a known period of time.


Tue, 16 Aug 2011 23:56:31 +0000


The PIC32 microcontrollers have five 16 bit counters (called Timers in the data sheet). Two pairs of them (2&3, 4&5) can be combined to produce 32 bit counters. The clock source for each of these counters can be set to come from one of two sources. 1)An internal frequency derived from the peripheral bus clock frequency (80Mhz normally on a chipKIT board) and a pre-scaler (a divider in between the pbclk and the counter), or 2) an external pin (a specific pin for each counter). These counters (Timers) are the same between the PIC32 devices on the Uno32 board and the Max32 board.

If you run your external frequency to the clock input pin of one of the counters, it will count cycles of the external signal. If you use another counter to generate a specific time interval (such as 1 second), then the cycle count from the first counter divided by the time interval will be the average frequency of the external signal over the time interval. The longer the time interval, the more precise the result, but the longer it takes to get the value.

Gene Apperson Digilent