Might be pushing it...
There are alternative techniques. One is a custom interrupt handler (to reduce the ISR overhead) Faster still - move the input to D5 and use it as input for TMR1... Both need some C.
Signal integrity needs to be good too - and not degrade due to capacitance etc
Martin
Freq meter err (FC11,Nano)
-
mnfisher
- Valued Contributor
- Posts: 1982
- http://meble-kuchenne.info.pl
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 158 times
- Been thanked: 940 times
-
mnfisher
- Valued Contributor
- Posts: 1982
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 158 times
- Been thanked: 940 times
Re: Freq meter err (FC11,Nano)
... and using TMR1 with an input as its pulse.
This requires D5 as the signal to be measured and D3 with a 1s pulse - I used a AWG for both signals
The theoretical maximum frequency it can measure is 6.4MHz - at 5MHz I got 4999986Hz
It is pretty much all in C - I used a custom interrupt for the timer1 overflow - to reduce the overhead (overflows is defined in supplementary code) - needed to create a dummy macro to keep FC happy (dummy - is never called)
The 1s (d3) interrupt uses the standard FC setup - but most of the code is in C (to retrieve the current value of TCNT1 and the number of overflows)
Now the pulse counter is hardware based - so the MCU isn't overloaded. This means we can use delays again.
An average value would be good - again I've kept the code as short as possible.
Martin
This requires D5 as the signal to be measured and D3 with a 1s pulse - I used a AWG for both signals
The theoretical maximum frequency it can measure is 6.4MHz - at 5MHz I got 4999986Hz
It is pretty much all in C - I used a custom interrupt for the timer1 overflow - to reduce the overhead (overflows is defined in supplementary code) - needed to create a dummy macro to keep FC happy (dummy - is never called)
The 1s (d3) interrupt uses the standard FC setup - but most of the code is in C (to retrieve the current value of TCNT1 and the number of overflows)
Now the pulse counter is hardware based - so the MCU isn't overloaded. This means we can use delays again.
An average value would be good - again I've kept the code as short as possible.
Martin
- Attachments
-
- freq_hs.fcfx
- (13.5 KiB) Downloaded 24 times
-
mnfisher
- Valued Contributor
- Posts: 1982
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 158 times
- Been thanked: 940 times
Re: Freq meter err (FC11,Nano)
Tested at 6.4MHz and pretty good (6399981Hz) and at 1MHz - 999997. A few 'ticks' are missed whilst handling the 1Hz / overflow interrupts?
Using an external 1Hz pulse means that the results are independent of the Arduino's clock - so should be reliable - depending on the accuracy of the timer instead.
I used a square wave for the 1Hz signal and initially a sine for the frequency to be measured (also tested with square for this)
Using an external 1Hz pulse means that the results are independent of the Arduino's clock - so should be reliable - depending on the accuracy of the timer instead.
I used a square wave for the 1Hz signal and initially a sine for the frequency to be measured (also tested with square for this)
-
viktor_aust
- Posts: 42
- Joined: Fri May 17, 2024 1:04 am
- Has thanked: 17 times
- Been thanked: 5 times
Re: Freq meter err (FC11,Nano)
Hi Martin
I like the idea of external 1Hz pulse.
Looks like this is the key to a good freq meter with Arduino.
Will test it with Mega shortly.
Thanks.
I like the idea of external 1Hz pulse.
Looks like this is the key to a good freq meter with Arduino.
Will test it with Mega shortly.
Thanks.
-
mnfisher
- Valued Contributor
- Posts: 1982
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 158 times
- Been thanked: 940 times
Re: Freq meter err (FC11,Nano)
Okay, let us know how you get on...
It looks like there will be a few changes - you'll need to change the pin from d5 to one that is a timer breakout (you might need to change to timer 5 and D47?) and d3 is int5 rather than int1
Will require some changes to the setup and interrupt handler for the 1hz pulse.
Martin
It looks like there will be a few changes - you'll need to change the pin from d5 to one that is a timer breakout (you might need to change to timer 5 and D47?) and d3 is int5 rather than int1
Will require some changes to the setup and interrupt handler for the 1hz pulse.
Martin
-
viktor_aust
- Posts: 42
- Joined: Fri May 17, 2024 1:04 am
- Has thanked: 17 times
- Been thanked: 5 times
-
chipfryer27
- Valued Contributor
- Posts: 1985
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 434 times
- Been thanked: 650 times
Re: Freq meter err (FC11,Nano)
Hi
In any counter application the problem is in the overheads, i.e. the time the code takes to actual process. Using an external timer as suggested above would free up the processor to focus on capture.
I've no access to hardware just now (still upside down) but I always planned to test the following.
External timer set to a suitable period in seconds. I would check against a scope / analyser and adjust until I got it as close as I possibly could to target time. If say it was one second then no averaging would be needed, but with five seconds then that would be my averaging value to divide by.
Microcontroller has interrupt already running on Freq-In pin set to trigger on rising edge.
Interrupt simply increments a counter when called (e.g. counter = counter +1)
Microcontroller sits waiting on the timer pulse to go High (for example).
Microcontroller clears counter
Microcontroller waits for timer pulse to go Low
Microcontroller assigns counter value (e.g. value = counter)
Divide value by how many seconds timer runs for to give value in Hz.
Back to beginning.
As I mentioned I'd always wanted to test the above to see what I got when connected to a known frequency source, perhaps checked against scope / analyser. May even be possible to slightly adjust parameters to factor in the time it takes to process at a known frequency.
Speed the microcontroller is running at would obviously affect accuracy.
About a year ago Martin and I attempted to measure how fast a uC could measure x-amount of ADC readings vs it using and external ADC. Quite fun.
Regards
In any counter application the problem is in the overheads, i.e. the time the code takes to actual process. Using an external timer as suggested above would free up the processor to focus on capture.
I've no access to hardware just now (still upside down) but I always planned to test the following.
External timer set to a suitable period in seconds. I would check against a scope / analyser and adjust until I got it as close as I possibly could to target time. If say it was one second then no averaging would be needed, but with five seconds then that would be my averaging value to divide by.
Microcontroller has interrupt already running on Freq-In pin set to trigger on rising edge.
Interrupt simply increments a counter when called (e.g. counter = counter +1)
Microcontroller sits waiting on the timer pulse to go High (for example).
Microcontroller clears counter
Microcontroller waits for timer pulse to go Low
Microcontroller assigns counter value (e.g. value = counter)
Divide value by how many seconds timer runs for to give value in Hz.
Back to beginning.
As I mentioned I'd always wanted to test the above to see what I got when connected to a known frequency source, perhaps checked against scope / analyser. May even be possible to slightly adjust parameters to factor in the time it takes to process at a known frequency.
Speed the microcontroller is running at would obviously affect accuracy.
About a year ago Martin and I attempted to measure how fast a uC could measure x-amount of ADC readings vs it using and external ADC. Quite fun.
Regards
-
mnfisher
- Valued Contributor
- Posts: 1982
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 158 times
- Been thanked: 940 times
Re: Freq meter err (FC11,Nano)
The version here has 'no' overheads - and can measure up to 6.4MHz(accurately - i think this is the max!) which is remarkable for a 16MHz processor!
The HS signal is acting as the pulse for TMR1 (decreases a 16 bit counter TCNT1) instead of the MCU clock (/some prescale)
This is done in hardware not code - the MCU runs normally and the only code is executed when timer1 overflows (every 65536 pulses) and once every second to calculate the frequency.
The Mega doesn't break out a pin for timer1 hence the need to change to timer5 - though i didn't look at the datasheet for long (original question was for a Nano) and I'd started wondering if i could use an ATTiny
and if i could measure PWM frequency and duty cycle...
Crikey - was it a year ago! It was fun! Time flies because of it....
Eek it is 2:30 am - really need to sleep before work
The HS signal is acting as the pulse for TMR1 (decreases a 16 bit counter TCNT1) instead of the MCU clock (/some prescale)
This is done in hardware not code - the MCU runs normally and the only code is executed when timer1 overflows (every 65536 pulses) and once every second to calculate the frequency.
The Mega doesn't break out a pin for timer1 hence the need to change to timer5 - though i didn't look at the datasheet for long (original question was for a Nano) and I'd started wondering if i could use an ATTiny
Crikey - was it a year ago! It was fun! Time flies because of it....
Eek it is 2:30 am - really need to sleep before work
-
chipfryer27
- Valued Contributor
- Posts: 1985
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 434 times
- Been thanked: 650 times
Re: Freq meter err (FC11,Nano)
Hi
I've no access to FC either at present so can't look at any charts (well not during the day)
Sleep..... Only in my dreams....
Typically waking around 2am and irrespective of when I go to sleep I'm still up at 2am.... That's about 4pm your time the day before. At best snatching a few hours here and there. However being an insomiac is nothing to lose sleep over as I will get plenty when I die <s>
I saw above it was HW orientated, just mentioning what I always wanted to try for the fun of it <s>
Interested to hear how Viktor gets on.
Regards
I've no access to FC either at present so can't look at any charts (well not during the day)
Sleep..... Only in my dreams....
Typically waking around 2am and irrespective of when I go to sleep I'm still up at 2am.... That's about 4pm your time the day before. At best snatching a few hours here and there. However being an insomiac is nothing to lose sleep over as I will get plenty when I die <s>
I saw above it was HW orientated, just mentioning what I always wanted to try for the fun of it <s>
Interested to hear how Viktor gets on.
Regards
-
mnfisher
- Valued Contributor
- Posts: 1982
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 158 times
- Been thanked: 940 times
Re: Freq meter err (FC11,Nano)
Wish lottery numbers would you like to know? Or if you're ahead? Euro millions would do nicely 


As the mega has multiple timers it could perhaps go back to generating the bogosecs (nearly 1Hz) interrupt too?
As the mega has multiple timers it could perhaps go back to generating the bogosecs (nearly 1Hz) interrupt too?