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.
Bookmarks