RTC test on M-DUINO PLUS version

RTC test on M-DUINO PLUS version

In this post is showed how to test RTC (Real Time Clock) on M-Duino PLUS version

First it is required to install Timelib library and DS1307RTC library. Once these libraries are intalled you can proceed uploading this sketch on your M-Duino PLUS version:

#define TEST_RTC
#define TEST_RTC_WRITE

#include <TimeLib.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
///////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600L);
  Serial.println("M-Duino PLUS RTC test started");

#if defined TEST_RTC
  Serial.println("RTC test enabled");
#if defined TEST_RTC_WRITE
  Serial.println("Get time ...");
  tmElements_t tm;
  tm.Wday = 0;
  if (getDate(__DATE__, tm) && getTime(__TIME__, tm)) {
    Serial.println("Set RTC time ...");
    if (RTC.write(tm)) {
      Serial.println("RTC set OK");
    } else {
      Serial.println("RTC set ERROR");
    }
  }
}
#endif // TEST_RTC_WRITE
#endif // TEST_RTC

////////////////////////////////////////////////////////////////////////////
void loop() {
#if defined TEST_RTC
  tmElements_t tm;

  Serial.println("Read RTC ...");
  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    Serial.print(tm.Hour);
    Serial.write(':');
    Serial.print(tm.Minute);
    Serial.write(':');
    Serial.print(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}
#endif // TEST_RTC

#if defined TEST_RTC
#if defined TEST_RTC_WRITE
bool getTime(const char *str, tmElements_t &tm)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str, tmElements_t &tm)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}
#endif // TEST_RTC_WRITE
#endif // TEST_RTC

 

Comments are closed.