PDA

View Full Version : DS1302 day of year function

lfreeze
10-23-2009, 07:01 PM
·
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.
·
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
·

Beau Schwabe
10-23-2009, 09:14 PM
lfreeze,

"(10 + 31) is 47"

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 (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

lfreeze
10-23-2009, 11:41 PM
Thanks for your help Beau, I modified the program and it worked perfectly. A working version is attached.

Larry

Tracy Allen
10-24-2009, 01:48 AM
Hi 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 (http://www.emesystems.com/BS2math4.htm#JulianDate) is an approach that I came up with that does it in one funny formula:

' enter with month and year in BCD
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

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

lfreeze
10-24-2009, 03:31 AM
Tracy,

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
10-24-2009, 05:52 AM
Hi Larry, I'm not sure quite where it went wrong. That snippet does not compile as written because month and year are not declared. Here is a rewrite that gives dayofyear=296 when you enter 10/23/09:

' {\$STAMP BS2pe}
' {\$PBASIC 2.5}
dayofyear VAR Word
mm VAR Byte
yy VAR Byte
dd VAR Byte
'_________________________________________________ ___________________________
top:
DEBUG "enter mm/dd/yy:"
DEBUGIN DEC mm,DEC dd, DEC2 yy
dayofyear=mm-1*30+(mm/9+mm/2)-(mm MAX 3/3*(yy//4 MAX 1 +1))+ dd
DEBUG CR," DayOfYear= ", DEC dayofyear," mm/dd/yy= ",DEC2 mm,"/",DEC2 dd,"/", DEC2 yy,CR,CR

PAUSE 1000
GOTO top

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

Post Edited (Tracy Allen) : 10/23/2009 10:57:37 PM GMT

sam_sam_sam
10-24-2009, 10:23 PM
Tracy Allen

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·http://forums.parallax.com/images/smilies/idea.gif·that you may have and all of your time finding them http://forums.parallax.com/images/smilies/smile.gif

·
·
·
·
Sam

Tracy Allen
10-24-2009, 11:25 PM
Hi sam_sam_sam,
I've done that reverse lookup with a LOOKDOWN for the month boundaries:

' {\$STAMP BS2pe}
' {\$PBASIC 2.5}
mm var byte ' month, (not BCD)
dd VAR Byte ' day, (not BCD)
yy VAR Byte ' year
jd VAR Word ' julian date in year, 0->365 or 366
ly VAR Bit ' 1 if leap year, 0 if not
xb VAR bit ' helper bit

DO
DEBUG "enter day of year, 1-365:"
DEBUGIN DEC3 jd
yy = 9 ' 2009 assumed
ly=0 ' 2009 is not a leap year
xb=jd MAX 60 / 60 * ly ' helper for leap year
LOOKDOWN jd-xb,<=[31,59,90,120,151,181,212,243,273,304,334,365],mm
mm=mm+1
dd=jd-(mm-1*30+(mm/9+mm/2)-(mm max 3/3*(2 - ly)))
DEBUG CR," DayOfYear= ", DEC jd," mm/dd= ",DEC2 mm,"/",DEC2 dd,CR,CR
LOOP

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

Post Edited (Tracy Allen) : 10/25/2009 3:45:32 PM GMT

sam_sam_sam
10-25-2009, 06:45 AM
Tracy Allen

Thanks for sharing that code with me

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·http://forums.parallax.com/images/smilies/idea.gif·that you may have and all of your time finding them http://forums.parallax.com/images/smilies/smile.gif

·
·
·
·
Sam