Shop OBEX P1 Docs P2 Docs Learn Events
Need advise on precision timing? — Parallax Forums

Need advise on precision timing?

wolffwolff Posts: 43
edited 2015-03-04 18:26 in BASIC Stamp
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!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2015-02-28 11:04
    There are two issues here ...
    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&gt; for schematics).
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-03-02 11:48
    What is the nature of the event? What is the width/type of signal?
  • ercoerco Posts: 20,257
    edited 2015-03-02 13:50
    wolff wrote: »
    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!

    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.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-03-02 14:40
    Yea, I would think if the pulse isn't too short even a BS1 could do it, though I'd really recommend a BS2 with an external RTC. Also, how critical is it if say, one event is missed?
  • Dakota WriterDakota Writer Posts: 34
    edited 2015-03-04 09:19
    I have a similar problem, and perhaps my solution could help you. I'm working on a circuit for my Boe Bot for a Sumo competition. For that, I need to monitor several inputs (IR LEDs, QTI Sensors, Whiskers, etc), analyse the data, then modify the pulse width for the servos. The servos need to be updated every 20ms, without fail. In the past, I've tried to use code timings to guess how much time I had to pause, which didn't always work well at all.
    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.
    What is the nature of the event? What is the width/type of signal?
    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!
    RC Timer Circuit.jpeg
    1024 x 745 - 97K
  • ercoerco Posts: 20,257
    edited 2015-03-04 13:56
    Dakota: Interesting solution! You probably know this already, but Parallax has an off-the-shelf solution to offloading servo management: their ServoPal: http://www.parallax.com/product/28824
  • Dakota WriterDakota Writer Posts: 34
    edited 2015-03-04 16:06
    Interesting solution!
    Thank you! I was certainly happy when it dawned on me; you know how it is.
    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...
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-03-04 18:26
    The following program counts events (transitions from low to high) on Stamp pin p0, and then after an interval it prints out the total to the debug screen. The inner loop tests the input pin to detect the low to high transition, and it also counts "ticks" which increment each time around the inner loop. It takes 40225 times around the loop to make one minute. The outer loop counts the number of minutes passed and branches to the DEBUG after interval=1 hour. The duration of the measurement interval is quite deterministic as it depends solely on the ceramic resonator and branchless PBASIC code. The number of ticksPerMinute needed will vary somewhat from stamp to stamp, and the value for the vanilla BS2 will be a smaller number than for the BS2pe. The event high time and the low time at p0 must each be longer than about 2 milliseconds. The execution time of the inner loop is about 60/40225 = 1.5 milliseconds.
    [font=courier][size=1]' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' author: Tracy Allen
    ' example timing/counting loop
    ' counts events on p0
    ' totalizes for one hour
    ' prints out the total to the debug screen
    ' starts another cycle.
    
    
    ticks VAR WORD
    minutes VAR WORD
    counts VAR WORD
    x0 VAR WORD
    x1 VAR WORD
    
    interval CON 60  ' minutes
    ticksPerMinute CON 40225  ' varies by stamp and stamp type
    
    DO
      minutes = 0
      counts = 0
      DO   ' the minutes counter
        TOGGLE 1   ' p1 should toggle once per minute, for testing
        x0 = IN0
        ticks = 0
        DO   ' the one minute duration loop
          x1 = IN0
          counts = counts + (x1 ^ x0 & x1)
          x0 = x1
          ticks = ticks + 1
        LOOP UNTIL ticks = ticksPerMinute
        minutes = minutes + 1    
      LOOP UNTIL minutes=interval   ' now print result
      DEBUG DEC counts, CR
    LOOP[/size][/font]
    
Sign In or Register to comment.