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


Closed Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427

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

    Hey Group,

    I am using a Dallas Semiconductor DS2417 RealTimeClock
    This little OneWire device simply starts counting seconds from where ever you tell it to start from (your date reference)
    and gives the count back to you in a 4 byte 32bit register
    With 32 bits the clock can count up for 136 Years before rolling over.

    I need some help figuring out the math to convert the 32bit seconds to HH:MM:SS

    My code below is able to do the conversion correctly on the first 16bit ok but fails to take into account the second 16 bits of the 32 bit data.
    so after 18 hrs 12 min and 15 sec (65535 or $FFFF) it rolls over to zero.

    I need to know how to deal with and include the upper 16 bits of the 32 bit data.

    My code takes the lower two bytes (C&D) and combines them into a Word (CD)
    and then does the math to convert to HH:MM:SS

    But I am at a loss as to what to do with the upper 16 bits.

    For now I am not interested in the DAYS, I am only wanting to keep track of a rolling 24 hour clock.
    So I expect to just devide the seconds down and discard multiples of 24 hrs.


    Code:
    '=============================================================
    '           Read OneWire Clock
    '=============================================================
    GetOWtime:
        owout OWTPin,%000,[$cc,$66]
    	owin  OWTPin,%010,[owctrl,ByteD,ByteC,ByteB,ByteA]    'ByteD=LSB ByteA=MSB
    
    	AB.byte1 = ByteA   'MSB
    	AB.byte0 = byteB
    	CD.byte1 = bytec
    	CD.byte0 = byteD   'LSB
    return      'with time 
    
    
    '=============================================================
    ' convert the one wire seconds count to HH:MM:SS
    '=============================================================
    HMS:
        owHH= CD/3600
        owMM= (CD/60)-(owHH*60)
        owSS= CD//60
    return
    I need help in figuring out what to do when the word "AB" starts counting above zero (which happens after the 18:12:15 point is reached (assuming I start from zero)

    My plan is to start the clock at zero and store the actaul time I started the clock in a variable and add/subtract the stored time variable from the incrementing count to get the current real time.

    I know there are other clocks that do all this for you (like the DS1307) but that one is I2C and I specifically need a One Wire solution.

    thanks for any help or guidance you can give.

    dwight
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  2. #2
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

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

    Dwight,

    You don't mention what PIC MCU you are using nor which version of Picbasic.

    If your PIC and version of Picbasic supports Long variable types, that would be the simplest way to go. You of course would need to account for bit32 since PBPL uses Signed Long variable types and the DS2417 uses an Unsigned 32bit value.

    Regards
    Regards,
    TABSoft

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    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.

  4. #4
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

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

    @TABSoft

    Sorry, I should have posted that info...

    Using PBP 3
    the PIC is 16F690 (but I also want to be able use the newer 16F1828)

    I had thought about using Long's but I've never used PBPL and what little info is in the manual is very vague.
    What does it take to use a Long?? and invoke PBPL?

    I haven't done much searching on PBPL yet. I'm going to give the code that Henrik (thanks!!) posted and see what it does for me.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  5. #5
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

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

    Wow Henrik!!

    You write amazingly good "unverified" code!
    You did miss one "NEXT" down at the end.
    I had to change your ooHH, etc. variables to match mine (owHH)

    But it is working beautifully!

    Code:
    '=============================================================
    ' convert the one wire seconds count to HH:MM:SS
    '=============================================================
    HMS:
    
    owDays 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
    owDays = DIV32 21600	' Divide by 86400
    owDays = owDays / 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 owDays
      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
    owHH = 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 owHH
      Temp = CD
      CD = CD - 3600
     
      IF Temp < CD THEN	' Did we underflow the low word?
        AB = AB - 1		' Subtract one from high word.
      ENDIF
    next
    ' 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
    owMM = CD / 60
    
    ' And finally we can get the seconds by getting the reminder.
    owSS = CD // 60
    
    return
    Thank you so much.
    I preloaded the clock with 23:59:00 and it rolled over to 00:00:00 as expected!

    Now to go digest and try and understand what you actually told my little micro-conntroller to do.

    after all that is what we are doing when we write code "telling these little pieces of silicone" how and when to do something

    thanks again!

    I actually have two clocks and a DS18b20 on my little breadboard...
    the I2C DS1307 and the onewire DS2417 (boy its a liittle guy) you can see it just below the LCD and on the PCB in the breadboard.

    You can see the time from the DS1307 (blue PCB) on the top line
    you can see the 4 bytes of seconds count from the DS2417 on the 3rd line
    on the bottom line is the result of you conversion code.

    Name:  rtc.jpg
Views: 816
Size:  283.6 KB



    Thanks
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

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

    Hi Dwight,
    That's excellent news, glad I could help!
    Sorry for the missing NEXT, I should have at least compiled it before posting but I was really short on time.

    Regarding using LONGs. If you're using Microcode Studio as your IDE there's a checkbox in the View -> Compile and program options saying Use Compiler Long Words (18 series MCU Only). Tick that and you'll be compiling with LONGs enables. As the text says you have to use an 18F part so the 16F690 or 16F1828 will not work.

    /Henrik.

  7. #7
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

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

    Sorry for the missing NEXT, I should have at least compiled it before posting but I was really short on time.
    Henrik,
    Please don't feel bad. I could only hope to quickly analyze someone elses code/project and toss out such a quick and good answer.

    The value of forums like this is the willingness of folks like you, TABsoft and others to donate their time and talent to help others out.

    The fact that you missed a "next" is trivial. The fact that you could understand my need and share a possible solution is the key.

    I had looked at the DIV32 function in the PBP manual but could not understand how to apply it in my situation. The manual led me to believe that I first had to multiply two numbers. Which I did not need to do (or is that actually what one is doing by combining a LowWord and a HighWord??). ((don't think so... because FF x FF =FE01 not FFFF))

    I did not know that one could pre-load the system variables manually (no mention of that fact in the manual) or which system variables would be involved (R0, R2)
    Last edited by Heckler; - 27th January 2015 at 03:56.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

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

    Hi Dwight,
    No, that sort of info is not in the manual. I don't remember from where I got it but I'd be surprised if it wasn't from one of Darrels post.

    Anyway, yes, normally what you do is multiply two 16bit number which results in a 32bit result then, imediately after, you execute the DIV32 which will divide that 32bit result by the 16bit divisor. The trick is to know that when you do the 16*16bit multiplication the result of that ends up in system variables R0 and R2. Once you know that there's nothing stopping you from manually loading those registers, the DIV32 function will not know the difference.

    If you have a total of 1,125,000 seconds in your ABCD variable, looking at that in hex: 0011 2A88 what goes in R0 is 11h (17) and what goes on R2 is 2A88 (10888).
    17*65536+10888=1,125,000.

    I think there might be a possible issue with the code as posted. When the number of seconds passed is less than 86400 (one day) then owDays will be 0 and the FOR-NEXT loop subtracting 86400 seconds per day will execute 65535 times. I think the result will still be correct since it'll just wrap around but it may take a fair bit of time..... You might want to enclose that FOR/NEXT loop in an IF/THEN block checking if owDays > 0.

    /Henrik.
    Last edited by HenrikOlsson; - 27th January 2015 at 06:31.

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