Shop OBEX P1 Docs P2 Docs Learn Events
Chronometer — Parallax Forums

Chronometer

nelson_ddsnelson_dds Posts: 2
edited 2015-03-17 14:25 in BASIC Stamp
Good morning,

I'm a new user Parallax and new in this website. I use (with my work to study parallax) a Super Carrier Board 8Bit with a BS2p, but it's very dificult for me to make a Chronometer, i have also a Parallax 2x16 Serial LCD (Backlit). I want to make a chronometer with two buttons Start/Stop and reset. The buttons are P9 and P12 and the Rx of LCD are P1. I have no idea how to do this.

PS: I live in French Swiss why my poor english :)

Nelson

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2015-03-11 07:40
    What do you mean by a Chronometer? Do you want it to display the time of day or just use it to time an event? How accurate and precise do you need it to be? ... to the nearest second? to some fraction of a second?
  • nelson_ddsnelson_dds Posts: 2
    edited 2015-03-16 01:07
    The nearest second. And it's like to start the chrono and stop with the same button, and one other button to restet just when it's stopped. It's to start a chrono at the moment, so when we want.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-03-16 01:47
    You actually might find this all easier to do with the Propeller and much better precision. There 32bit precision and faster clocking.

    But I can understand the desire to start in PBasic and an 8-bit solution.

    First, you need an infinite loop that provides you with a 1 second clock tick.

    Second, you need a register that will accumulate this ticks whenever you desire to start and which allows you to read whenever you desire (while counting or after a stop)

    +++++++++++
    Having suggested the above, 8 bits will only count to 255 seconds and then roll-over. If you desire more than 255 seconds of timing, you must chain together another register to get 16 bits, 24 bits, 32 bits and so on.

    And below all that, you have to create the actual one second tick from a 20,000,000 hz clock (or more, depending on which BasicStamp). That too will require a set of registers able to accumulate far more than 8 bits.

    ++++++++++++
    The Propeller would be much easier because you can have one Cog creating a one-second tick and running free without interferance, another Cog working as your Chronometer without interferance, and another Cog managing button i/o without interferance, and a final Cog providing output to LCD without interferance.

    There lies the huge advantages of parallel processing. You avoid interuptions and get precise deterministic computing.

    ++++++
    With the BasicStamp, you are going to have to be very careful to have all these different tasks share on CPU without affecting the underlying clock tick. In all honesty, I cannot recall if it can be done or if attaching a RTC might be required.

    Someone else may mention code that has already done so with the BasicStamp.

    Here is a 2007 which I have not read entirely. It may help. But it too begins by suggesting using the Propeller.
    http://forums.parallax.com/showthread.php/92076-Stopwatch-Timer-Counter

    And this...
    http://www.slideshare.net/vrehere/implementing-a-digital-stopwatch-using-basic-stamp2

    +++++++
    If SPIN seems to much for you, you could do this quite easily in Forth on a Propeller. I would try Tachyon Forth to begin with.
  • ElectrodudeElectrodude Posts: 1,660
    edited 2015-03-16 06:51
    The BS2p is a 16 bit processor, meaning it can count to 65535, not just 255. However, a Propeller would still be easier and cheaper (unless you already have the BS2p).

    Loopy mentioned using 4 separate cogs. You could do it easily in even one cog and still have deterministic (or precise, at least) timing, although I agree that serial for the LCD should have its own cog. Timing won't be nearly as precise on a BS2 - you can't check buttons and update an LCD while precisely keeping time. The only real timing the BS2 has is the sleep instruction, which won't take into account the fact that updating the LCD takes some unknown and not easily accountable time to be updated.

    If Forth seems too much for you, you could do this quite easily in Spin on a Propeller. :)
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2015-03-16 10:16
    With second resolution the BASIC Stamp 2 could do this using an external RTC such as the DS1302. I've done it many times myself in the past and created many projects which track time for control applications using these parts. If you want to use the BASIC Stamp module that's perfectly fine. =) Let me know if you need additional information.
  • tomcrawfordtomcrawford Posts: 1,129
    edited 2015-03-17 14:25
    Let me begin by saying "Of course I would use a Propellor. It's cheaper, easier to program, or ever so much more capable than a Stamp".

    But the original poster said he had a BS2 and so I wondered if one *could* write a stop watch without recourse to an external timer source. It has to be able to drive a Parallax 2x16 serial LCD and has to be, say, +- 1 per cent correct.
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    '---------I/O pins
    SS    PIN   4               'Start/Stop Switch
    Reset PIN   3               'Reset Switch
    LCD   PIN   2               'Serial Out to LCD
    Led1  PIN   1               'one led (not used)
    Led0  PIN   0               'another led
    
    '++++++++++Constants==========
    Baud   CON        110            '19200, etc etc
    Magic  CON        267           'magic delay counter
    
    '----------------variables-------------------
    Total   VAR    Word       'total seconds in 1/10th (allows 6538 seconds total)
    Lap     VAR    Word       'current seconds in 1/10th
    Delay   VAR    Word       'delay counter
    Accum   VAR    Word       'temporary
    
    begin:
    GOSUB StartLCD
    OUTPUT Led0
    
    top:
    HIGH Led0               'scope sync
    LOW Led0
    Total = Total + 1        '1/10th second
    IF SS = 0 THEN            'start/stop button
       Lap = Total            'button pressed lap time
       ELSE
       Lap = Lap              'keep the same
       ENDIF
    
    IF Reset = 0 THEN            'reset button
       Total = 0
       ENDIF
    
    GOSUB DispLap
    GOSUB DispTotal
    FOR Delay = 0 TO magic      'use up the rest of 100 msec
        Accum = Accum + 1
        NEXT
    GOTO top                    'every 1/10th second
    
    
    
    DispLap:
    SEROUT LCD, Baud, [135]     'top line, position 7
    SEROUT LCD, Baud, [DEC1 Total DIG 4]
    SEROUT LCD, Baud, [DEC1 Total DIG 3]
    SEROUT LCD, Baud, [DEC1 Total DIG 2]
    SEROUT LCD, Baud, [DEC1 Total DIG 1]
    SEROUT LCD, Baud, ["."]
    SEROUT LCD, Baud, [DEC1 Total DIG 0]
    RETURN
    
    DispTotal:
    SEROUT LCD, Baud, [155]     'bottom line, position 7
    SEROUT LCD, Baud, [DEC1 Lap DIG 4]
    SEROUT LCD, Baud, [DEC1 Lap DIG 3]
    SEROUT LCD, Baud, [DEC1 Lap DIG 2]
    SEROUT LCD, Baud, [DEC1 Lap DIG 1]
    SEROUT LCD, Baud, ["."]
    SEROUT LCD, Baud, [DEC1 Lap DIG 0]
    RETURN
    
    StartLCD:
    HIGH LCD
    PAUSE 100
    SEROUT LCD, Baud, [12]    'clear screen
    PAUSE 5
    SEROUT LCD, Baud, [22]    'turn off cursor
    SEROUT LCD, Baud, [128, "Total: "]
    SEROUT LCD, Baud, [148, "  Lap: "]
    RETURN
    
    

    There are two sources of inaccuracy. The inaccuracy of the resonator that provides basic instruction timing, and the differences in the number of instructions executed in each pass through the loop. In the description of the PAUSE instruction, they say the accuracy of the resonator is +-1 percent.
    Now we have to make sure that every pass through the loop takes exactly the same number of instructions. That is why the lap counter is written whether or not the button is pushed and that is why the SEROUT instructions are one character at a time.
    As much as I hate magic numbers, I had to tune the delay (using a logic analyzer), and it would likely be needful to tune this for each BS2 module.
    I would never consider this an anything other than an exercise, but I do think you can build a useable chono with a BS2 and no external timing source.
Sign In or Register to comment.