View Full Version : Two intervals from one pulse train

Jim Barnes
03-19-2009, 04:30 AM
Hi Gang,

Need to measure two time intervals from one signal.··First is the time high of a ~ 1-5 ms positive going pulse.· Secons is the period, or time·from the same initial high edge to the next high edge.· Ideally this is a 10:1 ratio, and ultimately the ratio is what I'm after, so a duty cycle measurement might suffice (?)

My pseudo-code:

VAR long ratio, long t1, long t2, long counter

·t1 = t2 = ratio = 0

wait for high edge
····· start counter··································· OR······· start ctrA, ctrB ·on highedge
····· wait for low edge·········································
············ t1 = counter·· 'short time······················ Stop ctrA on low edge,·· t1 = ctrA
····· wait for next high edge·································Stop ctrB on·high edge,·· t2 = ctrB
·············t2 = counter·· 'long time
······ratio = t2/t1


Start ctrA

The waitpeq looks promising but I thought the built in counter functions may be a better choice but alas, I am slightly confused with their structure.··I need counter resolution of at least 1 us· or better, and preferably the two times are measured from the same starting edge.·
Any help is appreciated.· Thanks,



03-19-2009, 01:36 PM
You don't even need any cog counter. The system counter is sufficient:

toHigh1st := cnt
toLow := cnt
highDuration := toLow - toHigh1st
periodDuration := toHigh2nd - toHigh1st
ratio := highDuration / periodDuration

You can implement wait...() function with wait... instructions in SPIN or PASM. If they need have the same execution / delay times (do they?) you won't introduce any error by using this software instead of hardware.

Airspace V - international hangar flying!
www.airspace-v.com/ggadgets (http://www.airspace-v.com/ggadgets) for tools & toys

Jim Barnes
03-19-2009, 11:27 PM
Thanks v-P,
I'm trying to figure this language out and not be totally helpless.· The way I understand what you wrote is that I use the waitpeq() as follows for a pulse on, say, ·pin3:

waitpeq(%0000,· %1000, 0)······ '· wait for low on pin3 before starting--the mask is always %1000 to isolate pin3
waitpeq(%1000, %1000, 0)······· '· wait until first high transition of pin3
toHigh1st := cnt······················· ' initial count
waitpeq(%0000, %1000,0)········ ' wait for pulse to come back to low
toLow := cnt····························· ' count acquired as of end of pulse
waitpeq(%1000, %1000,0)········ ' wait for next high transition
tohigh2nd := cnt······················· ' count acquired as of next high

'· now post processing

highDuration := toLow - toHigh1st
periodDuration := toHigh2nd - toHigh1st
ratio := highDuration / periodDuration

Thanks for your time and please point out any errors in my way of thinking!!


03-19-2009, 11:50 PM
Try this concrete code (in opposite to pseudo code) :

PUB GetRatio(pin) : ratio | t1,t2,pmsk
pmsk := |< pin
waitpne(pmsk,pmsk,0) 'wait until low
waitpeq(pmsk,pmsk,0) 'wait until high (pos edge)
t1 := cnt
waitpne(pmsk,pmsk,0) 'wait until low (neg edge)
t2 := cnt-t1
waitpeq(pmsk,pmsk,0) 'wait until high (pos edge)
t1 := cnt-t1
ratio = t2*1000/t1 'ratio in permille (1/1000)

I have it not tested, this is your turn...


Edit: Sorry, missed your last post, looks very similar :)

Post Edited (Ariba) : 3/19/2009 5:01:03 PM GMT