Shop OBEX P1 Docs P2 Docs Learn Events
Real time counter — Parallax Forums

Real time counter

EnriqueEnrique Posts: 90
edited 2007-06-06 16:33 in Robotics
I would like to measure the elapsed time between to events. Does the BS2 have a counter that will keep track of clock pulses?
·
·
Thanks,
·
Enrique

Comments

  • ZootZoot Posts: 2,227
    edited 2007-06-06 16:33
    Technically, no. The closest you could come without external hardware would be to increment a variable counter in your code, and structure your code so that you get consistent times through your main "loop". Here is a simple example:

    timecounter VAR Word
    
    DO
       HIGH 0
       PAUSE 50
       LOW 0
       PAUSE 450
       timecounter = timecounter + 1
    LOOP
    
    



    In theory, timecounter should count up twice per second (total pause = 500ms or .5 second). Naturally, that presumes all the instructions are "instantaneous". If you hook a scope or counter up to PIN 0 when running the code above, you will not get a 50ms pulse twice per second -- the instructions themselves chew up time. Experimenting with your code setup can help you get reasonably close timings if timing isn't super critical (e.g. "has it been just about 890 seconds"). If you are running a lot of code in your main loop, you can put the timecounter handler in a subroutine and sprinkle calls to the subroutine in your code so that the time is kept reasonably close.

    If you need more accurate times, consider an external real time clock like the DS1302 (serial) or the DS1307 (I2C). They are inexpensive and you only need a crystal and (optional) battery backup.
    You can also use a timer like 555 and set it up for a consistent square wave output -- feed that output into a Stamp pin and count each time the pin changes state. With a 555, you will still have to grapple with how often you check the pin during your code loop, so that you don't miss any pin state changes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
Sign In or Register to comment.