Author Topic: Frequency counter Project  (Read 25550 times)

0 Members and 1 Guest are viewing this topic.

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #25 on: March 31, 2016, 12:08:30 pm »
Today, it’s no problem to realize a frequency counter with internal 200 MHz reference. You don’t need any FPGA design, just a simple STM32F407

You can only make a primitive direct counting frequency counter. STM32 timers do not allow to implement the reciprocal frequency counter (at least, without external logic or other tricks). About interpolator can not even speak.
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 9754
  • Country: de
Re: Frequency counter Project
« Reply #26 on: March 31, 2016, 03:31:22 pm »
Modern µCs can very well use the timers to make a reciprocal counter. However that might be a limitation for the maximum input frequency, that may not be high enough for a universal counter. So an external prescaler may very well be needed.

Some chips (AFAIK some PIC) may even include a circuitry usable for an analog interpolator.
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #27 on: March 31, 2016, 05:26:12 pm »
Modern µCs can very well use the timers to make a reciprocal counter.

So it might seem at first glance. But if you try to do this, things are not so good. Modern µCs timers are not adapted to the implementation of the reciprocal counter.

Some chips (AFAIK some PIC) may even include a circuitry usable for an analog interpolator.

Yes, some PICs have Charge Time Measurement Unit (CTMU). But I doubt very much that it can help in the implementation of the interpolator. Frequency counter - a digital device that has two clock domains: input frequency domain and reference frequency domain. All external signals in µCs pass through synchronizers and associated with internal clock domain. Therefore, a good frequency counter uses CPLD or FPGA.
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8222
  • Country: 00
Re: Frequency counter Project
« Reply #28 on: March 31, 2016, 05:30:54 pm »
"All external signals in µCs pass through synchronizers and associated with internal clock domain."

Some pics have asynchronous counters. Some avrs too actually.
================================
https://dannyelectronics.wordpress.com/
 

Offline xyrtek

  • Regular Contributor
  • *
  • Posts: 65
  • Country: us
Re: Frequency counter Project
« Reply #29 on: March 31, 2016, 06:06:04 pm »
In case this can be useful for someone else that arrives at this thread, may I suggest:

http://www.foxdelta.com/products/fc3.htm

In fact look around, there are several versions of not only FC but some other useful kits.

They  sell the PCBs for most recent projects at very, very low cost. The owner replies to emails and is a great person!!!

EDIT:
answer framed for original question.

Quote
I would like to ask you about simple cheap microcontroller frequency counter circuit, I found some projects but I am looking for one can's measure from Hz up to 200Mhz or more 7-segment LED display maybe with prescaler.
« Last Edit: March 31, 2016, 07:06:25 pm by xyrtek »
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 9754
  • Country: de
Re: Frequency counter Project
« Reply #30 on: March 31, 2016, 06:49:39 pm »
The PIC based counter from
http://www.foxdelta.com/products/fc3.htm
seems to be based on direct counting. So not a reciprocal counter.

For building a reciprocal counter most (if not all) of the circuit is in the clock domain of the reference clock. The only part that needs a different clock domain (or just use a ripple counter) is a possible prescaling divider. Some extra start stop modes might also want extra logic - but this is usually not a must have for a frequency counter.  So with the µC running from the reference clock it is no problem using the µC internal timers for an reciprocal counter.
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #31 on: March 31, 2016, 07:13:24 pm »
So with the µC running from the reference clock it is no problem using the µC internal timers for an reciprocal counter.

It is not true. Give then at least a brief description of the structure of the reciprocal frequency counter. Of course, I do not mean the option, when in the process of measuring the software processing is involved. The frequency counter should run up to 1/2 of the CPU clock.

With the software processing is simple. For example, the input signal is fed to the capture input of timer, which counts the number of reference clock periods, and the number of periods of the input frequency is counting in the capture interrupt. But then the maximum input frequency is 20 - 50 times lower than the clock frequency. On the AVR this way I was getting the measurement frequency up to 500 kHz. It seemed that the more complex STM32 timers allow to abandon software processing, but it is not. On one of the forums was a variant of the reciprocal frequency counter based on the STM32, but it can not be called elegant.
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 72
  • Country: de
Re: Frequency counter Project
« Reply #32 on: May 13, 2016, 04:01:31 pm »
Quote
You can only make a primitive direct counting frequency counter. STM32 timers do not allow to implement the reciprocal frequency counter (at least, without external logic or other tricks). About interpolator can not even speak.

Reading this I'm a little bit astonished. In between maybe you found out, that this is not true.
With STM32 devices running at 200 MHz you can built 'true' reciprocal counters with 5 ns resolution, which will give you 8 digits resolution @ 2 measurements/s.

Thinking about a separate external interpolator to get 1 ns resolution seems not very attractive to me, because the improvement will only be factor 5. Factor 50 or 100 would be much better, but I don't know if you can handle 0.1 ns delays with discrete components. At last you need a very stable reference signal AND a stable input signal as well.
For common applications (RC- or XTAL oscillators) a counter with 5-7 digits will be sufficient.
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 9754
  • Country: de
Re: Frequency counter Project
« Reply #33 on: May 13, 2016, 05:01:34 pm »
The interpolator is not only useful for stable signals where high resolution is wanted. It's more needed for measuring short times (e.g µs rang) . However this usually will need two interpolating units: one for start and one for stop.

If it is just for high resolution on stable signals, one could use timestamping, to measure the time for more than just the first and last transition. This allows higher resolution and suppression of trigger noise - however sensitivity to true signal frequency noise can be larger. The hardware is the same as for normal reciprocal counting, just get the time for more transitions.
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #34 on: May 13, 2016, 05:41:01 pm »
Factor 50 or 100 would be much better, but I don't know if you can handle 0.1 ns delays with discrete components.

What does the delay? Interpolator based on capacitor charge during the measured time with a further voltage measurement using the built-in STM32 ADC will provide sub-ns resolution.

With STM32 devices running at 200 MHz you can built 'true' reciprocal counters with 5 ns resolution

Never seen implementation. You will need one timer clock the reference frequency, the second - the input frequency. In this case both timers need to enable / disable strictly at the same time, ie, in a single machine cycle. In addition, in synchronism with the transition of the input signal. STM32 timers logic does not allow it.
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 9754
  • Country: de
Re: Frequency counter Project
« Reply #35 on: May 13, 2016, 06:23:16 pm »
Counting the input signal can be done in software. Or maybe with one of the internal counter triggering on a capture event (not sure the STM32 can do that). This will limit the input frequency a little, to something like 1 or 5 MHz - but a similar (if not lower)  limit would apply to an analog interpolator as well. A prescaler can easy take care of this. So two ranges are needed: one for the high frequencies that could work down to something like 10 kHz and one for the lower frequencies that can work up to about 5 MHz depending on the implementation.
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #36 on: May 13, 2016, 07:18:35 pm »
Yes, software counting is possible. But when they say that in the STM32 realized "true reciprocal frequency counter", you want to expect the maximum frequency of up to 1/2Fclk.
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 72
  • Country: de
Re: Frequency counter Project
« Reply #37 on: May 14, 2016, 08:46:12 am »

Never seen implementation. You will need one timer clock the reference frequency, the second - the input frequency. In this case both timers need to enable / disable strictly at the same time, ie, in a single machine cycle. In addition, in synchronism with the transition of the input signal. STM32 timers logic does not allow it.
Here you can find a picture of a STM32F429-discovery based counter, reduced to 7 digits because of simple 8 MHz XTAL clock reference: https://www.mikrocontroller.net/topic/384435#4388056
Reference clock is corrected (with green trimmer) by 77.90 ppm to show exact results. STM32F429 is running at 180 MHz so input frequency range is from 0.05 Hz to about 90 MHz @ 3 readings/s.
Input signal is supplied by orange cable and the internal prescaler (input = PB4) is connected by grey cable from PC8 (presc. output) to PE6.
The little red LED shows prescaler is activated.

Here you can see another reciprocal counter based on STM32F407-discovery with 4,3" TFT (direct-drive): https://www.mikrocontroller.net/topic/275225#2891811

Reciprocal counters based on STM32 work fine and you can realise several channels with only one µC. Believe it or not - I don't mind :-)
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8222
  • Country: 00
Re: Frequency counter Project
« Reply #38 on: May 14, 2016, 10:41:42 am »
Quote
With STM32 devices running at 200 MHz you can built 'true' reciprocal counters with 5 ns resolution

Nothing magical about reciprocal counting vs. direct counting - both need two "clocks" (one input / signal + one reference). The only difference between the two is who's used to count whom.

As to STM32, the use of pll clock there can be problematic from a phase noise perspective -> less of an issue if you continue the counting.
================================
https://dannyelectronics.wordpress.com/
 

Offline danadak

  • Super Contributor
  • ***
  • Posts: 1875
  • Country: us
  • Reactor Operator SSN-583, Retired EE
Re: Frequency counter Project
« Reply #39 on: May 14, 2016, 11:30:12 am »
Some useful reference material.


https://www.dropbox.com/s/dmnyb69ntfgd3o3/Counters.zip?dl=0


Regards, Dana.
Love Cypress PSOC, ATTiny, Bit Slice, OpAmps, Oscilloscopes, and Analog Gurus like Pease, Miller, Widlar, Dobkin, obsessed with being an engineer
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #40 on: May 14, 2016, 01:57:00 pm »
Reciprocal counters based on STM32 work fine and you can realise several channels with only one µC. Believe it or not - I don't mind :-)

In the picture we can not say how well implemented frequency counter. Please specify how peripheral initialization and measurement procedure. One way to implement the reciprocal counter I found on the net, but it looked not very elegant:

1. TIM2 init to PWM mode/One-pulse mode, external clock - Gate and count N_input
2. Load to TIM2->ARR max. value
3. Init TIM3 and TIM15 (chain connected) as Slave timer in Gated mode (triggered by TIM2) - count N_ref
4. Start TIM2
5. Delay 0.5 s (for 1 s gate time)
6. Read TIM2->CNT, mul by 2, load to TIM2->ARR (on the fly)
7. Wait for TIM2 stops count
8. F_in = F_clk * TIM2->CNT (N_input) / TIM3, TIM15->CNT (N_ref)
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 72
  • Country: de
Re: Frequency counter Project
« Reply #41 on: May 15, 2016, 10:18:33 am »
In the picture we can not say how well implemented frequency counter. Please specify how peripheral initialization and measurement procedure.

Following the 2. link, you will see the source-code of 'f_mess.c'. This is the very first implementation of reciprocal counter on STM32F407 and shows the basic operation only. https://www.mikrocontroller.net/attachment/158664/f_mess.c

16 bit counter TIM9 is used as reference counter running at 168 MHz. Once initialised TIM9 is never stopped again!
Each event on PE5 (TIM9-capture input) increments a 32 bit software-counter and gives an exact time-stamp with 5,95 ns (1/168 MHz) resolution. Overuns of TIM9 are counted as well and are always taken into account, resulting in 32 bit time-stamps synchronously to the input event.

LOOP:
Starting measurement synchronously with the first input event, after some time (arround 0.5 s for 8 digits) you can take the last time-stamp (end_zeit -> end_time), substract the first time-stamp (start_zeit -> start_time) and you’ll get the actual time (mess_zeit -> act_time):
act_time = end_time – start_time;

Doing the same thing with counted input pulses you will get the number of events (mess_ereignisse -> act_count) as the difference of last count (end_ereignis -> end_count) and first count (start_ereignis -> start_count):
act_count = end_count – start_count;

To prepare next calculation simply update start_time and start_count:
start_time = end_time;
start_count = end_count;

Finally we want to see the result of frequency:
frequency =  act_count * 168e6 / act_time;

Convert and display frequency and
goto LOOP:
Looking at the photo of TFT-display act_count is displayed as 'Nin' and act_time as 'Nref'.

During measurement there is no stop (no missing input event). That is why you will get one reading/s @ 1 Hz input signal. For faster results with less digits needed, you can reduce measurement time to 1 ms and will get 1000 readings/s @ 5 digits resolution.
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #42 on: May 15, 2016, 10:49:38 am »
Each event on PE5 (TIM9-capture input) increments a 32 bit software-counter and gives an exact time-stamp with 5,95 ns (1/168 MHz) resolution.

This is not true hardware reciprocal frequency counter. Input pulse count is software, so their frequency must be much lower Fclk/2, as should have time to execute the interrupt handler. I mean a true hardware timers-based frequency counter that do not require real-time software processing. In the post above, I gave an example of such a solution.
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 72
  • Country: de
Re: Frequency counter Project
« Reply #43 on: May 15, 2016, 12:43:40 pm »
As discribed before a reciprocal counter is classified by this calculation: f = Nin * F_CLOCK / Nref.
No matter which kind of counters are used.

If you like it, you can even measure your heartbeat in reciprocal way:
starting a timer at first beat, counting beats in mind and stop the timer after some seconds synchronously with the last beat.
Even mental calculation is possible and you will get an accurate true reciprocal result (3 digits in less then 10 s ;-)

This is not true hardware reciprocal frequency counter. Input pulse count is software, so their frequency must be much lower Fclk/2, as should have time to execute the interrupt handler. I mean a true hardware timers-based frequency counter that do not require real-time software processing.

No!
A prescaler will handle all frequencies as desired without any loss of resolution or accuracy.
But as I see, it makes no sence to show further code with full implementation of automatic prescaler selection.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8222
  • Country: 00
Re: Frequency counter Project
« Reply #44 on: May 15, 2016, 12:49:18 pm »
"before a reciprocal counter i"

As you are counting continuously (by time stamping the incoming pulse train), it doesn't matter if you are counting reciprocally or directly.

Continuous counting is more advanced than reciprocal counting (better suited for low frequency) or direct counting (better suited for high frequency).
================================
https://dannyelectronics.wordpress.com/
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #45 on: May 15, 2016, 01:42:03 pm »
No!
A prescaler will handle all frequencies as desired without any loss of resolution or accuracy.
But as I see, it makes no sence to show further code with full implementation of automatic prescaler selection.

I understand that the use of prescaler can solve the problem of the frequency range without loss of resolution. But this solution has a number of disadvantages - the delay to determine the prescaler ratio, the limitation on the intensity of the other interrupts in the system, etc. Using software counting of input pulses reciprocal counter simply realized on AVR or STM. But it is interesting to build a true reciprocal frequency counter, which would use only the timers without real-time software processing.
PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 72
  • Country: de
Re: Frequency counter Project
« Reply #46 on: December 01, 2016, 01:24:44 pm »
But it is interesting to build a true reciprocal frequency counter, which would use only the timers without real-time software processing.

Using STM32F4xx you can take two timers: one for counting ext. pulses and the other one for counting internal Fref.
Adding a dual D-type flip-flop will synchronize ext. pulses and Fref on request and will generate a common capture signal to both timers (timestamp). TIM2 and TIM5 are 32-bit counters so you don't need any interaction of software (or any prescaler) for frequencies from 0,05 Hz to ~42-90 MHz. (42 MHz ..F407 @168 MHz; 90 MHz ..F427 @180 MHz)

Actually I'm trying to add a discrete time-to-digital converter (TDC) to the circuit described above to get a finer resolution <= 100 ps and to get results of 10 digits/s. Maybe this will work as expected - maybe STM32F4xx will be to noisy.
 

Offline Liv

  • Regular Contributor
  • *
  • Posts: 134
  • Country: by
Re: Frequency counter Project
« Reply #47 on: December 01, 2016, 02:26:19 pm »
Adding a dual D-type flip-flop will synchronize ext. pulses and Fref

By adding external logic, the problem is solved. But an elegant solution - it is no external logic. Input frequency is fed to pin of the controller - and all.

Actually I'm trying to add a discrete time-to-digital converter (TDC) to the circuit described above to get a finer resolution <= 100 ps and to get results of 10 digits/s. Maybe this will work as expected - maybe STM32F4xx will be to noisy.

I tried to do TDC as an addition to the AVR. But I did not need a resolution better than 1 ns. The noise level was below 1 ns.

PSU PSL-3604 Pulse gen. PG-872 Freq. cnt. FC-510
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 72
  • Country: de
Re: Frequency counter Project
« Reply #48 on: December 01, 2016, 03:53:34 pm »

But an elegant solution - it is no external logic. Input frequency is fed to pin of the controller - and all.

I understand. You like to snap your fingers and like a rainbow you can see the result by looking in the sky?
An elegant solution indeed.
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 72
  • Country: de
Re: Frequency counter Project
« Reply #49 on: December 31, 2017, 05:30:27 pm »
Actually I'm trying to add a discrete time-to-digital converter (TDC) to the circuit described above to get a finer resolution <= 100 ps and to get results of 10 digits/s. Maybe this will work as expected - maybe STM32F4xx will be to noisy.

As announced one year ago, I finished the development of a 2 channel reciprocal counter with 10 digits/s on main channel. The second channel offers 8 digits/s. It can be used separately or to stabilize the internal or external 10 MHz reference clock by 1pps GPS-signal down to 0.1 ppb.
Both channels work from 1 mHz up to 80 MHz directly. An external prescaler (i.e. MC12080)  can extend the range up to 1.5 GHz.
For faster tests channel 1 makes up to 1000 meas./s with 7 digits resolution continously or single shot with external trigger.
Take a look at the circuit diagram.

Further pictures (german information) can be found here: http://mino-elektronik.de/FM_407/fmeter_407.htm#c3
« Last Edit: December 31, 2017, 05:40:18 pm by mino-fm »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf