Frequency counter.

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

mnf
Valued Contributor
Valued Contributor
Posts: 482
Joined: Wed May 31, 2017 11:57 am
Has thanked: 52 times
Been thanked: 267 times
Contact:

Frequency counter.

Postby mnf » Sat Feb 16, 2019 9:03 pm

Inspired by the topic 'How fast an interrupt can capture external pulses' (viewtopic.php?f=63&t=20962)- and having just built an xr2206 signal generator (a cheap Chinese kit)

So - an Arduino frequency counter. I hope to try this with an ARM chip (at 64MHz) - to see how well it copes - but in the first instance I got an Arduino (in my case a Nano clone) doing the honours. It manages to measure up to ~250kHz and you can see how the interrupts take their toll - the display is redrawn slowly (as multiple interrupts will occur).

The hardware is simple - I used a DS3231 RTC (to generate an accurate 1s interrupt)
An oLED i2c display SSD1306 (although I also tested outputting frequency to UART)
The signal - in this case is generated by an XR2206 (https://www.banggood.com/XR2206-Functio ... rehouse=CN) - which is a very cheap kit, although the xr2206 chips may be fake and lose focus at above 12v.
frequency.jpg
frequency.jpg (58.25 KiB) Viewed 762 times


I tested the resulting frequency count using a SmartScope (https://www.amazon.co.uk/LabNation-A17- ... B00NS2AB60)

The setup is straightforward - RTC and oLED are connected to power, gnd and i2c, no need for pullups (built in to RTC)
SQW from RTC connected to d2 (INT0) and output from signal generator connected to GND and D3 (INT1) I'd tweaked the output from the signal generator to be ~5v (using the amplitude adjustment) - worryingly it still seemed able to power the Arduino and display! (Still no harm done?)
The Arduino Micro was just on the board and doesn't do anything here. The program simply counts the interrupts received on INT1 per 1s (using the interrupt on INT0 from the RTC)
It displays the frequency if it has changed..

Note that I used the -o3 compiler optimisations to minimize the interrupt handler time. (viewtopic.php?f=76&t=20638&hilit=interrupt+o3&start=25) With a little tweaking the Arduino should be able to go faster still! I haven't tested with the standard optimisations to see how much it impairs performance.

frequency.fcfx
(14.23 KiB) Downloaded 47 times

Note that there are probably many enhancements possible - for example measuring the frequency for a fixed period (say 10s) and the disabling the interrupt whilst it is displayed - which would even out some of the fluctuations in frequency.. Simply disabling the interrupt during the display would stop the 'display crawl' at high frequencies.
Have fun..

Martin

As an aside - a slight bug in v8 - Setup interrupt, 'ok and edit macro' creates the macro but returns to editing the current macro rather than the interrupt routine.
The glcd print number is limited to (signed) 16 bit (rather than 32 bit). I sidestepped it here by converting the number to a string.
These users thanked the author mnf for the post (total 3):
jgu1 (Sun Feb 17, 2019 1:43 am) • Benj (Mon Feb 18, 2019 10:38 pm) • medelec35 (Mon Feb 18, 2019 11:00 pm)
Rating: 16.67%
 

jgu1
Posts: 1046
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 951 times
Been thanked: 242 times
Contact:

Re: Frequency counter.

Postby jgu1 » Sun Feb 17, 2019 1:45 am

Hi Martin!

Thank’s Martin. Look forward to test your project. I am on holyday at the moment. :D

Br Jorgen.
These users thanked the author jgu1 for the post:
mnf (Tue Feb 19, 2019 10:43 am)
Rating: 5.56%
 

User avatar
Benj
Matrix Staff
Posts: 14073
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4297 times
Been thanked: 4050 times
Contact:

Re: Frequency counter.

Postby Benj » Mon Feb 18, 2019 10:39 pm

I would be interested to see how a 16-bit PIC compares. i.e. one of the 140MHz EP devices.

I might give it a go on the ECIO40P16 if I get chance.
These users thanked the author Benj for the post:
mnf (Tue Feb 19, 2019 10:43 am)
Rating: 5.56%
 

mnf
Valued Contributor
Valued Contributor
Posts: 482
Joined: Wed May 31, 2017 11:57 am
Has thanked: 52 times
Been thanked: 267 times
Contact:

Re: Frequency counter.

Postby mnf » Tue Feb 19, 2019 10:46 am

Would be interested to hear how you get on...

Unfortunately my Windows computer has gone wrong (more phut than bang - just the windows whirly or starting automatic repair) - so haven't had much chance to work on things. Away for a few days (Stornoway :D ) - will fix (fingers crossed) on return.

Martin ( Linux won't let you down :) )