Shop OBEX P1 Docs P2 Docs Learn Events
Cog timers — Parallax Forums

Cog timers

Christofer AChristofer A Posts: 9
edited 2008-01-14 21:29 in Propeller 1
Hi all,

I'm hoping to get some help with the timer CTRA.

I would like to measure the pulse with with timer CRTA using the %01X00 POS/NEG detector mode, I only need to measure the pulse with when pin is high.
I'm using this method (code below) today but would like to learn how to do it without waitpeq command.
repeat
    PHSA := 0                                                                        ' reset PHSA to 0
    waitpeq(%10000000000000000, %10000000000000000, 0)  ' Wait for pin 16 to be high
    FRQA := 1                                                                        ' starts the CTRA counting from 0
    waitpne(%10000000000000000, %10000000000000000, 0)  ' Wait for pin 16 to be low
    waitpeq(%10000000000000000, %10000000000000000, 0)  ' Wait for pin 16 to be high
    FRQA := 0                                                                        ' stop the accumulation




Would be grateful fore spin example of how to to this.

Thanks in advance.
Christofer A.

Comments

  • OzStampOzStamp Posts: 377
    edited 2008-01-09 09:46
    Hi Christofer A.

    An excellent source for info like this is to have a look at Martin Hebels BS2function.obj
    goto the www.obex.parallax.com.au website and download that object..
    Look thru the object and find the pulse in PUB.. it is all there.

    cheers Ron Nollet Mel OZ
  • Christofer AChristofer A Posts: 9
    edited 2008-01-09 13:54
    Thanks for your reply OzStamp,

    As far as I can understand in BS2, PUB PULSIN_Clk you use a method similar to my example, or am I misunderstanding something?

    What I was looking for was a solution when the cog don't need to intervene unless for initialize and reading the result.
    Similar to generating one puls by:

    ctra := %00100_000 << 23 + 1 << 9 + Pin  'Establish mode and APIN
    frqa := 255                                                'Set FRQA so PHSA[noparse][[/noparse]31] toggles every clock
    
    



    maybe it's not possible? After reading the PropellerDatasheet-v1.0.pdf I got this Idea that this was possible! but no luck so far.

    Any suggestions?

    //Christofer
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-09 14:03
    The cog does need to intervene on every pulse because the count is cumulative. FRQA is added to PHSA on every clock where the input is positive or negative (depending on the mode). You have to read PHSA and reinitialize it after the true to false pulse transition. The advantage of using the counter is its high resolution.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-01-09 14:41
    If you were expecting a single pulse only you could use one of the logic modes otherwise you must do something to stop the counting as Mike says.

    Graham
  • Christofer AChristofer A Posts: 9
    edited 2008-01-14 20:58
    OK, now I see how it works.

    Thank you very much Mike Green. I didn't see that It was that simple, just to read PHSA and clear it between pulses. blush.gif

    Thanks everyone for posting.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-14 21:07
    In Spin this is easy to do once the counter is set up:
        waitpeq(%10000000000000000, %10000000000000000, 0)  ' Wait for pin 16 to be high
    ' you can do something useful here if it's not too much longer than the high period
        waitpne(%10000000000000000, %10000000000000000, 0)  ' Wait for pin 16 to be low
        count := phsa~
    ' you can do something useful here if it's not too much longer than the time between pulses
    
    


    The trick here is that you don't want to read phsa while the input is high since phsa will be incrementing
    and you don't want to sample phsa too close to the next pulse since there's a little delay between the
    waitpne and when phsa is sampled and you don't want the next pulse to begin before you've read phsa.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-14 21:29
    For many cases it is not really necessary to use the H/W counter,
    even CNT read from SPIN can give a good result without prior calibration, if done in the right way:
        waitpeq(%10000000000000000, %10000000000000000, 0)  ' Wait for pin 16 to be high
        count1 := CNT
        waitpne(%10000000000000000, %10000000000000000, 0)  ' Wait for pin 16 to be low
        count2 := CNT
    
    


    count2-count1 will give you the length of the pulse most likely up to 12.5 ns !!
    However, the pulse must be at least 15 µs long, which will restrict this simple approach to a few situations only.
Sign In or Register to comment.