DS1302 day of year function
lfreeze
Posts: 174
·
I need help with something I thought would be simple. I want to program the Stamp
And a DS1302 To calculate and display the day of the year, ·
Example:
················ January 1······· =·· day of year· 1
················ December 31· =· day· of year· 365
·
·I have connected the Stamp to a DS1302 and I am getting accurate Readings for the basic DS1302 functions.· I built a Select case statement ( below)· I calculated the number of days to the first of each month for the year in my select case statement, I then· added the date to each months total· example:
·
Month = February, Date =10 , day of year should equal 41
·
The result I get for the day of year using my select case statement· (10 + 31)· is· 47
·
I tried many various hex, dec manipulations (DIG) function, but couldn’t get it to work correctly.· Can anyone offer a solution to this?
The full program I am using is attached.
Thanks in advance for your help.
·
Larry
·
dayofyear···· VAR Word
·
SELECT month
·CASE=$01·············· ·····'January
·dayofyear=date
·CASE=$02········ ··········'February
·dayofyear=date· +31··· ·'$1f
·CASE =$03·············· ···'March
·dayofyear=date· +59··· '$3b
·CASE =$04·············· ··'April
·dayofyear=date· +90··· '$5a
·CASE =$05·············· ···'May
·dayofyear=date· +120·· '$79
·CASE =$06·············· ···'June
·dayofyear=date· +151·· '$97
·CASE =$07············· ····'July
·dayofyear=date· +181·· '$b5
·CASE =$08·············· ··'August
·dayofyear=date· +212·· '$d4
·CASE =$09·············· ··'September
·dayofyear=date· +243·· '$f3
·CASE =$10·············· 'October
·dayofyear=date· +273·· '$111
·CASE =$11·············· 'November
·dayofyear=date· +304·· '$130
·CASE =$12·············· 'December
·dayofyear=date· +334·· '$14e
ENDSELECT
·
I need help with something I thought would be simple. I want to program the Stamp
And a DS1302 To calculate and display the day of the year, ·
Example:
················ January 1······· =·· day of year· 1
················ December 31· =· day· of year· 365
·
·I have connected the Stamp to a DS1302 and I am getting accurate Readings for the basic DS1302 functions.· I built a Select case statement ( below)· I calculated the number of days to the first of each month for the year in my select case statement, I then· added the date to each months total· example:
·
Month = February, Date =10 , day of year should equal 41
·
The result I get for the day of year using my select case statement· (10 + 31)· is· 47
·
I tried many various hex, dec manipulations (DIG) function, but couldn’t get it to work correctly.· Can anyone offer a solution to this?
The full program I am using is attached.
Thanks in advance for your help.
·
Larry
·
dayofyear···· VAR Word
·
SELECT month
·CASE=$01·············· ·····'January
·dayofyear=date
·CASE=$02········ ··········'February
·dayofyear=date· +31··· ·'$1f
·CASE =$03·············· ···'March
·dayofyear=date· +59··· '$3b
·CASE =$04·············· ··'April
·dayofyear=date· +90··· '$5a
·CASE =$05·············· ···'May
·dayofyear=date· +120·· '$79
·CASE =$06·············· ···'June
·dayofyear=date· +151·· '$97
·CASE =$07············· ····'July
·dayofyear=date· +181·· '$b5
·CASE =$08·············· ··'August
·dayofyear=date· +212·· '$d4
·CASE =$09·············· ··'September
·dayofyear=date· +243·· '$f3
·CASE =$10·············· 'October
·dayofyear=date· +273·· '$111
·CASE =$11·············· 'November
·dayofyear=date· +304·· '$130
·CASE =$12·············· 'December
·dayofyear=date· +334·· '$14e
ENDSELECT
·
Comments
"(10 + 31) is 47"
Your adding a HEX value to a Decimal value.
10 Hex = 16 Decimal .... 16 + 31 = 47
Since the 'Date' is stored in what's called BCD (Binary Coded Decimal) you need to convert it to Decimal first before adding it to your offset.
Try:
dayofyear = (Date.NIB1 * 10 + Date.NIB0) + 31
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Larry
That is a good clean approach. I'd recommend that the program make one calculation of
dayofyear = (Date.NIB1 * 10 + Date.NIB0)
then do the CASE's to add the constant factor to that. Just to save program space used by repeating the NIB calculation. Don't forget leap years!
Here is an approach that I came up with that does it in one funny formula:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
I tried the formula you suggested, and I must be missing something. when I run the formula using
todays date, I get a "day of year result" of 273. the calendar tells me I should be getting 296.· I like
your suggestion of streamling the code and will proceed with it. Thanks for your response, and please
tell me what I've got wrong with the formula.·· Here is the code I am using.
Larry
dayofyear··· VAR Word
mm··········· ·VAR Byte
yy············ ·VAR Byte
dd············ ·VAR Byte
mm=10
dd =23
yy =09
'____________________________________________________________________________
top:
GOSUB TELLTIME··· 'GETS THE CURRENT DATE AND TIME
MM=month.NIB1*10+month.NIB0
YY=year.NIB1*10+year.NIB0
dayofyear=MM-1*30+(MM/9+MM/2)-(MM MAX 3/3*(YY//4 MAX 1 +1))+ DD
DEBUG "···· DAYOFYEAR = ", DEC3 dayofyear,"··· MONTH =· ",HEX2 month,"··· DATE = ",HEX2 date,CR,CR
PAUSE 1000
goto top
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 10/23/2009 10:57:37 PM GMT
dayofyear=mm-1*30+(mm/9+mm/2)-(mm·MAX·3/3*(yy//4·MAX·1·+1))+·dd
How would you work it back the other way
Take 252 day which would be 09/09/09
Can this be done
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
I've done that reverse lookup with a LOOKDOWN for the month boundaries:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 10/25/2009 3:45:32 PM GMT
Thanks for sharing that code with me
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam