PropBasic: Reciprocal Frequency Counter (0.5Hz to 40MHz) 40MHz now !!!
Bean
Posts: 8,129
Someone had asked about a frequency counter.
I wrote this one in PropBasic,itmeasures the frequency on P0 and sends ascii to a PC terminal program.
It is good up to 40 MHz (a prescaler could be used for higher frequencies).
It is a reciprocal counter, so the resolution is quite good even with a 1.0 sec gate time.
Let me know what you think.
[edit] Posted "Freq Counter Simple" it is a standard counter. Very easy to understand.
Bean.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/forums/default.aspx?f=25&p=1&m=412552
March 2010 Nuts and Volts articlehttp://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [RUSH - Freewill]
Post Edited (Bean) : 6/15/2010 12:08:14 PM GMT
I wrote this one in PropBasic,itmeasures the frequency on P0 and sends ascii to a PC terminal program.
It is good up to 40 MHz (a prescaler could be used for higher frequencies).
It is a reciprocal counter, so the resolution is quite good even with a 1.0 sec gate time.
Let me know what you think.
[edit] Posted "Freq Counter Simple" it is a standard counter. Very easy to understand.
Bean.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/forums/default.aspx?f=25&p=1&m=412552
March 2010 Nuts and Volts articlehttp://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [RUSH - Freewill]
Post Edited (Bean) : 6/15/2010 12:08:14 PM GMT
Comments
I like it .
I tested the program with a few different freq.s, It worked great. I checked the accuracy against my BK Precision Freq.counter, Dead on the money. I actually get a reading from the PropBasic program and the bst Term.window faster then the BK can track in on the freq. and display it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Truth is out there············___$WMc%___···························· BoogerWoods, FL. USA
You can feel stupid by asking a stupid question or You can be really·stupid by not asking at all.·
I don't understand when you "WRBYTE ascii(temp), digit"
you don't have to "RDBYTE ascii" to send it to the PC.
thanks again for posting it.
Bottom line, I work with a lot of audio stuff. All the readings from 440 hz up to 5kz are way off in the PBasic program. The closer I get to 1Mhz, the readings are closer. At 2mhz, they are almost spot on.
I'd like to see if anyone has taken measurements in the audio range, (say 100Hz to 20KHz). I don''t have any of my equipment to do this for the next month.
Again, this may be all associated with the PropScope not working properly. It's going into "Don't use untill fixed"drawer.
Srceen shots attached,
Jim
· Looking at your first screen shot the output is only 0.221 volts peak-to-peak. That is probably not enough to properly toggle the pin of the propeller. Idealy you want a zero to 3.3V waveform with a nice fast rise and fall time.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
Post Edited (Bean) : 6/11/2010 12:23:46 AM GMT
The amplitude of your input seems really low to me.I don't have a propscope,so I could be reading the attched image's wrong. In my first test I started @ 100kHz and went up to 2mHz.
I shot a 4volt peak DC square wave through a 220ohm resister to PIN 0 with a 10k pull down to clean off some of the A/C noise leaking from my older Function Generator.
My Function Generator gets a little squarely below 5kHz,But I can use a BS2 to simulate some low freq.s with about the same signal I had from the Function Generator. I' ll get back later with my results!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Truth is out there············___$WMc%___···························· BoogerWoods, FL. USA
You can feel stupid by asking a stupid question or You can be really·stupid by not asking at all.
want speed!__RIDE THE LIGHTIN__ with PropBaisc___www.propbasic.com__
Had to drop back to PropScope Ver 1.04 to get any amplitude out of the Freq Gen. Supposed to do 0-5V. All I could get out of it was 3V, (which is fine for this test). Attached is good working 440hz test.
Code looks good Bean!
Jim
I have been working through this code to get a better understanding of how it works. I think it's brilliant, But theirs one statement that has me puzzled.
" sigCnt = ___remainder "
I don't see "__remainder" declared as a VAR,CON,etc. and I didn't see it in the reserved words. Could you explain to me what that line of code does in your program?
Thanks in advance for an explanation.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Truth is out there············___$WMc%___···························· BoogerWoods, FL. USA
You can feel stupid by asking a stupid question or You can be really·stupid by not asking at all.
·
I'll have to get it added to the documentation.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
Here is some additional comments I added:
··· ' Since cntTime is in 80MHz units, the frequency is sigCnt / (cntTime / 80_000_000) = Hertz
··· ' Rearranged this can be written as (sigCnt * 80_000_000) / cntTime = Hertz
··· ' We want the result in milliHertz (1/1000 of a hertz) so we need to use:
··· ' (sigCnt * 80_000_000_000) / cntTime = milliHertz
··· ' Okay, now we have a problem, 80_000_000_000 cannot even be represented in 32 bits. Hmmm
··· ' We can calculate the answer one digit at a time.
··· ' The leftmost digit is MegaHertz and that digit can be calculated as:
··· ' (sigCnt * 80) / cntTime = MegaHertz
··· ' This we can do easily. Now the "trick" is how do we get the next digit ?
··· ' We take the remainder from the division, multiply it by 10 and divide again.
··· '· by repeating this process of taking the remainder and multipling by 10 we
··· '· can get as many digits as we need until we run out of precision.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
Thanks for the explanation.
I feel better now,I thought I was having another brain-fart. LOL!!!
I'll look out for the updated Doc's
Thanks again for the REPLY
Most Importantly, thanks for ProBasic!!!!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Truth is out there············___$WMc%___···························· BoogerWoods, FL. USA
You can feel stupid by asking a stupid question or You can be really·stupid by not asking at all.
·
I can't wait to see the new code!!!!
Brilliant
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Truth is out there············___$WMc%___···························· BoogerWoods, FL. USA
You can feel stupid by asking a stupid question or You can be really·stupid by not asking at all.
·
Chinese 2 line LCD displays would be a great beginners
Propeller project
Also notice that the gate time does not affect the calculations. Only the precision of the result.
You can just change the "LOOP UNTIL cntTime > 8_000_000 ' 0.1 Second gate time" line to use a different gate time.
What blows my mind is that with the Propeller you COULD measure 8 frequencies at the same time.
Pretty sweet for a $8 chip.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
Post Edited (Bean) : 6/12/2010 1:45:13 PM GMT
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
·Bean what is the osx file?
What osx file are you talking about ?
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
Quite amazing to watch the frequency of my analogue audio-generator drift around with the room temperature [noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I mean, if I went around sayin' I was an emperor just because some moistened bint had lobbed a scimitar at me they'd put me away!"
So now is good up to 40MHz.
This is turning out to be a pretty nice project.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you choose not to decide, you still have made a choice. [noparse][[/noparse]RUSH - Freewill]
Thanks Bean!
Jim
Nice. One question: Why limit the display to 1mHz ?
Taking the example of 440Hz, and a nominal 'gate' time of 1sec (actually the time for
the nearest whole Fu cycles, over a set point)
If it resolves to 40MHz/25ns, then we get
440*40e6/(40000000-2) ans = 440.0000220000011
440*40e6/(40000000-1) ans = 440.000011000000275
440*40e6/(40000000-0) ans = 440
440*40e6/(40000000+1) ans = 439.999989000000275
440*40e6/(40000000+2) ans = 439.9999780000011
so the LSB is 11uHz, so display to ~.01mHz is the resolution limit (at 440Hz)
Also, with reciprocal counters, if you take care to never drop an edge, you can
run a background resolution extension, eg take 10 readings over ~10 seconds,
and you improve the slower updated value to ~1uHz resolution. (9 digits)
So, I'd suggest a double display, - a faster refresh one (1-10 readings/sec), and then a slower one, ~ 10sec update, that gives highest precision for drift.
It might have been me that requested this. I just finished the Spin version with the help of JonnyMac and his object. Since all the hardware is still wired up on my bench it is the perfect time to try it out.
Counter (0.5Hz to 40MHz) 40MHz now !!! "; I want to using PIC18F452 & PIC18F8722 with crystal
of;XTAL=20MHZ to make a reciprocal Frequency Counter with C languageto measure input signal via a
function in anywhere of my main program when I call it.Would you please tell me how can I convert
your PropBasic: Reciprocal Frequency Counter (0.5Hz to 40MHz) to a C language for
PIC18F8722 &PIC18F452?
Thanks,
I don't think a direct conversion will work.
You'll have to adapt the concept to the hardware and instructions that the PIC offers.
Bean