Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions libraries/RTC/examples/Example3_TestRTC/Example3_TestRTC.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* Author: Nathan Seidle and stephenf7072
Created: January 28th, 2020
License: MIT. See SparkFun Arduino Apollo3 Project for more information

This example test the internal HAL to make sure the days advance correctly.
*/

#include "RTC.h"
APM3_RTC myRTC; //Create instance of RTC class

int previousDay = 1;

void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Artemis RTC testing");

//myRTC.setTime(hh, mm, ss, hund, dd, mm, yy);
myRTC.setTime(23, 59, 59, 99, 1, 1, 19); //Manually set RTC to 1s before midnight
}

void loop()
{
printArtemisTime();

myRTC.getTime();
myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); //Manually set RTC
delay(11); //Allow us to roll from midnight the night before to the new day
}

void printArtemisTime()
{
char buf[50];
char weekdayBuf[4];

myRTC.getTime();
int i = myRTC.weekday + 1;
switch (i)
{
case (1):
strcpy(weekdayBuf, "Sun");
break;
case (2):
strcpy(weekdayBuf, "Mon");
break;
case (3):
strcpy(weekdayBuf, "Tue");
break;
case (4):
strcpy(weekdayBuf, "Wed");
break;
case (5):
strcpy(weekdayBuf, "Thu");
break;
case (6):
strcpy(weekdayBuf, "Fri");
break;
case (7):
strcpy(weekdayBuf, "Sat");
break;

default:
strcpy(weekdayBuf, "???");
break;
}

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);
Serial.print(buf);

//Move the previous day forward one day and make sure it matches today
if ((previousDay + 1) % 7 != myRTC.weekday)
{
Serial.printf(" Error! previousDay: %d today: %d\n", previousDay, myRTC.weekday);
while (1)
;
}

previousDay = myRTC.weekday;

Serial.println();
}
6 changes: 3 additions & 3 deletions libraries/RTC/src/RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void APM3_RTC::setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uin
hal_time.ui32Hundredths = hund;

hal_time.ui32DayOfMonth = dayOfMonth;
hal_time.ui32Month = month - 1; //HAL is expecting 0 to 11 months
hal_time.ui32Month = month; //HAL is expecting 1 to 12 months
hal_time.ui32Year = year;
hal_time.ui32Century = 0;

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

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

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

Expand Down