PDA

View Full Version : Melanies RTC code has broken



keithdoxey
- 6th October 2006, 00:57
Hi All,

This is aimed at anyone using Melanies RTC code in a project particularly if you use the Calculate Day of Week & Save part of the code which includes the following warning....

Melanie's fudge for calculating Days of Week using PBP's integer math...
by the time someone's clock displays the wrong Day of Week, I'll hopefully have long retired and be past caring...
I wouldn't however go stick this routine in a Nuclear Power Station to purge the reactor every Monday morning without checking how far it will work before the integer math overflows...
In my routine RTCWDay=1 is a Tuesday (the start of my week) and continues sequentially until RTCWDay=7 which is a Monday

It seems the end may have come earlier than anticipated :(

Melanies original code is as follows...


CounterA=SetYear+4
CounterB=SetMonth
If SetMonth<3 then
CounterA=CounterA-1
CounterB=CounterB+12
endif
CounterD=(SetDay+(153*CounterB-457)/5+365*CounterA+CounterA/4-CounterA/100+CounterA/400+2) MOD 7
RTCWDay=CounterD+1

My 9 year old daughter took great delight in informing me that the LCD on my current project was displaying
FRI 04 OCT 2006

I knew the code had been working fine so I set about investigating when it went wrong and was using http://www.timeanddate.com/calendar/ as a reference calendar. Listed below is the point where it breaks this year. The problem only occurs when actually setting the RTC so provided you set it early enough in the year you will be fine !!!

SEPTEMBER 2006
03 SUN
04 MON
05 TUE
06 WED
07 THU
08 FRI
09 SAT
10 SUN SAT -1 Day
11 MON SUN -1 Day
12 TUE MON -1 Day
13 WED TUE -1 Day
14 THU WED -1 Day
15 FRI THU -1 Day
16 SAT FRI -1 Day
17 SUN SAT -1 Day
18 MON SUN -1 Day
19 TUE MON -1 Day
20 WED MON -2 Days
21 THU TUE -2 Days
22 FRI WED -2 Days
23 SAT THU -2 Days
24 SUN FRI -2 Days
25 MON SAT -2 Days
26 TUE SUN -2 Days
27 WED MON -2 Days
28 THU TUE -2 Days
29 FRI WED -2 Days
30 SAT WED -3 Days

OCTOBER 2006
01 SUN TUE +2 Days
02 MON WED +2 Days
03 TUE THU +2 Days
04 WED FRI +2 Days
05 THU SAT +2 Days
06 FRI SUN +2 Days

I tried the first of November and December this year and they also came out wrong.

I was surprised find that 01 JAN 2007 came out with the correct day so I tried the first of the month for each month of 2007 and 2008 and in each case OCT, NOV, DEC were wrong. 01 SEP 2007 came out correctly but several random dates tried for Aug 2007 were broken.

I tried "01 JAN 20xx" which failed when I got to 2011

Is this something that is fixable by breaking the calculation into smaller chunks rather than attemting everything in a single calculation?

Its way too late for me to try and manually work out a calculation with that many parts but if someone wants to try with the numbers above then feel free :)

I hope this can be fixed because the DayOfWeek is more important in my application than the actual date!

I would be grateful if others who may be using this code can check if theirs is broken as well.

keithdoxey
- 6th October 2006, 13:15
Sorry Melanie !!!

Your code is NOT WRONG

I tried re-creating the formula in an Excel spreadsheed forcing results to be INT but it threw up wrong results all over the place.

I then took an alternative approace of getting the PIC to output its information so that I could stick it into a spreadsheet. I then got Excel to calculate the correct weekday for each of the dates. I cant see ANY discrepancies in the 7000+ rows although I havent looked at each one individually.

Here is the section containing the dates that I posted last night....



Date Weekday DayID RealDay

9-Sep-2006 Sat 5 Saturday
10-Sep-2006 Sun 6 Sunday
11-Sep-2006 Mon 7 Monday
12-Sep-2006 Tue 1 Tuesday
13-Sep-2006 Wed 2 Wednesday
14-Sep-2006 Thu 3 Thursday
15-Sep-2006 Fri 4 Friday
16-Sep-2006 Sat 5 Saturday
17-Sep-2006 Sun 6 Sunday
18-Sep-2006 Mon 7 Monday
19-Sep-2006 Tue 1 Tuesday
20-Sep-2006 Wed 2 Wednesday
21-Sep-2006 Thu 3 Thursday
22-Sep-2006 Fri 4 Friday
23-Sep-2006 Sat 5 Saturday
24-Sep-2006 Sun 6 Sunday
25-Sep-2006 Mon 7 Monday
26-Sep-2006 Tue 1 Tuesday
27-Sep-2006 Wed 2 Wednesday
28-Sep-2006 Thu 3 Thursday
29-Sep-2006 Fri 4 Friday
30-Sep-2006 Sat 5 Saturday
1-Oct-2006 Sun 6 Sunday
2-Oct-2006 Mon 7 Monday
3-Oct-2006 Tue 1 Tuesday
4-Oct-2006 Wed 2 Wednesday
5-Oct-2006 Thu 3 Thursday
6-Oct-2006 Fri 4 Friday
7-Oct-2006 Sat 5 Saturday
8-Oct-2006 Sun 6 Sunday




Date, Weekday and DayID are what I got from the PIC.

RealDay is using the =TEXT(value,"dddd") function in Excel

What I cant figure out is that this is the SAME PROGRAM with just a small section of code copied into a separate subroutine to generate the dates for this test.

As you can see, 6-OCT-2006 gets generated as "FRI" but when I use the date setting routine in my program I end up with "SUN".

Will investigate further but it look like I was predicting the end of the world prematurely. Apologies to Melanie

keithdoxey
- 6th October 2006, 15:22
Found the problem and sorted it.

I got the PIC to output the test variables then use them to set the RTC before reading back and outputting the read values.

01-Sep-2006 Fri
01-Sep-2006 Fri

02-Sep-2006 Sat
02-Sep-2006 Sat

03-Sep-2006 Sun
03-Sep-2006 Sun

04-Sep-2006 Mon
04-Sep-2006 Mon

05-Sep-2006 Tue
05-Sep-2006 Tue

06-Sep-2006 Wed
06-Sep-2006 Wed

07-Sep-2006 Thu
07-Sep-2006 Thu

08-Sep-2006 Fri
08-Sep-2006 Fri

09-Sep-2006 Sat
09-Sep-2006 Sat

10-Sep-2006 Sun
10-Sep-2006 Sat

11-Sep-2006 Mon
11-Sep-2006 Sun

12-Sep-2006 Tue
12-Sep-2006 Mon

13-Sep-2006 Wed
13-Sep-2006 Tue

14-Sep-2006 Thu
14-Sep-2006 Wed

15-Sep-2006 Fri
15-Sep-2006 Thu

16-Sep-2006 Sat
16-Sep-2006 Fri

17-Sep-2006 Sun
17-Sep-2006 Sat

18-Sep-2006 Mon
18-Sep-2006 Sun

19-Sep-2006 Tue
19-Sep-2006 Mon

20-Sep-2006 Wed
20-Sep-2006 Mon

21-Sep-2006 Thu
21-Sep-2006 Tue

22-Sep-2006 Fri
22-Sep-2006 Wed

23-Sep-2006 Sat
23-Sep-2006 Thu

24-Sep-2006 Sun
24-Sep-2006 Fri

25-Sep-2006 Mon
25-Sep-2006 Sat

26-Sep-2006 Tue
26-Sep-2006 Sun

27-Sep-2006 Wed
27-Sep-2006 Mon

28-Sep-2006 Thu
28-Sep-2006 Tue

29-Sep-2006 Fri
29-Sep-2006 Wed

30-Sep-2006 Sat
30-Sep-2006 Wed

01-Oct-2006 Sun
01-Oct-2006 Tue

02-Oct-2006 Mon
02-Oct-2006 Wed

03-Oct-2006 Tue
03-Oct-2006 Thu

04-Oct-2006 Wed
04-Oct-2006 Fri

05-Oct-2006 Thu
05-Oct-2006 Sat

06-Oct-2006 Fri
06-Oct-2006 Sun

07-Oct-2006 Sat
07-Oct-2006 Mon

08-Oct-2006 Sun
08-Oct-2006 Tue

09-Oct-2006 Mon
09-Oct-2006 Wed

10-Oct-2006 Tue
10-Oct-2006 Wed

11-Oct-2006 Wed
11-Oct-2006 Thu

12-Oct-2006 Thu
12-Oct-2006 Fri

13-Oct-2006 Fri
13-Oct-2006 Sat

14-Oct-2006 Sat
14-Oct-2006 Sun

15-Oct-2006 Sun
15-Oct-2006 Mon

16-Oct-2006 Mon
16-Oct-2006 Tue

17-Oct-2006 Tue
17-Oct-2006 Wed

18-Oct-2006 Wed
18-Oct-2006 Thu

19-Oct-2006 Thu
19-Oct-2006 Fri

20-Oct-2006 Fri
20-Oct-2006 Fri

21-Oct-2006 Sat
21-Oct-2006 Sat

22-Oct-2006 Sun
22-Oct-2006 Sun

23-Oct-2006 Mon
23-Oct-2006 Mon

24-Oct-2006 Tue
24-Oct-2006 Tue

25-Oct-2006 Wed
25-Oct-2006 Wed

26-Oct-2006 Thu
26-Oct-2006 Thu

27-Oct-2006 Fri
27-Oct-2006 Fri

28-Oct-2006 Sat
28-Oct-2006 Sat

29-Oct-2006 Sun
29-Oct-2006 Sun

30-Oct-2006 Mon
30-Oct-2006 Sun

31-Oct-2006 Tue
31-Oct-2006 Mon


After studying my "SetRTC" subroutine I spotted the problem.

In Melanie's original code she had two sets of variables "SETvarname" and "RTCvarname". In my project the clock is set using commands via the serial port and data is fed in in the forms DDMMYY to set the date and HHMMSS to set the time.

Once a command is received I overwrite the existing RTC variables and call the SetRTC routine which converts them to BCD and sets the clock. There was the problem. Because I only have the one set of variables by the time I got the the DayofWeek calculation the values were now BCD rather than Decimal.

Moving the Day of Week calculation to before the Decimal to BCD conversions fixed the problem :)

The worst thing about the "bug" was that it only went wrong on certain dates so could have gone undetected for a long time.

Once again, apologies for doubting the integrity of Melanie's code.