All Arduinos offer three or more timers which can be used to produce squarewave signals and also to count pulses. A very good frequency counter was published by Nick Gammon https://www.gammon.com.au/timers.
Nick was using the Input Capture function of pin "T1" of the ATmega328. Actually, the ATmega328 has two pins which offer that ability, but the problem is that the other Input Capture pin is "T0" which is connected to TIMER0, which does the system time-keeping. Unfortunately, there is no "T2"-pin. As I still wanted to use the second Input Capture, I had to disable the internal time-keeping and do it myself, using TIMER2, similar to Nick's sketch. As you might know, TIMER0 is an 8-bit timer, and TIMER1 is a 16-bit timer. When using both of them, I had to treat TIMER1 as an 8-bit timer. At first, I wanted to use the Overflow Interrupt, but for TIMER0, it is already defined by the Arduino system and cannot not get redefined. So I decided to use the COMPA interrupts of all the three timers. That makes it all a bit tricky, but it works quite well up to 4 MHz for both input signals.
Side note: The Renesas RA4M1 used for the UNO R4 also has pins with Input Capture function, but they are not accessible through pins 0 to 13. So there is no way to port my program to the new R4 platform. Frequency counting without the Input Capture function never can catch such high frequencies.
In order to test these frequency counters, I had to program another Arduino to generate two different frequencies. I could have installed a lot of switches to select a frequency but I used only one button per generator to cycle through a number of high frequencies from 8 MHz down to 470 kHz.
The program is not really perfect: when you apply the same signal to both counters you would expect identical results, but the outcome is some small differences between both values. Maybe somebody finds the reason and can solve it.


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)



_3u05Tpwasz.png?auto=compress%2Cformat&w=40&h=40&fit=fillmax&bg=fff&dpr=2)
Comments