Midpoint of two time intervals

Discussion in 'Embedded Systems and Microcontrollers' started by RG23, Dec 31, 2012.

  1. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    How to find the Midpoint of two time intervals?

    For example if first time is 9:00:01 and second time is 10:00:02 then it should calculate the midpoint accurately
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Add them together then divide by two.
     
  3. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    HRS: 13
    MINS:48
    SECS:54

    HRS1:13
    MINS1:17
    SECS1:39

    Then the midpoint as per excel sheet comes out to be 13:33:16

    When you said add them and divide by two how will it work in this case?
     
    Last edited: Dec 31, 2012
  4. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    But you must use sexagesimal (base 60) numbers when you do this.
    Code ( (Unknown Language)):
    1.  
    2. Example
    3.   10:02:00
    4.  - 8:56:45
    5. -----------
    6.    1:05:15
    7.  
    8.   10:07:22
    9.  + 8:56:45
    10. -----------
    11.    19:04:07
    12.  
    13. 19:04:07 / 2 = 09:28:22.5 = 09:32:03:30
    14.  
    15. I've expressed the half second as 30 jiffies
    16. to be consistent with the sexagesimal representation.
    17.  
    http://en.wikipedia.org/wiki/Jiffy_(time)
    see the electronics sub-heading
     
    Last edited: Dec 31, 2012
  5. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    Your answer is rounded up. Excel knows how to do this. It converts date and time fields to an internal representation, does the arithmetic, and then converts back.

    By hand I get 13:33:16:30 where the last 30 jiffies is the half second.

    Code ( (Unknown Language)):
    1.  
    2. Step 1:  Add the two times together using base 60 arrithmetic
    3.    13:48:54
    4.  + 13:17:39
    5. ------------
    6.    26:65:93  sums should be modulo 60 with a carry
    7.  = 27:06:33
    8.  
    9. Step 2: Divide by 2
    10. 27/2 : 06/2 : 33/2 = 13.5:03:16.5
    11.  
    12. Step 3: Convert fractions to the next lower group
    13.  
    14. 13.5 hrs   = 13:30:00:00
    15. 03 mins    = 00:03:00:00
    16. 16.5 secs  = 00:00:16:30
    17.  
    18. Step 4: Add 'em up
    19.  
    20. Result = 13:33:16:30
    21.  
    Sorry for the delay I took my wife on a date this afternoon
     
    Last edited: Dec 31, 2012
  6. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    By hand I get 13:33:16:30 where the last 30 jiffies is the half second

    How did you do the calculation?

    I am not clear
     
  7. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    @papabravo

    I am not clear with the example you posted
     
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Alternatively (if you don't want to do base 60 math);

    1. convert both numbers from HHMMSS to seconds (some C compilers have a fuinction)
    2. get the difference between the two numbers in seconds
    3. halve the difference
    4. add it to the low number
    5. convert the result back to HHMMSS (if required)
     
  9. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I am using DS1307 RTC where the contents of time registers are in BCD format

    So the numbers that get stored in the SECS and SECS5 registers are actually hex
    When I try to perform arithmetic operations on these registers I am getting wrong results

    Actually I want to treat those hex numbers as decimal to get desired results

    Part of the computation for seconds
    movlw h'59'
    movwf SECS5

    movlw h'58'
    movwf SECS

    movf SECS5,0
    addwf SECS,1
    movf SECS,0
    movwf SECS7

    movlw h'60'
    subwf SECS7,0
    btfsc CARRY
    incf MINS,1
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    Reread the example in the previous post since I made an edit to it rather than creating a new post. The general rule for adding seconds and minutes is that whenever you get a number greater than 60, you generate a carry of 1 minute or 1 hour as appropriate and reduce the sum by 60. For example if you add 50 seconds to 43 seconds and get 93 seconds that is the same as (93-60=33 seconds) plus a carry of 1 minute. A similar operation happens with borrow when you borrow from the hours column that is equivalent to 60 minutes.

    I think you're just going to have to stare at it until you see it.
     
  11. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    i did understand your example papabravo

    thank you for that

    But now I am having an issue

    I am using DS1307 wherein the contents of time registers are in BCD format

    So the data is actually in the hex format and somehow I want to interpret those hex values as decimal to perform further computations

    I couldn't figure it out
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    OK. Yeah BCD and Hex look exactly the same on paper because the representation of the digits is identical. The problem comes in when you use a binary ALU (Arithmetic Logic Unit). In the datasheet look for a "half carry" flag OR a "decimal adjust accumulator" instruction. These things will help you do BCD Arithmetic on a binary machine.

    I found the following link which you might find helpful
    http://mathforum.org/library/drmath/view/51901.html

    BTW - what processor are you using? If I knew I could give more targeted advice.
     
    Last edited: Jan 3, 2013
  13. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    Just be prepared to do something intelligent in the case where the earlier time is before midnight, and the later time is after it.
     
  14. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I am using PIC18f67J90
     
  15. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    Look for the bit in the STATUS Register labeled `DC' for Digit Carry
     
Loading...