Shop OBEX P1 Docs P2 Docs Learn Events
Propeller Frequency Counter? — Parallax Forums

Propeller Frequency Counter?

I was toying with the idea of doing a frequency counter project. I would probably use a prescaler, such as the LMX2322. There's numerous PIC-based projects that do this, the prescaler feeds a pin on the PIC that increments a counter, and then the PIC samples and resets that counter at a known interval. By knowing how many events were counted during the interval, you can compute the frequency in hertz. Ideally I'm shooting for something that would work from the low hertz range all the way up to a gigahertz or so.

I've read AN001, and it looks like there are some logic modes that can detect rising or falling edges on an input pin, and then do something with the FRQx and PHSx, but I'm not exactly sure how to put something practical together from this. I was hoping someone had a working example of a pulse-counting configuration.

Thanks,
Scott

Comments

  • jmgjmg Posts: 15,173
    edited 2015-12-06 09:29
    Take a look at a proper Reciprocal Counter - counts both Cycles and Time, and then over a nominal gate time
    calculates Freq = Cycles/Time

    http://forums.parallax.com/discussion/123170/propbasic-reciprocal-frequency-counter-0-5hz-to-40mhz-40mhz-now

    LMX2322 looks to be obsolete ?

    Not sure what a modern replacement is.
    Si5351A looks to have a bypass mode and divider, but may not get > 200MHz ?
    Divider choices are
    R0_DIV[2:0] R0 Output Divider.
    000b: Divide by 1
    001b: Divide by 2
    010b: Divide by 4
    011b: Divide by 8
    100b: Divide by 16
    101b: Divide by 32
    110b: Divide by 64
    111b: Divide by 128
  • There's plenty of LMX2322 on ebay, so I have a few on order. Though you're right it would be nice to find a modern replacement, so that a few years down the line if someone is reading my blog on the project, they don't have trouble sourcing the chip.

    Thanks for the pointer to the reciprocal counter, I'll take a look. I've been mostly away from the prop for a couple years, so prop basic will be entirely new to me!

    Scott
  • This one works pretty good up to 29Khz. But it does not read decimal like 1400.73 Hz. etc. You can go straight into the pin you declare in this case pin 5. But keep it under 3.3 volts. Or use a Schmitt trigger like a HEF40106B. For higher Hz. use a pre-scaler,Hz. divider and then do the math. The Arduino has one that works really well with all the decimal.



    CON

    _clkmode = xtal1 + pll16x ' Crystal and PLL settings.
    _xinfreq = 5_000_000 ' 5 MHz crystal.

    Pin = 5 ' Counter Input Pin



    VAR
    long frequency



    repeat


    'Pulses are sampled on this pin.
    DirA[Pin] := In
    CTRA := 0 'Clear CTRA settings
    CTRA := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (Pin) 'Trigger to count rising

    FRQA := 1000 'Count 1000 pulses on each trigger
    PHSA := 0 'Clear accumulated value
    WaitCNT( 80_000_000 + CNT ) 'Wait for 1 second
    Frequency := PHSA / 1000 'Calculate Freq based on the sampling
    ' duration, 1000 ms (1 second)

    LCD.DecF(frequency,5) ' This just shows I'm displaying on the LCD you have to add the LCD OBJ.

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-12-07 02:39
    Without a prescaler, the Prop's counters should be able to count edges at nearly 40 MHz. Just set up the counter to increment on rising edges, set frqx to 1, initialize phsx to 0, and count during an interval of 1 second. Then divide phsx by 80 to get MHz, 80_000 to get kHz (80 MHz system clock).

    -Phil
  • For a more up to date Hz. divider use a ADF4007 from analog devices for that high Hz. stuff. Some of those older IC's like the SP8647 is to hard to get. But it still works great in my pre-historic 225 Mhz. Heath counter.
  • Thanks, I'll take a look at the ADF4007 as well.

    In the meantime, I also want to figure out how to get an accurate clock reference for the prop. Let's say I want to hook up a 3.3V TCXO oscillator. I happen to have a 25Mhz TCXO on hand. Can I just connect the TCXO to the XIN pin, set the PLL to x2, and operate a 50Mhz? Alternatively, I could try to get a 10Mhz or 20Mhz TCXO and operate at the full 80Mhz.

    Thanks,
    Scott
  • jmgjmg Posts: 15,173
    edited 2015-12-08 08:11
    smbaker wrote: »
    In the meantime, I also want to figure out how to get an accurate clock reference for the prop. Let's say I want to hook up a 3.3V TCXO oscillator. I happen to have a 25Mhz TCXO on hand. Can I just connect the TCXO to the XIN pin, set the PLL to x2, and operate a 50Mhz? Alternatively, I could try to get a 10Mhz or 20Mhz TCXO and operate at the full 80Mhz.
    The Prop PLL has limited control, and IIRC the VCO always runs at 16x Xtal In, The Post-VCO divider you can change, but the VCO range imposes limits on the Crystal.

    Data sheet spec's 4MHz <= Xtal <= 8MHz

    If you have a 25MHz TCXO, then a 'simple' /5 would be needed for Prop-drive. (ie add HC161 et al)

    Maybe? the Prop can work with a 52MHz TCXO in Xtal x 1 mode ? (data mentions to 60MHz) - not quite 80MHz but closer than 25MHz :)

  • smbaker wrote:
    I happen to have a 25Mhz TCXO on hand. Can I just connect the TCXO to the XIN pin, set the PLL to x2, and operate a 50Mhz?
    No. The input to the PLL is spec'd to be between 4 and 8 MHz. However, you should be able to drive it with a 10 MHz TCXO reliably.

    -Phil
  • kwinnkwinn Posts: 8,697
    jmg wrote: »
    smbaker wrote: »
    In the meantime, I also want to figure out how to get an accurate clock reference for the prop. Let's say I want to hook up a 3.3V TCXO oscillator. I happen to have a 25Mhz TCXO on hand. Can I just connect the TCXO to the XIN pin, set the PLL to x2, and operate a 50Mhz? Alternatively, I could try to get a 10Mhz or 20Mhz TCXO and operate at the full 80Mhz.
    The Prop PLL has limited control, and IIRC the VCO always runs at 16x Xtal In, The Post-VCO divider you can change, but the VCO range imposes limits on the Crystal.

    Data sheet spec's 4MHz <= Xtal <= 8MHz

    If you have a 25MHz TCXO, then a 'simple' /5 would be needed for Prop-drive. (ie add HC161 et al)

    Maybe? the Prop can work with a 52MHz TCXO in Xtal x 1 mode ? (data mentions to 60MHz) - not quite 80MHz but closer than 25MHz :)

    A /4 is a bit simpler, and provides a 6.25MHz clock so you can run the prop at 100MHz.
  • Have you considered using a PECL ripple counter as a pre-scaler or pre-counter? For example, the http://www.onsemi.com/PowerSolutions/product.do?id=MC100E137 counts at up to 2GHz and supports asynchronous gating. It'd need a high speed comparator (or fancy bias network) for a front end. Also, it would need to be gated if used as a pre-counter so the bits have time to settle down before they're read in.

    Another option would be to use a small CPLD to make a Johnson-counter. The sparse grey-code output of the Johnson-counter would avoid the need to gate the pre-counter. Speed wise, 400-500MHz is a realistic max count rate for a FPGA/CPLD. (IO pin speed is the limit)

    Marty
  • jmgjmg Posts: 15,173
    Lawson wrote: »
    It'd need a high speed comparator (or fancy bias network) for a front end.
    That's one of the problems...
    The Frequency Synthesizer / PLL parts tend to be
    * low cost
    * have amplifiers inbuilt
    * have a choice of divider, ideally from /1

    - but they also suffer from lifetime issues, as the markets move and vendors follow.

    Also depends on just how many MHz is considered important.
    Many of the Frequency Synthesizer / PLL parts have more than one input choice - up to 3 : Xtal, IF, RF
    Hittite seem to be one of the few that include DC as a lower limit.
  • jmgjmg Posts: 15,173
    edited 2015-12-14 00:12
    Some more info on the post above

    I tested a Si5351A on a Adafruit PCB https://www.adafruit.com/product/2045
    jmg wrote: »
    Si5351A looks to have a bypass mode and divider, but may not get > 200MHz ?
    Divider choices are
    R0_DIV[2:0] R0 Output Divider.
    000b: Divide by 1
    001b: Divide by 2
    010b: Divide by 4
    011b: Divide by 8
    100b: Divide by 16
    101b: Divide by 32
    110b: Divide by 64
    111b: Divide by 128

    Bypass mode ( Xtal /2^N) works well, and will divide to above 160MHz (my test limit)

    However, there is a caveat lurking here :
    The XA input will not divide reliably below about 3MHz, and if you GND XA (biasing the Osc Amplifier hard one way) then the /2^N pins output indicate around roughly 60MHz of parasitic internal oscillation.
    I tried a square wave drive to XA, with the same results.
    ( ie Behaviour is like there is some AC coupling internally.)

    That means Si5351A is good for direct Divide of Xtals > 5MHz, (eg it could do 25MHz -> 6.25MHz discussed here ) and it can also PLL to any frequency 2.5 kHz to 200 MHz (which is useful as a Signal Generator) and the XA can be AC coupled to a clipped Sine source, so there are many uses in a Frequency Counter design within the timebase.

    Whilst it is low cost, and a second one could be used for a Fi prescaler > 5MHz, the way it 'generates it own frequency' outside the range, makes it rather less practical as a general use divider.


  • smbakersmbaker Posts: 164
    edited 2015-12-22 07:03
    Just wanted to mention that this project is coming along wonderfully.

    The first thing I did was to use a variation of DigitalBob's code and see how well I could make the prop count. With a one second gate time, I'm easily able to count to 25Mhz. There's a little bit of error, probably due to the time to execute a few of the statements that is falling into the measurement loop (I'm off by 700 Hz on a 25 Mhz signal). I figure I can probably correct for that. I'm able to reliably measure square waves up to the limit of my function generator, which is 25 Mhz.

    Sine waves are a different beast entirely -- I think I need some signal conditioning. I'm probably going to go with a 74HC4046 PLL chip and use part of it for signal conditioning. This seems to be what a lot of low end frequency counter projects do.

    Then I tried out the LMX2322 chip. I set it to divide by 128 for now. It's easily able to function to the limit of my function generator (again, 25 MHz). It bottoms out around 5 Mhz.

    I figure my project is going to have two input paths -- one for 1 Hz - 25 MHz using no prescaler, and a second path from 5 Mhz to 2.4 Ghz using the prescaler. I do have some 5 Mhz TCXO on order, and I'll eventually use those to yield a precision timebase.

    I need to get my hands on a higher range frequency generator so that I can do some actual tests.

    Thanks for all the help
    Scott
  • jmgjmg Posts: 15,173
    smbaker wrote: »
    The first thing I did was to use a variation of DigitalBob's code and see how well I could make the prop count. With a one second gate time, I'm easily able to count to 25Mhz. There's a little bit of error, probably due to the time to execute a few of the statements that is falling into the measurement loop (I'm off by 700 Hz on a 25 Mhz signal).
    That's quite a large error.
    Did you look at Bean's counter in the link above ?



    smbaker wrote: »
    Sine waves are a different beast entirely -- I think I need some signal conditioning. I'm probably going to go with a 74HC4046 PLL chip and use part of it for signal conditioning. This seems to be what a lot of low end frequency counter projects do.

    Then I tried out the LMX2322 chip. I set it to divide by 128 for now. It's easily able to function to the limit of my function generator (again, 25 MHz). It bottoms out around 5 Mhz.

    I figure my project is going to have two input paths -- one for 1 Hz - 25 MHz using no prescaler, and a second path from 5 Mhz to 2.4 Ghz using the prescaler.

    I presume that 5MHz bottom, is for the 2.4GHz in ? which is not too bad.

    Those parts can also count from the XTAL in, which has a sine amplifier portion. - ie you may not need a 4046 - just use the Xtal pathway you already have.

  • jmgjmg Posts: 15,173
    edited 2015-12-22 09:14
    oops
  • jmg wrote: »
    smbaker wrote: »
    The first thing I did was to use a variation of DigitalBob's code and see how well I could make the prop count. With a one second gate time, I'm easily able to count to 25Mhz. There's a little bit of error, probably due to the time to execute a few of the statements that is falling into the measurement loop (I'm off by 700 Hz on a 25 Mhz signal).
    That's quite a large error.
    Is it? That's only 28ppm, fully within the spec of a regular XO. It's actually astonishing that the error is not larger, given that the frequency was attempted to be measured using Spin. Perhaps the 25MHz signal happens to deviate in the same direction as the Prop's clock source?
  • Scott,
    Can you provide details on the TCXO you ordered? Source, part number, price, etc.? I'm trying to make a simple high precision stopwatch and have had trouble finding low-error 5MHz crystals.
    smbaker wrote: »
    I do have some 5 Mhz TCXO on order, and I'll eventually use those to yield a precision timebase.
    Also, are these drop-in replacements for the standard 5MHz crystal, or are circuit changes required? If so, can you help me with the schematic?

    Thanks,
    Jim

  • jmgjmg Posts: 15,173
    Scott,
    Can you provide details on the TCXO you ordered? Source, part number, price, etc.? I'm trying to make a simple high precision stopwatch and have had trouble finding low-error 5MHz crystals.
    You do not have to always use 5MHz -> 80MHz
    for many apps, something like a direct operation from

    ASVTX-11-121-19.200MHZ-T

    will be ok. ( $1.39 1+, 2ppm 1.8mA VCTCXO)

    That gives appx 50ns Stop watch LSB - pretty good for a stopwatch :)

    That one you can trim using a GPS clock.

  • mbvmbv Posts: 7
    edited 2018-10-17 18:21
    -
Sign In or Register to comment.