This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Frequency Meter

Hi,
I want to develop a frequency meter with a resolution of .01 Hz. I am using 89c51 and a 12Mhz clocker. Kindly let me know how to do it. Thanks

  • Are you talking about monitoring the frequency of a digital clock signal? Off the top of my head, I'd probably start by reading up on the timer 2 modes in the datasheet.

    In capture mode, connect the signal you want to monitor to the T2 input, and it can capture the time when the external signal transitions. Two captures in a row, and you've got a clock cycle. The difference in the two capture times is the cycle time, which you can convert to a frequency.

    Timer 2 is most likely clocked at your main cpu frequency divided by 12, so the resolution should be well within your spec.

    You don't say what range of frequencies you're trying to measure. It will take you some number of clocks to respond to the timer interrupt, read the sampled values, calculate the differences, and so on. You won't be able to capture edges that arrive faster than you can execute this code. So I would only expect this method to be useful roughly up into the hundreds of kiloHertz at best, depending on how much of an 8051 coding wizard you are and how much other work you have to do. And it obviously fails once you reach the frequency at which T2 counts. It also will fail for very slow signals (< 15 Hz), where the 16 bit range of T2 isn't sufficient to count the length of time between clocks.

    Another way to do it would be to use T2 as a event counter, and use one of the other timers to generate interrupts at some sampling frequency, say 200 Hz. You can then look at the counter at each sample time and derive a frequency from the count.

    Or are you talking about building some sort of spectrum analyzer for an analog signal?

  • Hi Drew,
    Thanks for your response. I am looking at monitoring the line frequency, i.e. around 50 to 100 cycles. I want a resolution of .01 Hz. I used the counter method, where i count for a specified period of time and divide the counter value by the time. The max. resolution i can get is 1 Hz. I can get an resolution of .01 Hz, but i have to set the timer to 100 secs, which is too large. I have to monitor the frequency every 2 or 3 secs. Hope i have made myself clear. Thanks

  • 0.01 Hz is a period of 10ms which is a long time to a microcontroller. A simple solution might be to have an interrupt running periodically with a period of (say) 1ms and use a bit of software to increment a 32-bit counter.

    For continuous sampling, you will need to at least double-buffer the results. When the count is complete, you could trigger another interrupt internally or set a flag that will be checked on a round-robin basis.

    This will consume significant processor resources, but if you are careful this should not be a problem. The main advantage of this approach is its simplicity. You might also want to take the opportunity to do some filtering such as removing glitches - much easier this way.

    A 10ms interrupt period could still reliably give you the required resolution if you add some simple digital filtering such as taking the mean of 10 consecutive samples.

  • Trying to do this with a microcontroller may already be the wrong approach. Analog tools may well be cheaper --- I suspect some power plants out there still use mechanical tuning forks for this.

    That said, if you really want to do it using a micro, counting alone won't do it. You have to measure the duration of a certain number of cycles and do a division, instead. E.g. generate a zero-crossing signal, wire that to an interrupt input, and count for, say, 50 cycles. Have a second counter running free at a fixed frequency (CPU clock divided by something suitable, or an external reference oscillator). Start it on the first of those 50 cycles, and stop it after the last. Read it out, and you have the time taken for 50 cycles. Should be 1 second. The difference is your measurement.