Skip to content

Commit f2e2d68

Browse files
author
Owen
authored
Merge pull request #120 from sparkfun/rtcFixAgain
Fix month adjustment. Add test example.
2 parents 277564d + e5c3670 commit f2e2d68

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* Author: Nathan Seidle and stephenf7072
2+
Created: January 28th, 2020
3+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
4+
5+
This example test the internal HAL to make sure the days advance correctly.
6+
*/
7+
8+
#include "RTC.h"
9+
APM3_RTC myRTC; //Create instance of RTC class
10+
11+
int previousDay = 1;
12+
13+
void setup()
14+
{
15+
Serial.begin(115200);
16+
delay(10);
17+
Serial.println("Artemis RTC testing");
18+
19+
//myRTC.setTime(hh, mm, ss, hund, dd, mm, yy);
20+
myRTC.setTime(23, 59, 59, 99, 1, 1, 19); //Manually set RTC to 1s before midnight
21+
}
22+
23+
void loop()
24+
{
25+
printArtemisTime();
26+
27+
myRTC.getTime();
28+
myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); //Manually set RTC
29+
delay(11); //Allow us to roll from midnight the night before to the new day
30+
}
31+
32+
void printArtemisTime()
33+
{
34+
char buf[50];
35+
char weekdayBuf[4];
36+
37+
myRTC.getTime();
38+
int i = myRTC.weekday + 1;
39+
switch (i)
40+
{
41+
case (1):
42+
strcpy(weekdayBuf, "Sun");
43+
break;
44+
case (2):
45+
strcpy(weekdayBuf, "Mon");
46+
break;
47+
case (3):
48+
strcpy(weekdayBuf, "Tue");
49+
break;
50+
case (4):
51+
strcpy(weekdayBuf, "Wed");
52+
break;
53+
case (5):
54+
strcpy(weekdayBuf, "Thu");
55+
break;
56+
case (6):
57+
strcpy(weekdayBuf, "Fri");
58+
break;
59+
case (7):
60+
strcpy(weekdayBuf, "Sat");
61+
break;
62+
63+
default:
64+
strcpy(weekdayBuf, "???");
65+
break;
66+
}
67+
68+
sprintf(buf, "%02d-%02d-%02d (%s) %02d:%02d:%02d.%02d", myRTC.year, myRTC.month, myRTC.dayOfMonth, weekdayBuf, myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
69+
Serial.print(buf);
70+
71+
//Move the previous day forward one day and make sure it matches today
72+
if ((previousDay + 1) % 7 != myRTC.weekday)
73+
{
74+
Serial.printf(" Error! previousDay: %d today: %d\n", previousDay, myRTC.weekday);
75+
while (1)
76+
;
77+
}
78+
79+
previousDay = myRTC.weekday;
80+
81+
Serial.println();
82+
}

libraries/RTC/src/RTC.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void APM3_RTC::setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uin
5555
hal_time.ui32Hundredths = hund;
5656

5757
hal_time.ui32DayOfMonth = dayOfMonth;
58-
hal_time.ui32Month = month - 1; //HAL is expecting 0 to 11 months
58+
hal_time.ui32Month = month; //HAL is expecting 1 to 12 months
5959
hal_time.ui32Year = year;
6060
hal_time.ui32Century = 0;
6161

@@ -76,7 +76,7 @@ void APM3_RTC::setToCompilerTime()
7676
hal_time.ui32Hundredths = 00;
7777
hal_time.ui32Weekday = am_util_time_computeDayofWeek(2000 + toVal(&__DATE__[9]), mthToIndex(&__DATE__[0]) + 1, toVal(&__DATE__[4]));
7878
hal_time.ui32DayOfMonth = toVal(&__DATE__[4]);
79-
hal_time.ui32Month = mthToIndex(&__DATE__[0]);
79+
hal_time.ui32Month = mthToIndex(&__DATE__[0]) + 1; //Compiler ouputs months in 0-11.
8080
hal_time.ui32Year = toVal(&__DATE__[9]);
8181
hal_time.ui32Century = 0;
8282

@@ -92,7 +92,7 @@ void APM3_RTC::getTime()
9292
seconds = hal_time.ui32Second;
9393
hundredths = hal_time.ui32Hundredths;
9494

95-
month = hal_time.ui32Month + 1; //Convert from 0-11 to 1-12
95+
month = hal_time.ui32Month; //HAL outputs months in 1 to 12 form
9696
dayOfMonth = hal_time.ui32DayOfMonth;
9797
year = hal_time.ui32Year;
9898

0 commit comments

Comments
 (0)