Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 measuring elapsed time (stop watch) ? — Parallax Forums

DS1302 measuring elapsed time (stop watch) ?

techstudenttechstudent Posts: 21
edited 2011-03-25 14:45 in BASIC Stamp
I got the RTC to work and display time/date onto an LCD, but what I am after is using it for measuring elapsed time, somewhere in the ranges of 45 min to 2hrs 45mins (seconds are not important) I want to set it up like a stopwatch where I can push a button to start measuring time elapsed and then push the button again to stop and display the amount of time elapsed. My problem is in figuring out how to do the math on the hrs/mins data from the DS1302 to count elapsed time? My clock is in 24hr mode, and then the next question would be how to compensate for timing 'over midnight' and doing the math from when the hours go from 24 to 1. I think what is confusing me is that the data is displayed in HEX2 format, and to go from hour HEX2 "09" to hour HEX2 "10" it is skipping HEX2 "0A" to HEX2 "0F"

I am thinking others have built stopwatch type applications, and if you could point me to any examples/discussions or just give a general idea of how to go about doing this?

Thanks

Comments

  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-01-07 23:58
    ·This what you need to do what you want to DO
    ·
    ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    Init:
      reg = CWPr                            ' Initialize DS1302
      ioByte = WPr0                         ' Clear Write Protect
      GOSUB RTC_Out                         ' Send Command
     
      reg = %10010000
    ioByte = %10101001
    GOSUB RTC_Out
     
     
      reg = $8A          ' Point to register 8Ah
    ioByte = %00000000 ' initializes the clock days to "00"
    GOSUB RTC_Out      ' Send Command
    
     reg = $84          ' Point to register 84h
    ioByte = %00000000 ' initializes the clock in 24 hour mode and clear the hours to "00"
    GOSUB RTC_Out      ' Send Command
    
    reg = $82          ' Point to register 82h
    ioByte = %00000000 ' clear the minutes to "00"
    GOSUB RTC_Out      ' Send Command
    
    reg = $80          ' Point to register 80h
    ioByte = %00000000 ' starts the clock by clearing the Clock Halt bit and clears the Seconds to "00"
    GOSUB RTC_Out      ' Send Command
    --------------------------------------------------------------------------------------------- 
    This will need to be running in a routine to keep the clock stop at the time that you want 
    ----------------------------------------------------------- 
    Halt_clock:
     reg = $80          ' Point to register 80h
     ioByte = %10000000 ' Set Clock Halt Bit
     GOSUB RTC_Out      ' Send Command
     RETURN
     
    -----------------------------------------------------------This only need to runned one time to restart the clock 
    ----------------------------------------------------------- 
    Restart_clock:
     reg = $80          ' Point to register 80h
     ioByte = %0110000 ' starts the clock by clearing the Clock   'Halt bit and clears the minutes to "00"
     GOSUB RTC_Out      ' Send Command
     RETURN
    
    


    ·Here is where I used in a Project @ this Post
    http://forums.parallax.com/showthread.php?p=737892

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 1/8/2009 12:15:45 AM GMT
  • techstudenttechstudent Posts: 21
    edited 2009-01-08 00:31
    Thanks for the quick reply, I failed to mention in the original post that I also wanted to record a time and date for when the event occurred, but it may just be a lot easier to use two RTC, one for recording the time/day, and the other as the stopwatch using the code posted above... thanks again, I think I will get to ordering another DS1302
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-01-08 01:49
     ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    

    Init:
      T_reg = T_CWPr                            ' Initialize DS1302
      T_ioByte = T_WPr0                         ' Clear Write Protect
      GOSUB T_RTC_Out
    

      E_reg = E_CWPr                            ' Initialize DS1302
      E_ioByte = E_WPr0                         ' Clear Write Protect
      GOSUB E_RTC_Out                         ' Send Command
    

      T_reg = %10010000
    T_ioByte = %10101001
    GOSUB T_RTC_Out
    

     E_reg = %10010000
    E_ioByte = %10101001
    GOSUB E_RTC_Out
    

    T_reg = $86          ' Point to register 86
    T_ioByte = %00000000 ' initializes the clock date to "00"
    GOSUB T_RTC_Out      ' Send Command
    

      T_reg = $8A          ' Point to register 8Ah
    T_ioByte = %00000000 ' initializes the clock days to "00"
    GOSUB T_RTC_Out      ' Send Command
    

     T_reg = $84          ' Point to register 84h
    T_ioByte = %00000000 ' initializes the clock in 24 hour mode and clear the hours to "00"
    GOSUB T_RTC_Out      ' Send Command
    

    T_reg = $82          ' Point to register 82h
    T_ioByte = %00000000 ' clear the minutes to "00"
    GOSUB T_RTC_Out      ' Send Command
    

    T_reg = $80          ' Point to register 80h
    T_ioByte = %00000000 ' starts the clock by clearing the Clock Halt bit and clears the Seconds to "00"
    GOSUB T_RTC_Out      ' Send Command
    

      E_reg = $86          ' Point to register 86
    E_ioByte = %00000000 ' initializes the clock date to "00"      0 to 31
    GOSUB E_RTC_Out      ' Send Command
    

    E_reg = $8A          ' Point to register 8Ah
    E_ioByte = %00000000 ' initializes the clock days to "00"      0 to 7
    GOSUB E_RTC_Out      ' Send Command
    

    E_reg = $88          ' Point to register 88h
    E_ioByte = %00000000 ' clear the months to "00"                0 to 12
    GOSUB E_RTC_Out      ' Send Command
    

     E_reg = $84          ' Point to register 84h
    E_ioByte = %00000000 ' initializes the clock in 24 hour mode and clear the hours to "00"
    GOSUB E_RTC_Out      ' Send Command
    

    E_reg = $82          ' Point to register 82h
    E_ioByte = %00000000 ' clear the minutes to "00"
    GOSUB E_RTC_Out      ' Send Command
    

    E_reg = $80          ' Point to register 80h
    E_ioByte = %00000000 ' starts the clock by clearing the Clock Halt bit and clears the Seconds to "00"
    GOSUB E_RTC_Out      ' Send Command
    

    Here what you have to do if you use two DS1302 time chips

    One word of caution you will run out of memory very fast if you are not ·careful how you write your code routine this happen to when writing this code using two time chip if you want to keep track of total timed
    event

    If you can find a way to use the DS1302 Ram to save these values Please Post Your Code
    I would like to see it when your done

    Here is the code where Rev the frist code

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 1/8/2009 2:00:55 AM GMT
  • techstudenttechstudent Posts: 21
    edited 2009-01-08 04:09
    Wow, thanks for all the help Sam, I won't have time work through it all for a couple of days, but if I figure out anything about utilizing the DS1302 Ram I will let you know... I also appreciated seeing your project and all the pics/schematics thanks again..

    ~techstudent
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-01-08 17:38
    Another way to approach this with is to compute the elapsed time from the real time. Read hours and minutes from the DS1302, then:

    julian0 = hours.nib1 * 10 + hours.nib0 * 6 + minutes.nib1 * 10 + minutes.nib0

    That converts the HEX2 into a number that goes straight from 0 to 1439 as time goes from 00:00 to 23:59. The format from the clock is actually called BCD (binary coded decimal), which is similar to HEX, but as you noticed, it includes only digits 0 to 9 and not A to F. The above calculation goes strictly left to right (no precedence for multiplication, a peculiarity of Stamp math).

    At the end of the time interval you calculate again

    julian1 = hours.nib1 * 10 + hours.nib0 * 6 + minutes.nib1 * 10 + minutes.nib0

    Then

    elapsed = julian1 - julian0 + 1440 // 1440

    The purpose of +1440//1440 is to account for possible transitions past midnight, positive definite.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-01-08 18:29
    One method I have used to calculate elapsed time is to simply increment or decrement a separate counter when the seconds register changes. This has several advantages…first of all it does not interfere with the normal functions of the RTC. You can set the date/time normally and access them at any time.

    Another benefit is you can have multiple counters, each with a resolution of 1 second. These can count up (like a stopwatch) or down (like a count-down timer) and can occur concurrently with each other. If you like each can be enabled/disabled via flag variable.

    Basically all you do is in your main loop when you need to count, each time the seconds change you update your counters as well as the old seconds value. This happens each iteration at exactly 1 second intervals and allows for many other events to happen between the changes, such as updating the enabled (if you use that) counters and activating any sort of indication of a completed event (or logging the date/time). I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • fsbfsb Posts: 24
    edited 2011-03-24 12:48
    Chris,
    seems like this is exactly what I need to do, but I am not experienced enough to figure out the code. What I am trying to do is have my camera take pictures at defined intervals (e.g every hour or once every 24 hrs). I would like to use the DS1302 as a count up clock, and at the appropriate intervals, trigger the picture. Can you give me an example of code on how to implement that with the DS1302?

    Thanks
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-24 13:20
    Try this , this is once every hour
    ' -----[ Program Code ]----------------------------------------------------
    Start:
    hrs  = $00
    mins = $00
    secs = $00
    GOSUB Set_Time
    DO
    LOW 0
    GOSUB Get_Time
    DEBUG HOME, HEX2 hrs, ":",   HEX2 mins, ":", HEX2 secs
    IF hrs = $00 AND mins = $59 AND secs = $00 THEN EXIT
    LOOP
    DEBUG CR, CR, "00:59:00 Elapsed"
    HIGH 0
    PAUSE 100
    GOTO start
    
    ' =========================================================================
    '
    '   File...... DS1302_Demo.bs2
    '   Purpose... Demonstrate The DS1302 Clock & RAM Functions
    '   Author.... Chris Savage -- Parallax, Inc.
    '   E-mail.... [EMAIL="csavage@parallax.com"]csavage@parallax.com[/EMAIL]
    '   Started...
    '   Updated... 10-09-2005
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
     
    ' -----[ Program Description ]---------------------------------------------
    ' This code demonstrates the use of the DS1302 RTC/RAM Chip with the BASIC
    ' Stamp 2 Microcontroller, making easy use of the 12 Hour & 24 Hour time
    ' modes, as well as accessing the RAM.  The sample code will use the DEBUG
    ' window for input and output for simplicity.  You can change the output
    ' to the device of your choice.  For example, using a Parallax Serial LCD
    ' requires very few changes to the code to use it for output.
    ' A modified version of this code was adapted to display the date and time
    ' on a Parallax Serial LCD Display (#27976), also with backlight (#27977)
    ' and on Parallax Professional Development Board (#28138, coming soon!)
    ' using a MAX7219 8-Digit Display Driver.
    ' This code will work on all BS2 Stamp Modules.
    '
    ' The Parallax 2 X 16 Serial LCD Displays are availble at the following
    ' links:
    ' [URL]http://www.parallax.com/detail.asp?product_id=27976[/URL]
    ' [URL]http://www.parallax.com/detail.asp?product_id=27977[/URL]
    '
    ' Pin Assignments
    ' 0   DS1302 DataIO
    ' 1   DS1302 Clock
    ' 2   DS1302 Chip Select
     
    ' -----[ Revision History ]------------------------------------------------
     
    ' -----[ I/O Definitions ]-------------------------------------------------
    DataIO          PIN     4               ' DS1302.6
    Clock           PIN     3               ' DS1302.7
    CS1302          PIN     5               ' DS1302.5
     
    ' -----[ Constants ]-------------------------------------------------------
    WrSecs          CON     $80             ' Write Seconds
    RdSecs          CON     $81             ' Read Seconds
    WrMins          CON     $82             ' Write Minutes
    RdMins          CON     $83             ' Read Minutes
    WrHrs           CON     $84             ' Write Hours
    RdHrs           CON     $85             ' Read Hours
    CWPr            CON     $8E             ' Write Protect Register
    WPr1            CON     $80             ' Set Write Protect
    WPr0            CON     $00             ' Clear Write Protect
    WrBurst         CON     $BE             ' Write Burst Of Data
    RdBurst         CON     $BF             ' Read Burst Of Data
    WrRam           CON     $C0             ' Write RAM Data
    RdRam           CON     $C1             ' Read RAM Data
     
    ' -----[ Variables ]-------------------------------------------------------
    index            VAR     Byte            ' Loop Counter
    reg               VAR     Byte            ' Read/Write Address
    ioByte           VAR     Byte            ' Data To/From DS1302
    secs             VAR     Byte            ' Seconds
    secs01          VAR     secs.LOWNIB
    secs10          VAR     secs.HIGHNIB
    mins             VAR     Byte            ' Minutes
    mins01          VAR     mins.LOWNIB
    mins10          VAR     mins.HIGHNIB
    hrs                VAR     Byte            ' Hours
    hrs01            VAR     hrs.LOWNIB
    hrs10            VAR     hrs.HIGHNIB
    date             VAR     Byte
    month           VAR     Byte
    day              VAR     Nib             ' Day
    year             VAR     Byte            ' Year
     
    work            VAR     Byte            ' Work Data
     
    ' -----[ Initialization ]--------------------------------------------------
    Init:
      reg = CWPr                            ' Initialize DS1302
      ioByte = WPr0                         ' Clear Write Protect
      GOSUB RTC_Out                         ' Send Command
     
    ' -----[ Program Code ]----------------------------------------------------
    Start:
    hrs   = $00
    mins = $00
    secs = $00
    GOSUB Set_Time
    DO
     LOW 0
      GOSUB Get_Time
     DEBUG HOME, HEX2 hrs, ":",  HEX2 mins, ":", HEX2 secs
      IF hrs = $00 AND mins = $59 AND secs = $00 THEN EXIT
    LOOP
    DEBUG CR, CR, "00:59:00 Elapsed"
    HIGH 0
    PAUSE 100
    GOTO start
    ' -----[ Subroutines ]-----------------------------------------------------
     
    RTC_Out:
      HIGH CS1302                           ' Select DS1302
      SHIFTOUT DataIO, Clock, LSBFIRST, [reg, ioByte]
      LOW CS1302                            ' Deselect DS1302
      RETURN
    RTC_In:
      HIGH CS1302                           ' Select DS1302
      SHIFTOUT DataIO, Clock, LSBFIRST, [reg]
      SHIFTIN DataIO, Clock, LSBPRE, [ioByte]
      LOW CS1302                            ' Deselect DS1302
      RETURN
    Set_Time:                               ' DS1302 Burst Write
      HIGH CS1302                           ' Select DS1302
      SHIFTOUT DataIO, Clock, LSBFIRST, [WrBurst]
      SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs,
        date, month, day, year, 0]
      LOW CS1302                            ' Deselect DS1302
      RETURN
    Get_Time:                               ' DS1302 Burst Read
      HIGH CS1302                           ' Select DS1302
      SHIFTOUT DataIO, Clock, LSBFIRST, [RdBurst]
      SHIFTIN DataIO, Clock, LSBPRE, [secs, mins, hrs, date, month, day, year]
      LOW CS1302                            ' Deselect DS1302
      RETURN
    

    I hope this helps
  • fsbfsb Posts: 24
    edited 2011-03-24 19:49
    Thanks, sam, that was really helpful to me.

    Here's what I did so far, using Chris's DS1302 template and ideas I gleaned from your response.

    I can adjust time for exposure (shutter speed of camera) and interval the photos are taken.

    The one last thing I would like to do is be able to stop the program after a certain period of time. Is there a way the program can count the number of iterations that the program completes from the start: portion through the goto start portion?


    fsb
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-25 05:52
    fsb wrote: »
    The one last thing I would like to do is be able to stop the program after a certain period of time. fsb

    Please explain in more detail what you want to do here

    Are you wanting to stop the " Timer "for some amount of time ?
    or
    Do you want to wait for some amount of time before the your code routine run again ?

    What you have to do is different in the way you write the code for the first question vers the second question
  • fsbfsb Posts: 24
    edited 2011-03-25 09:05
    Thanks sam.

    I am trying to stop the process after a certain amount of time. For instance, if I am taking a picture every hour, I would, for instance, want to stop after 48 hrs. This would be 48 iterations of the program. Not sure if BS2 can do something like that. May have to learn spin

    fsb
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-25 09:59
     
     ' -----[ Program Code ]----------------------------------------------------
    Start:
    mins = $00
    secs = $00
    GOSUB Set_Time
    DO
     LOW 0
      GOSUB Get_Time
     DEBUG HOME,HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
      IF mins = $59 AND secs = $00 THEN GOSUB routine1:
      IF  THEN EXIT
      ENDIF
    LOOP
     
    DEBUG CR, CR, "48:00:00 Elapsed"
     
     
     
    ' -----[ Subroutines ]-----------------------------------------------------
    routine1:
    DEBUG CR, CR, "00:59:00 Elapsed"
    HIGH 0
    PAUSE 100
    RETURN
     
    

    OR
    '
    ' -----[ Variables ]-------------------------------------------------------  
    '
    ' 
    iteration       VAR     Byte                 
    ' 
    ' 
    ' -----[ Program Code ]----------------------------------------------------
    Start:
    mins = $00
    secs = $00
    GOSUB Set_Time
     
    iteration_set:
    DO
    LOW 0
    GOSUB Get_Time
    DEBUG HOME,HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
    IF mins = $59 AND secs = $00 THEN GOSUB routine1:
    IF iteration = 48 THEN EXIT
    ENDIF
    LOOP
    'Your code here
    GOTO start
    ' -----[ Subroutines ]-----------------------------------------------------
     
    Routine1:
    DEBUG CR, CR, "00:59:00 Elapsed"
    HIGH 0
    iteration = iteration + 1
    PAUSE 100
    RETURN
    
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-25 10:21
    Start:
    iterations  = 00
    hrs  = $00
    mins = $00
    secs = $00
    GOSUB Set_Time 'starts timer from zero
        DO
           GOSUB Get_Time 'moniters timer
     
     
           DEBUG HOME, HEX2 mins, ":", HEX2 secs
     
           IF hrs = int_hr AND mins = int_min AND secs = int_sec THEN EXIT   
                                                        '  ^ < .......You need to change this to a subroutine not EXIT ......> 
        LOOP
            HIGH led
            PAUSE exp_time
            LOW led
          iterations =  iterations + 1                ' <............ For this line to work the way you want this to work
        GOTO start
     
    

    Something like this
    DO
    GOSUB Get_Time 'moniters timer
     
     
    DEBUG HOME, HEX2 mins, ":", HEX2 secs,DEC2 ? iterations
     
    IF hrs = int_hr AND mins = int_min AND secs = int_sec THEN GOSUD routine_1
    IF iterations = 48 THEN EXIT 
     
    LOOP
     
    GOTO start
     
     
    Routine_1: 
    HIGH led
    PAUSE exp_time
    LOW led
    iterations = iterations + 1
    RETURN
    

    I hope this helps
  • fsbfsb Posts: 24
    edited 2011-03-25 11:35
    Thanks

    I will try this

    fsb
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-25 14:45
    I do want you to know that could have the DS1302 ram save your setting when have the Basic Stamp powered down
    If you need help with this Please ask and I will help you as much as I can how ever I do not have one set to test the code right now but it is not hard to do

    This is some thing a little while ago so might give you idea what can be done

    For the DS1302 to save your data all you need a 1 MFD super cap to do this with this set up it would hold you data for more than a week

    Here is where you buy a MFD super cap

    http://www.allelectronics.com/make-a-store/item/CBC-17/1-FARAD-5.5-VOLT-SUPER-CAP//1.html
    WriteRam:                                        ' Routine to Save Data to a DS1302 Time Chip
                                    ' Send Command
      reg = $FE
      HIGH Timer
      SHIFTOUT DataIO, Clock,LSBFIRST, [reg]
      SHIFTOUT DataIO, Clock, LSBFIRST, [PDSecs.HIGHBYTE,PDSecs.LOWBYTE,SLSecs.HIGHBYTE,SLSecs.LOWBYTE,S,M,H,T]
      LOW Timer
      RETURN
      ReadRam :                           'Routine to Read Data from DS1302 Time Chip
      reg = $FF
      HIGH Timer
      SHIFTOUT DataIO, Clock, LSBFIRST, [reg]
      SHIFTIN DataIO, Clock, LSBPRE, [PDSecs.HIGHBYTE,PDSecs.LOWBYTE,SLSecs.HIGHBYTE,SLSecs.LOWBYTE,S,M,H,T]
      LOW Timer
      DEBUG HOME, "Product = ",DEC5  PD,"  Silo = ",DEC5 SL, CR, CR,
                               DEC5 ? PDSecs, DEC5 ? SLSecs,CR
     
      DEBUG   DEC2  T, " T ",CR, CR, " H = ", DEC2  H, " M = ",
              DEC2  M," S = ", DEC2  S
     
     
    Metered:
      GOSUB ReadRam
     
    PD = PDsecs + (PDsecs ** 13741)  ' Caculate Sand used from Sand Silo
    SL = SLsecs + (SLsecs ** 13741)
     
    T = (SL/500) * 1               ' T = Caculate percentage of product used
     
    'S = seconds                  ' Timer Counter Names are one letter because there is not enough-
    'M = Minutes                  ' -space to show all Data on LCD Display
    'H = Hours
     
    IF s = 60 THEN
     S = 00
     M = M + 1
     IF M = 60 THEN
     M = 00
     H = H + 1
     ENDIF
     ENDIF
     
    IF oldsecs <> secs THEN      ' This Routine Compars the oldsecs to secs
       oldsecs = secs            ' This Routine is so that Counter Update once a Seconds
       S = S + 1                 ' This for the Timer Counter for the amount of time  has that been used by the Sand Silo
       PDSecs = PDSecs + 1
       SLSecs = SLSecs + 1
    ENDIF
    GOSUB WriteRam
    RETURN
     
    
Sign In or Register to comment.