Accurate Frequency measurement

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DzNg_1446756
Level 3
Level 3
First like received First like given

I'm trying to read a sensor that outputs frequency (pulses).  I use the method described in this document:

http://www.cypress.com/file/144696/download

It can read the output frequency from the sensor, but it seems to be about 10% less than what I'm observing from my scope.  Is there any way to improve the accuracy from this method?

0 Likes
1 Solution
WaMa_286156
Level 5
Level 5
First comment on blog 100 replies posted 50 replies posted

If your pulse width is proportional to the frequency, you can use something similar to this:

PulseWidth.png

The timer starts on rising edge and captures on falling edge.   the Capture interrupt has the results.  You have a 1 microsecond accuracy.

If you need to capture from rising pulse to rising pulse, then put a flip flop in there that sends Q NOT into D, and the rising edge will send Q high.  Send that into counter enable.  The next rising edge will send Q low, disabling the counter.  You can  capture the count using Q Not into Capture (capture on rising edge),  interrupt on capture, then read the capture register.  I found out through experimentation that capture works on a counter with enable low, which is why Q NOT can give you the counter capture.

Be sure and put this in, then build, then put any USB onto the schematics after a successful build and run, else you can end up with routing issues, with sporadic capture interrupts. (Based on experience with with 6 or more identical counters in the schematic page with USB placed on the page first.)

View solution in original post

0 Likes
2 Replies
Anonymous
Not applicable

If you program a microcontroller to count the number of pulses in a second, and it's wrong.  It's either counting the number of pulses incorrectly, or it isn't accurately estimating the passing of a second.  I'm willing to bet it's the later. 

To measure time accurately, your microcontroller's clock source either needs to be accurate (crystal source) or needs to be compensated (how many clock cycles are actually in a second VS rated approximation of microcontroler's internal clock).

0 Likes
WaMa_286156
Level 5
Level 5
First comment on blog 100 replies posted 50 replies posted

If your pulse width is proportional to the frequency, you can use something similar to this:

PulseWidth.png

The timer starts on rising edge and captures on falling edge.   the Capture interrupt has the results.  You have a 1 microsecond accuracy.

If you need to capture from rising pulse to rising pulse, then put a flip flop in there that sends Q NOT into D, and the rising edge will send Q high.  Send that into counter enable.  The next rising edge will send Q low, disabling the counter.  You can  capture the count using Q Not into Capture (capture on rising edge),  interrupt on capture, then read the capture register.  I found out through experimentation that capture works on a counter with enable low, which is why Q NOT can give you the counter capture.

Be sure and put this in, then build, then put any USB onto the schematics after a successful build and run, else you can end up with routing issues, with sporadic capture interrupts. (Based on experience with with 6 or more identical counters in the schematic page with USB placed on the page first.)

0 Likes