#picaxe 08m2
'#no_data
symbol Tmpb3=b11
symbol Tmpb1=b12
symbol Tmpb2=b13
symbol TempW1=W7
symbol TempW2=W8
symbol LSBpointer=b16
symbol MSBpointer=b17
symbol M1Low=W10
symbol M1High=W11
symbol M2Low=W12
symbol M2High=W13
symbol hours=b10
symbol mins=b9
symbol secs=b8
symbol year=b7
symbol month=b6
symbol day=b5
symbol oldsecs=b4
symbol Tmpb4=b3
symbol Tmpb5=b2
symbol Tmpb6=b1
symbol SiteLongDeg=149 'Degrees Longitude of location of clock (75 - 323 degrees East)
symbol SiteLongMin=5 'Minutes Longitude of location of clock (0-60)
setfreq m8
serout c.4,N2400_8,("?f") 'Clear Display
do
do
Hi2CSETUP i2cmaster,%11010000,i2cslow_8,i2cbyte 'To communicate with RTC
readi2c 0,(secs,mins,hours,day,day,month,year) 'Get current time & date
Tmpb1=secs&$0f
secs=secs/16*10+Tmpb1
Tmpb1=mins&$0f
mins=mins/16*10+Tmpb1
Tmpb1=hours&$0f
hours=hours/16*10+Tmpb1
Tmpb1=day&$0f
day=day/16*10+Tmpb1
Tmpb1=month&$0f
month=month/16*10+Tmpb1
Tmpb1=year&$0f
year=year/16*10+Tmpb1
loop until secs<>oldsecs
oldsecs=secs
bintoascii day,Tmpb1,Tmpb1,Tmpb2
bintoascii month,Tmpb3,Tmpb3,Tmpb4
bintoascii year,Tmpb5,Tmpb5,Tmpb6
sertxd (Tmpb1,Tmpb2,"/",Tmpb3,Tmpb4,"/",Tmpb5,Tmpb6,", ")
serout c.4,N2400_8,("?y0?x09(",Tmpb1,Tmpb2,"/",Tmpb3,Tmpb4,"/",Tmpb5,Tmpb6,")")
bintoascii hours,Tmpb1,Tmpb1,Tmpb2
bintoascii mins,Tmpb3,Tmpb3,Tmpb4
bintoascii secs,Tmpb5,Tmpb5,Tmpb6
sertxd (Tmpb1,Tmpb2,":",Tmpb3,Tmpb4,":",Tmpb5,Tmpb6,cr,lf)
serout c.4,N2400_8,("?x00",Tmpb1,Tmpb2,":",Tmpb3,Tmpb4,":",Tmpb5,Tmpb6)
For bptr=28 to 35 'Zero Result
@bptr=0
next bptr
call dayint 'Calculate the number of days since 1/1/15 & save in M1Low
M1High=0
M2Low=20864 '86400
M2High=1
call Mul32by32 'Convert days to seconds
'call prt64
M1Low=hours*3600 'Determine number of seconds to current time today
M1High=hours**3600
TempW1=mins*60+secs+M1Low
if TempW1<M1Low then: inc M1High: endif
M1Low=TempW1
call AddM1 'Adds M1High:M1Low to 8 byte result
'call prt64
bptr=27 'High byte of M2
for Tmpb1=31 to 28 step -1 'Move bottom 4 bytes of Rusult down to M2
peek Tmpb1,Tmpb2
@bptrdec=Tmpb2
next Tmpb1
For bptr=28 to 35 'Zero Result
@bptr=0
next bptr
M1High=$B3 '0.00273790932
M1Low=$6E7F
call Mul32by32 'Multiply seconds since 1/1/15 by 0.00273790932
bptr=28 'Move top 4 bytes of Result down over bottom 4 bytes - effectively divide by 2^32
for Tmpb1=32 to 35
peek Tmpb1,Tmpb2
@bptrinc=Tmpb2
next Tmpb1
do: @Bptrinc=0:loop until bptr>35 'Zero the top 4 bytes
M1Low=SiteLongDeg-75*240 'Site Longitude East can range from 75 to 322 degrees
M1Low=SiteLongMin*4+M1Low 'Add in minutes of Longitude (4 seconds/minute)
M1Low=M1Low+5981 '6:39:41 - 5:00:00 (75 degrees of longitude)
M1High=0
call AddM1 'Add in initial hours @ 1/1/2015 + Longitude offset
M1Low=hours*3600 'Determine number of seconds to current time today
M1High=hours**3600
TempW1=mins*60+secs+M1Low
if TempW1<M1Low then: inc M1High: endif
M1Low=TempW1
call AddM1 'Adds in the current time
bptr=23 'High byte of M1
for Tmpb1=31 to 28 step -1 'Move bottom 4 bytes of Rusult down to M1
peek Tmpb1,Tmpb2
@bptrdec=Tmpb2
next Tmpb1
lp: 'Reduce seconds to less than a day
if M1High>0 then
If M1High=1 and M1Low<$5180 then 'if <86400
else
M1Low=M1Low-$5180
if M1Low>$AE7F Then: dec M1High: endif
dec M1High
goto lp
endif
endif
Hours=0
if M1Low>43200 or M1High>0 then
M1Low=M1Low-43200
if M1Low>$7fff then: dec M1High: endif
Hours=hours+12
endif
secs=M1Low//60
M1Low=M1Low/60
mins=M1Low//60
Hours=M1Low/60+hours
bintoascii hours,Tmpb1,Tmpb1,Tmpb2
bintoascii mins,Tmpb3,Tmpb3,Tmpb4
bintoascii secs,Tmpb5,Tmpb5,Tmpb6
sertxd (Tmpb1,Tmpb2,":",Tmpb3,Tmpb4,":",Tmpb5,Tmpb6,cr,lf,cr,lf)
serout c.4,N2400_8,("?y1?x00",Tmpb1,Tmpb2,":",Tmpb3,Tmpb4,":",Tmpb5,Tmpb6," Sidereal")
'call prt64
nap 4 'sleep for 288 msecs
loop
stop
end
'-------------------------------------------------------------
Mul32by32:
'Multiply the 32 bit munmers stored in M1 & M2 & store answer in b28 - b35. Needs to be cleared initially.
'bptr=28
For Tmpb1=20 to 23
bptr=Tmpb1+8 'needs to progress along answer location
For Tmpb2=24 to 27
peek tmpb1,b16
peek tmpb2,b17
TempW1=b16*b17
TempW2=@bptr+b14
tmpb3=bptr
do
@bptrinc=b16
TempW2=TempW2/256
TempW2=b16+@bptr
loop while b17>0
@bptrinc=b16
bptr=tmpb3+1
TempW2=@bptr+b15
tmpb3=bptr
do
@bptrinc=b16
TempW2=TempW2/256
TempW2=b16+@bptr
loop while b17>0
@bptrinc=b16
bptr=tmpb3
next tmpb2
next tmpb1
return
'-------------------------------------------------------------
Dayint:
'Calculates the number of days since 1/1/15 & saves result in M1Low
M1Low=year-15
If M1Low>0 then
M1Low=0: dec year
For Tmpb1=15 to year
M1Low=M1Low+365
Tmpb2=Tmpb1/4*4
if Tmpb2=Tmpb1 then
inc M1Low
endif
next Tmpb1
inc year
endif
For Tmpb1=1 to month
if Tmpb1=month then
M1Low=M1Low+day
else
read Tmpb1,Tmpb2
M1Low=M1Low+Tmpb2
if Tmpb1=2 then
Tmpb2=year/4*4
if Tmpb1=Tmpb2 then
inc M1Low
endif
endif
endif
next Tmpb1
dec M1Low 'Starts from 1/1/15 not 0/1/15
'sertxd ("M1Low=",#M1Low,cr,lf)
return
'-------------------------------------------------------------
AddM1:
'Adds the 4 byte word M1 to the 8 byte result(bytes 28 to 35)
For Tmpb3=20 to 23
bptr=Tmpb3+8 'needs to progress along answer location
peek tmpb3,Tmpb2
do
W6=@bptr+Tmpb2
@bptrinc=Tmpb1
loop while Tmpb2>0 'Tmpb2 is high byte of w6 - Tmp1 is low
next Tmpb3
return
'-------------------------------------------------------------
Prt64:
'Print out the 64 bit number starting at bptr=28 in decimal but destroys it in the process (debug only)
bptr=36 'set bptr to bottom of the remainder stack
MSBpointer=35 'MSB of long 64 bit word
LSBpointer=35-8+1 'LSB of 64 bit word
do
b15=0 'set initial remainder to zero
for Tmpb2=MSBpointer to LSBpointer step-1 'step through each digit of the long word
peek Tmpb2,b14 'get the digit
Tmpb1=TempW1/10 'calculate the quotient
poke Tmpb2,Tmpb1 'write quotient back into long word
b15=TempW1//10 'carry the remainder onto the next digit
next Tmpb2
@bptrinc=b15 'push remainder of long word/10 onto stack
peek MSBpointer,Tmpb1 'get the MSB of the remaining long word
if Tmpb1=0 then: MSBpointer=MSBpointer-1:endif 'move MSB pointer down if current digit is zero
loop until MSBpointer<LSBpointer 'Finish when the original long word is reduced to zero
dec bptr
do 'print out the result
Tmpb1=@bptrdec
if Tmpb1<10 then
sertxd(#Tmpb1)
else
Tmpb1="A"-10+Tmpb1
sertxd(Tmpb1)
endif
loop until bptr<36
sertxd (cr,lf)
Return
'-------------------------------------------------------------
eeprom 0,(0,31,28,31,30,31,30,31,31,30,31,30,31)