Shop OBEX P1 Docs P2 Docs Learn Events
Why doesn't frequency measurement work this work? — Parallax Forums

Why doesn't frequency measurement work this work?

RichardFRichardF Posts: 168
edited 2007-06-25 22:21 in Propeller 1
Why do I get 13,225 for freq, instead of 1000? Clock is running at 80 mhz.
Pins 21 and 11 are tied together externally.

PUB start
cognew(freqGen, @stack1)
cognew(getFreq, @stack2)
repeat
· updateLcd(freq, 12,0)

PUB freqGen
dira[noparse][[/noparse]21]~~
outa[noparse][[/noparse]21]~
repeat···································· ··· ·'generate 1 khz out
· !outa[noparse][[/noparse]21]
·· waitcnt(clkfreq/2000 + cnt)
···
PUB getFreq
freq·:= 0
t0 := cnt
repeat while cnt < t0+ 80_000_000··· 'measure for 1 sec
· if ina[noparse][[/noparse]11] == 1
··· freq++

Pain-in-the-butt,
Richard

Post Edited (RichardF) : 6/25/2007 6:02:15 PM GMT

Comments

  • rokickirokicki Posts: 1,000
    edited 2007-06-25 17:58
    Your counter is doing it wrong; you're counting the number of times you "see" a one, not the
    number of times you "see" a zero->1 transition. Something like this might be better:

    freq := 0
    t0 := cnt
    pinmask := 1 << 11
    repeat while cnt < t0 + clkfreq
       waitpeq pinmask,pinmask,0
       waitpeq 0,pinmask,0
       freq++
    
    



    Note this is extemporaneous and untested.

    Anyway . . . keep those questions coming!
  • deSilvadeSilva Posts: 2,967
    edited 2007-06-25 18:14
    You are counting for around half a second (if you are lucky and there is no overflow of CNT smile.gif ).

    A loop with an IF an an IN in SPIN could easily take 20 mys.

    Your fine benchmark shows that this loop in fact takes 0.5/13,654 s = around 40 mys.


    This shows that it is quite possible to handle a 1 kHz signal by a more correct program.
  • rokickirokicki Posts: 1,000
    edited 2007-06-25 18:24
    Oops, forget about the overflow of count; deSilva is correct that that will cause problems (about one time in 50).

    Note that the counter can directly count pin transitions for you, so it would be relatively trivial to set up a counter
    to do the counting (at which point you could count any frequency up to 40MHz, pretty much, assuming the duty
    cycle is high enough).
  • deSilvadeSilva Posts: 2,967
    edited 2007-06-25 18:33
    The syntax of WAITPEQ and WAITPNE ist State, Mask, Port - but I am sure RichardF is able to sort that out smile.gif


    In fact there are always four possibilities:
    (1) Simple polling as in Richard's first try, which seems to be possible from SPIN upto 10 kHz; a minor disadvantage being that the COG will unnecessarily use power
    (2) Using WAITPEQ/NE which will improve on the power need in case of lower frequencies but go only marginally beyond 10 kHz
    (3) Using assembler with waitpeq/ne, which will catapult your program into the 1 MHz range
    (4) Use hardware (cf. the application note AN001 from Parallax www.parallax.com/dl/appnt/prop/AN001-PropellerCountersv1.1.zip) to reach 40 Mhz, as rokicki pointed out.

    Post Edited (deSilva) : 6/25/2007 6:41:18 PM GMT
  • RichardFRichardF Posts: 168
    edited 2007-06-25 21:46
    Gentleman,
    Please take a look in the "stickies" at "Spin Code Examples For The Beginner" example 14. I find his example that only works to about 10 khz as you suggest, yet the author shows a picture of it working to 200 khz plus. I wonder how he did that. I have tried different pin polling methods and, as noted, the language is just too slow to handle the higher frequencies.

    Problem!!!!!! I just received 8 new TSL230 Light to Frequency converters which output a 1 to 1 mhz signal linearly with incoming light energy. I want to use these at full scale. They do feature, though, pin selectable 1,2, 10 and 100 divide-by scaling. What I am learning here is if I am going to use Spin I guess I must use the divide-by 100 scaling to get the full range of response. Obviously this will greatly decrease my ability (by a factor of 100) to detect very small variations in light energy. I hate to not be able to fully utilize the chips capability for resolution. Would appreciate your thoughts on this. If you want to look at the data sheet for the TSL230, go to mouser.com and enter a search for TSL230.

    Maybe our Parallax friends can chime in here.

    As always, thanks,
    Richard
  • rokickirokicki Posts: 1,000
    edited 2007-06-25 22:21
    If I were doing this, I would either:

    1. Use a single cog and tight assembly to read (probably 8) of these. This is an interesting challenge;
    small and simple enough, yet interesting enough; people should see what they can do.

    2. Use counters, and do two sensors per cog, thus using four cogs, completely in Spin. (The reason
    to do two sensors per cog is there are two counters per cog). One initialization of a counter
    register and you will have no trouble whatsoever with whatever frequency you want (up to 40MHz.)

    If anyone takes up the assembly challenge, note that the high frequency is 1MHz, so you have 80 cycles.
    You don't want to miss a pulse (or transition) when updating memory, so you have to be somewhat
    careful. But I bet you can do all 8 with a single cog.

    I know how I'd do it, but I'm going to hold off posting to see what others come up with. It's not trivial
    (at least, it's not trivial for *me*) but it's fun.
Sign In or Register to comment.