PDA

View Full Version : Cog timers



Christofer A
01-09-2008, 04:09 PM
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.

OzStamp
01-09-2008, 04:46 PM
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 A
01-09-2008, 08:54 PM
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[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 Green
01-09-2008, 09:03 PM
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 Stabler
01-09-2008, 09:41 PM
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 A
01-15-2008, 03:58 AM
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. http://forums.parallax.com/images/smilies/blush.gif

Thanks everyone for posting.

Mike Green
01-15-2008, 04:07 AM
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.

deSilva
01-15-2008, 04:29 AM
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.