PDA

View Full Version : Accurate Timing

Jason Crystal
07-26-2005, 01:34 AM
Hello,

I wish to measure the length of time between events (specifically, the length of pulses ranging from 0.5 to 8 seconds). However, these measurements must be very accurate (preferably to the millisecond). The Pulsin command doesn't work for me because of the maximum pulse width allowed.

One option that I've explored is attaching a 555 chip and counting the number of pulses. However, is there a way for the Stamp to count these pulses while still performing other functions?

Any ideas or suggestions would be welcome!

Thank you!
-Jason

Paul Baker
07-26-2005, 04:09 AM
I must applogize, my original code was not according to PBASIC requirements, the following code should work:

OuterCount VAR Byte
time VAR Word

OuterCount = -1
time = 0

DO WHILE time = 0
PULSIN InPin, 1, time
OuterCount = OuterCount + 1
LOOP

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10

Post Edited (Paul Baker) : 8/1/2005 3:43:47 PM GMT

Jason Crystal
07-26-2005, 11:53 PM
One problem though...

Doesn't PULSIN sense the TRANSITION from Low to High (or vice versa) when determining whether to evaluate the time? From what I gather from the documentation, if PULSIN does not receive a transition within the specified period of time (based on what Stamp model you have), it will return zero. So, given your function above, I think it would always return zero (or infinitely loop) because in an 7sec pulse there would only be one transition.

Or am I reading into this incorrectly?

Thanks,
-Jason

Paul Baker
07-27-2005, 12:19 AM
You are correct, PULSIN will return 0 each time a transistion is not detected and the PULSIN will be called again, but once the transition does occur the loop is exited (with time containing the last value), the total time will be (OuterCount * 65536 + time). So if your pulse is 7 seconds and you are using a BS2 (2uS resolution) the code above would execute PULSIN 53 times (each PULSIN returning the maximum value of .131072 Seconds) the 54th time PULSIN will return a value of 26,592 (actual value will be a bit less than this because of time taken to execute the loop and the increment). Knowing the number of times PULSIN was executed and the final value returned, you can calculate the length of very long pulses. For the above example you get 53*65536 + 26,592 = 3500000.·3,500,000 * (2uS)= 7 seconds.·With the variable OuterCount being a byte, the maximum pulse you can detect is a little over 33 seconds, If you make OuterCount a Word variable you can measure pulses as long as 2 hours and 23 minutes!

The only time the code will never exit is if there is never a transition on the line you are monitoring.

You can think of OuterCount as just extending the value of PULSIN into a larger number, with OuterCount a byte you are working with a 24-bit number (16 bits returned by PULSIN and 8 bits for OuterCounter). With·OuterCount a word variable you are working with a 32 bit number (16 bits from PULSIN, 16 bits from OuterCount).

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10

Post Edited (Paul Baker) : 7/26/2005 5:31:03 PM GMT

Jon Williams
07-27-2005, 09:10 PM
You cannot count pulses "in the background" as your "... do other functions" question suggests. The BASIC Stamp is single threaded and while running your counting subroutine you are only doing that.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Paul Baker
07-28-2005, 12:43 AM
Thanks Jon, I glossed over that part of the question. Jon is correct, my code doesn't permit you to do other things while you are measureing the pulse width. Now another point, you are merging the terms counting pulses and pulse width, these are two completely different things. Counting pulses returns the number of pulses that occured during a time period, measuring the width returns the width of a single pulse. These are two different things, which are you trying to do? Counting pulses externally is fairly simple with a binary counter, measuring widths of pulses is not as easily accomplished externally.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10

Jason Crystal
07-28-2005, 12:51 AM
Thanks for all your help! I've actually got it working with the help of Paul initial code. Very insightful.

Thanks guys,
-Jason

Paul Baker
07-28-2005, 12:54 AM

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10

lfreeze
08-01-2005, 03:48 AM
I have the same problem as described by Jason. I am using a 555 timer to generate a pulse of about .4 seconds. I need to

verify this pulse length. I tried the code posted by Paul baker but keep getting zero's . Here is the code I am using,

any help would be really appreciated.

' {\$STAMP BS2}
' {\$PBASIC 2.5}

OuterCount VAR Word
time······ VAR Word
OuterCount = -1
DO
·PULSIN 0, 1, time
·OuterCount = OuterCount + 1
LOOP UNTIL time = 0

DEBUG " outercount = ", DEC outercount,CR
DEBUG " time······ = ", DEC time····· ,CR

Paul Baker
08-01-2005, 09:17 PM
The difference I see is you have reversed your LOOP logic, my code will continue to loop until a non-zero value is returned, your code will continue to loop until it finds a zero value, "LOOP UNTIL time = 0" and "WHILE time =0" evaluate to the exact opposite meaning.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10

lfreeze
08-01-2005, 09:41 PM
Thanks for your reply.·· I must be making a very obvious mistake. I copied
you code exactly. When I run it, I get an error message with ·"while" highlited
saying "expected a label, variable, or instruction".·· ·If I change the "while time =0"
to "loop until time > 0"· The program runs, but returns zeros in my debug statements.

This seems like such a great programming tool for measuring long pulses. I hope
·you will be able to solve this for me.

Larry

' {\$STAMP BS2}
' {\$PBASIC 2.5}
OuterCount VAR Word
time······ VAR Word
DEBUG " outercount = ", DEC outercount,CR
DEBUG " time······ = ", DEC time····· ,CR
OuterCount = -1
DO
·PULSIN 0, 1, time
·OuterCount = OuterCount + 1
'LOOP UNTIL time > 0
WHILE time=0
DEBUG " outercount = ", DEC outercount,CR
DEBUG " time······ = ", DEC time····· ,CR

Chris Savage
08-01-2005, 09:54 PM
·· Okay, I see what's happening...You are not using LOOP.· You must have a LOOP keyword for each DO.· Paul had a LOOP UNTIL and you change it to just WHILE.· Try LOOP WHILE.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Post Edited (Chris Savage (Parallax)) : 8/1/2005 2:57:41 PM GMT

lfreeze
08-01-2005, 10:14 PM
Thanks Chris,· It now runs but stays in the loop. The stamp is ·not reacting to

the transition from· zero to five volts. I connected my volt meter in parallel with Stamp pin zero, and because the pulse from the 555 is so slow (400MS) I can see the change,

on my meter, so I know the transistion from zero to five is reaching the stamp.

In order to eliminate the stamp as the source of my problem, I duplicated the little circuit in the stamp manual for PULSIN. It ran perfectly. So again I am stumped.

Larry

Chris Savage
08-01-2005, 10:25 PM
Larry,

·· When I run your code I get the following results...With a 10hz signal into P0 I get:

outercount = 0
time = 0
outcount = 0
time = 22966

·· When I change the frquency to 100hz, I get:

outercount = 0
time = 0
outcount = 0
time = 2300

·· When I change the frequency to 1000hz, I get:

outercount = 0
time = 0
outcount = 0
time = 230

Not sure what exactly you're trying to do.· A negative one for start value will result in 65535 in that variable initially.· It will be zero on the first pass through the loop, and since there is a signal, I don't see it changing on my end.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Paul Baker
08-01-2005, 10:45 PM
Grrr,
I just found out you can't quote your own post, ended up editing the original.

Here it is reprinted:

I must appologize, my original code was not according to PBASIC requirements, the following code should work:

OuterCount VAR Byte
time VAR Word

OuterCount = -1
time = 0

DO WHILE time = 0
PULSIN InPin, 1, time
OuterCount = OuterCount + 1
LOOP

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10

Chris Savage
08-01-2005, 10:55 PM
·· Remember, for this to compile you will need the Stamp and Syntax declarations as well as defining InPin to whatever pin you are using.· In any case, OuterCount should still return a 0 if you have a pulse coming in when this code is executed.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

lfreeze
08-02-2005, 12:20 AM
Thanks everyone for the responses. I am still scratching my head......

Here is what I am attempting to do

I have a 555 timer chip I have set it up to generate a very slow pulse of .76 seconds on and .69 seconds off. I have verified that the 555 is outputing a pulse (+5V) as it is slow enough to observe on my volt meter.

I know that the value of the components I've used will cause these times to be slightly off. I am trying to measure

the actual pulse· time using the pulsin command.· Still not sure where I have gone wrong.....

Chris Savage
08-02-2005, 01:31 AM
·· I know this doesn't help your immediate problem, but this would sure be a good job for our USB Scope!· =)· On another note, are you still getting zeros for results?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

lfreeze
08-02-2005, 01:57 AM
Yes, still getting zeros. I· think I give it a rest for a few hours then try rebuilding everything from scratch with all new
components. It's one of those things that should work but doesn't. I suspect it may be how I am connecting the output of
the 555 to the stamp. It's pretty straight forward. The output goes thru a 1k resistor then to the stamp.
Thanks for all the tips, It got me going in the right direction....
···

Chris Savage
08-02-2005, 02:04 AM
If the 555 is being powered by 5 volts, try a smaller resistor, like 330 ohms and see what happens then.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

lfreeze
08-02-2005, 02:22 AM
I am using 5v as power for the 555, I tried a 330ohm resistor in series with the output but get the same result. I removed the resistor entirely and still get zero's, fortunatly no smoke.....