Need advise on precision timing?
wolff
Posts: 43
I want to count a number of events that occur over a given time period. The events occur about once a second and the time period is about and hour. The time period doesn't have to be exactly one hour but must be precisely the same length of time each period (give or take 1 or 2 seconds) Does the timing circuit within the stamp have this level of precision? If not can anyone recommend a clock chip easily interfaced with a stamp (that has the precision I require?)
Thanks for ANY help or suggestions!
Thanks for ANY help or suggestions!
Comments
1) The Stamp runs off a resonator which should have the short-term precision needed, but it's difficult to actually do the counting reliably. The COUNT statement will suspend execution for periods up to a little over a minute and count pulses coming in with excellent precision. Look at the description of the statement in the Stamp Reference Manual.
2) After the COUNT statement executes, there would be a short period (a few hundred microseconds) until the Stamp could start another COUNT statement. During this time, the Stamp would completely ignore any pulses coming in and might miss one.
Even if you were to use an external clock chip, the Stamp can only do one thing at a time and might miss an event while it's checking the time looking for the end of a timing interval. There are easy ways to add external hardware to get around these Stamp limitations. For example, you can use an external flip-flop that's triggered by the external event and can be read by a Stamp I/O pin. The flip-flop changes its output from 0 to 1 where it stays until the Stamp resets it. The Stamp can read the flip-flop and reset it well within a half second period. Similarly, you can make a 0.5 second accurate timer with a 32KHz crystal and a 14-stage counter like the 74HC4060 or CD4060 (see the datasheet <http://pdf1.alldatasheet.com/datasheet-pdf/view/12183/ONSEMI/MC74HC4060A.html> for schematics).
Doesn't sound too demanding at all. If the events are about a second apart and you have the right circuitry to reliably detect each event/pulse, I would not use the COUNT command, just a couple of calibrated, nested timing loops using either the button command or an "IF PIN1=0 THEN abc" command. Obviously you will use a word variable for the count since the expected number is ~3600.
Easily done with any Stamp, even a BS1 is up to the task. You do need to make sure that when you sense an event and branch to increment the counter that your routine takes the same amount of time as if you didn't branch. A bit of experimenting and verifying is in order, but all in all, a very doable and worthy Stamp project.
Per Chris and Mike, any additional details you can share might help us make more specific recommendations.
My current plan is to make an RC circuit that drops from approx. 5V (logical high) to 1.5V (logical low) in about 19-20ms. After every few lines of code, I'll check the (decaying) voltage across the capacitor. When it reaches logical low, I'll send pulses to the motors, then recharge the capacitor. If it works, it will sure beat code timings!
I bring this up because, depending on how long each monitored event lasts, a similar method could work for you. If you charge up a capacitor, then let the voltage start decaying (with values such that it will drop to 1.5V in, say, 1 hour), you can watch your timer via an input pin without taking much time out of your monitoring loop. Just a suggestion.
Of course, for a capacitor to discharge over one hour, you'll probably need a fairly large capacitor and/or resistor. I'm attaching a preliminary sketch of my circuit that I made last night, showing the charging and discharging states. If you can't find the right combination for 3600 seconds, you could always go for, say, 10 seconds and reset the timer 360 times. This, though, is the real question that needs to be answered. If the time it takes to check the input state of a pin is too long, then this circuit isn't worth the trouble of setting up.
Good luck!
I actually hadn't known about the ServoPal, though it certainly would provide an elegant answer. Unfortunately, for the contest I'm entering, the tight rules would, er, rule out the piece. Thanks anyway.
Man, that sketch I did looks ugly! It was late...