counting (up to 400) milliseconds (while waiting for a pulse)
NickeA
Posts: 5
Hi!
I need to measure the time·that passes between input pulses (on a bs2). My first thought was using the RCTIME or PULSIN·commands, but·they·can't measure time spans longer than 131 milliseconds.·Maximum time between pulses from my sensor is 400 milliseconds.
My second tought was to make a loop that counts milliseconds:
1. reset counter
2. check pin for pulse
3. if pulse appeared then return counter and end
4. pause 1 millisecond
5. increment counter
6. return to 2.
However, I now suspect that the code itself takes enough time to make the result wrong. I guess that 300 roundtrips in the loop takes considerably more than the 300 milliseconds that the counter would return.
Is that a correct suspicion?
Another solution would be to use RCTIME/PULSEIN several times and keeping track of how many times it "times out". I would then use the result of the RCTIME/PULSEIN and add 131 milliseconds for every time it "timed out".
Do you think this would work? Do you have a better solution?
Best regards, Nicke
·
I need to measure the time·that passes between input pulses (on a bs2). My first thought was using the RCTIME or PULSIN·commands, but·they·can't measure time spans longer than 131 milliseconds.·Maximum time between pulses from my sensor is 400 milliseconds.
My second tought was to make a loop that counts milliseconds:
1. reset counter
2. check pin for pulse
3. if pulse appeared then return counter and end
4. pause 1 millisecond
5. increment counter
6. return to 2.
However, I now suspect that the code itself takes enough time to make the result wrong. I guess that 300 roundtrips in the loop takes considerably more than the 300 milliseconds that the counter would return.
Is that a correct suspicion?
Another solution would be to use RCTIME/PULSEIN several times and keeping track of how many times it "times out". I would then use the result of the RCTIME/PULSEIN and add 131 milliseconds for every time it "timed out".
Do you think this would work? Do you have a better solution?
Best regards, Nicke
·
Comments
Post Edited (Mike Green) : 5/6/2007 8:15:17 PM GMT
PULSIN thePin,0,time
IF time = 0 and thePin = 1 then
."
Hmm...Do we assume that time = 0 means that we found a startpuls but PULSIN timed out before we found endpulse? It might as well be that PULSIN didn't find the startpulse, and therefore didn't start counting at all, or?
"PULSIN will wait, for the desired pulse, for up to the maximum pulse
width it can measure [noparse][[/noparse]131ms]... ...If it never sees the start of the pulse, or the pulse is too long PULSIN "times out" and store 0 in Variable."
It's a pity it returns 0 in both cases. It makes my task a bit harder.
One thing the BASIC Stamp can do for a protracted period is count pulses. With a little extra hardware, you can convert your pulse intervals into intervals of the same length containing a 32.768KHz clock. Then you can use the COUNT command to count the number of clocks within the interval. The COUNT command requires a fixed interval over which to count. You will have to estimate this interval so you catch one, and only one, pulse interval. Whether this is realistic depends on the maximum and minimum spans and how fast the interval can change. Here's how I'd code it:
····1. Wait for Pulses / 2 to be high.
····2. Wait for Pulses / 2 to go low.
····3. COUNT for estimated interval * 2.5
····4. Compute time from number of clocks counted.
A schematic of the extra hardware, along with some waveforms, is shown below. The oscillator uses a 32.768KHz watch crystal. You may have to fiddle with the resistor and capacitor values for reliable oscillation.
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com