PropBasic: Reciprocal Frequency Counter (0.5Hz to 40MHz) 40MHz now !!!

BeanBean Posts: 7,997
edited 2011-04-16 - 07:35:51 in Propeller 1
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

Comments

  • $WMc%$WMc% Posts: 1,884
    edited 2010-06-10 - 22:02:59
    Bean

    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.·
    __Walt McDonald__

    The Truth is out there
    It's not rocket-surgery
    I see why we don't have any water,All of the pipes are full of wires!
    E=WMc2
    Now with WiFi
    Not in the Spin Bunch
    import RPi.GPIO as GPIO

    ABB M202 certified
    ABB M211 certified
  • camelot2camelot2 Posts: 54
    edited 2010-06-10 - 22:10:19
    hi Bean, thanks for the code. It works great.
    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.
  • hover1hover1 Posts: 1,927
    edited 2010-06-10 - 23:32:23
    Very un-scientific test because it was based on the PropScop Signal Generator. I can't rely on the product, although it says it's putting out the correct frequency and the scope input seems to correspond. But I can not trust this device as it took me 1/2 hour just to get it connected again and I needed to restart it multiple times just to get valid readings for this test.

    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
    1440 x 900 - 706K
    1440 x 900 - 713K
    1440 x 900 - 734K
    1440 x 900 - 732K
    I have three propellers
  • BeanBean Posts: 7,997
    edited 2010-06-11 - 00:17:13
    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
  • kuronekokuroneko Posts: 3,623
    edited 2010-06-11 - 00:22:00
    hover1 said...
    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.
    FWIW, I faked a 440Hz wave on P0 (NCO, $00005C46, @80MHz) and that's what I'm told by the program (439.994Hz in fact). So as Bean suggested it might be the signal level.
  • $WMc%$WMc% Posts: 1,884
    edited 2010-06-11 - 00:34:12
    hover1

    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__
    __Walt McDonald__

    The Truth is out there
    It's not rocket-surgery
    I see why we don't have any water,All of the pipes are full of wires!
    E=WMc2
    Now with WiFi
    Not in the Spin Bunch
    import RPi.GPIO as GPIO

    ABB M202 certified
    ABB M211 certified
  • hover1hover1 Posts: 1,927
    edited 2010-06-11 - 03:06:23
    The test started out at a higher voltage. I didn't noticed that they dropped during the tests.

    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
    1391 x 747 - 657K
    I have three propellers
  • $WMc%$WMc% Posts: 1,884
    edited 2010-06-12 - 01:15:43
    Bean

    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.

    ·
    __Walt McDonald__

    The Truth is out there
    It's not rocket-surgery
    I see why we don't have any water,All of the pipes are full of wires!
    E=WMc2
    Now with WiFi
    Not in the Spin Bunch
    import RPi.GPIO as GPIO

    ABB M202 certified
    ABB M211 certified
  • BeanBean Posts: 7,997
    edited 2010-06-12 - 02:50:55
    __remainder is the remainder of the last division operation. But it MUST be assigned right after the division because it could be used by another command.

    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]
  • BeanBean Posts: 7,997
    edited 2010-06-12 - 03:14:37
    I'm working on a tidy'er version of the program.

    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]
  • $WMc%$WMc% Posts: 1,884
    edited 2010-06-12 - 03:46:10
    Bean

    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.

    ·
    __Walt McDonald__

    The Truth is out there
    It's not rocket-surgery
    I see why we don't have any water,All of the pipes are full of wires!
    E=WMc2
    Now with WiFi
    Not in the Spin Bunch
    import RPi.GPIO as GPIO

    ABB M202 certified
    ABB M211 certified
  • $WMc%$WMc% Posts: 1,884
    edited 2010-06-12 - 04:09:25
    Bean

    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.

    ·
    __Walt McDonald__

    The Truth is out there
    It's not rocket-surgery
    I see why we don't have any water,All of the pipes are full of wires!
    E=WMc2
    Now with WiFi
    Not in the Spin Bunch
    import RPi.GPIO as GPIO

    ABB M202 certified
    ABB M211 certified
  • HollyMinkowskiHollyMinkowski Posts: 1,398
    edited 2010-06-12 - 06:24:26
    This program paired with one of those cheap 3.50
    Chinese 2 line LCD displays would be a great beginners
    Propeller project smile.gif
    [The Bug Stops Here]
  • BeanBean Posts: 7,997
    edited 2010-06-12 - 13:40:16
    You will notice that the posted code starts at the 10Mhz digit so the initial multiplication is * 8, but this digit will alway be zero since the maximum frequency is 2.5MHz.

    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
  • BeanBean Posts: 7,997
    edited 2010-06-12 - 19:50:42
    I have posted the updated code in the first post of this thread as "Freq Counter2".

    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]
  • mikedivmikediv Posts: 825
    edited 2010-06-13 - 00:05:05
    Thanks Bean your awesome
    ·Bean what is the osx file?
  • BeanBean Posts: 7,997
    edited 2010-06-13 - 01:03:07
    Mike,
    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]
  • BradCBradC Posts: 2,601
    edited 2010-06-14 - 01:45:35
    Very cool Bean. I'm seeing some issues on slow signals with slow rise times, but I suspect putting a schmitt trigger on the front end to clean up the input will make that go away.

    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!"
    wtf?
  • BeanBean Posts: 7,997
    edited 2010-06-14 - 18:40:40
    I have changed the code to use a hardware counter for the counting.
    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]
  • hover1hover1 Posts: 1,927
    edited 2010-06-14 - 18:43:43
    40 MHz..Sweet!

    Thanks Bean!

    Jim
    I have three propellers
  • jmgjmg Posts: 13,777
    edited 2010-08-13 - 23:50:00
    Bean wrote: »
    I have changed the code to use a hardware counter for the counting.
    So now is good up to 40MHz.
    This is turning out to be a pretty nice project.

    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.
  • yarisboyyarisboy Posts: 245
    edited 2010-08-14 - 10:42:45
    Thanks Bean,
    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.
  • bobxbobx Posts: 1
    edited 2011-04-16 - 06:29:26
    Hello,Mr.Bean.With thanks for your very nice and great program;"PropBasic: Reciprocal Frequency

    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,
  • BeanBean Posts: 7,997
    edited 2011-04-16 - 07:35:51
    The PropBasic code relies heavily on the Propeller chips counters and WAITPxx instructions.
    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
Sign In or Register to comment.