32 bit seconds math (how do I include the upper 16 bits?)


Results 1 to 17 of 17

Threaded View

  1. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 32 bit seconds math (how do I include the upper 16 bits?)

    Hi Dwight,
    Try the following:
    Code:
    ooDays VAR WORD
    i VAR WORD
    Temp VAR WORD
    
    ' This uses DIV32 by preloading the system variable it uses with
    ' the value we want to divide. To find out how many days has passed
    ' we divide the number of seconds with 86400 (number of seconds in a day).
    ' However, DIV32 only supports 16 bit divisor so we need to do the
    ' division in two steps, 21600 * 4 = 86400.
    
    R0 = AB			' High word of seconds into system var
    R2 = CD			' Low word of seconds into system var
    ooDays = DIV32 21600	' Divide by 86400
    ooDays = ooDays / 4
    
    ' Now we need to subtract 86400 seconds from the running time
    ' one time for each day that has passed. Since we're working with
    ' 16-bit words we need to this sort of "manually".
    ' 65536 + 20864 = 86400
    
    For i = 1 to ooDays
      Temp = CD
      CD = CD - 20864
    
      IF Temp < CD THEN	' Did we underflow the low word?
        AB = AB - 1		' If so, decrement high word
      ENDIF
    
      AB = AB - 1            ' Subtract 65536 
    NEXT
    
    ' At this point ABCD contains anything from 0 to 86399 and we need to
    ' figure out how many "full hours" is in it. Because 86399 is more than
    ' what fits in a 16-bit word we're using DIV32 trick to divide the
    ' number of seconds by 3600 (number of seconds in an hour). 
    
    R0 = AB			' High word of what's left in seconds into system var
    R2 = CD			' Low word of what's left in seconds into system var
    ooHH = DIV32 3600	' Divide by the number of seconds in an hour
    
    ' Subtract 3600 from the running time, one time for each hour.
    For i = 1 to ooHH
      Temp = CD
      CD = CD - 3600
     
      IF Temp < CD THEN	' Did we underflow the low word?
        AB = AB - 1		' Subtract one from high word.
      ENDIF
    
    ' Now, we're down to minutes and there can be no more than 3599 seconds left
    ' so we can easily handle it with just the low word and normal math
    ooMM = CD / 60
    
    ' And finally we can get the seconds by getting the reminder.
    ooSS = CD // 60
    It's based on a piece of code I originally wrote for doing SNTP with W5100 chip. SNTP basically gives you the numner of seconds passed since 1900-01-01 00:00:00 so I stripped out the year, leapyear, and month stuff and adopted it to your variables. I have not verified the above to be working.

    /Henrik.
    Last edited by HenrikOlsson; - 25th January 2015 at 17:55.

Similar Threads

  1. 32 bit math
    By fnovau in forum General
    Replies: 4
    Last Post: - 12th February 2008, 23:55
  2. Averaging 16 bit values without using 32 bit math
    By sirvo in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th October 2007, 22:18
  3. PIC18Fxx math 24 & 32 bit
    By ronsimpson in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 2nd December 2006, 12:52
  4. 32 bit math
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 28th August 2006, 13:34
  5. PBP 16-bit ADC result math
    By sonic in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2005, 14:21

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts