log

リアルタイムクロックによる割り込み処理

他のサイトを参考にしても微妙に動かなかったのでメモ。

  • loop()関数の中でif(RTC.alarm(ALARM_1))でフラグ検出してリセット
  • コンパイラのマクロ__DATE__,__TIME__を用いて時刻設定する

 #include <DS3232RTC.h>

#include <TimeLib.h>

tmElements_t tm;
// __TIME__パース用関数
bool getTime(const char *str)
{
  int Hour, Min, Sec;
  if (sscanf(str, "%d:%d:%d"&Hour, &Min, &Sec) != 3return false;
  tm.Hour   = Hour;
  tm.Minute = Min;
  tm.Second = Sec + 12;   //★遅れ分補正★★★
  return true;
}
// __DATE__パース用関数
const char *monthName[12= {
  "Jan""Feb""Mar""Apr""May""Jun",
  "Jul""Aug""Sep""Oct""Nov""Dec"
};
bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;
  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0break;
  }
  if (monthIndex >= 12return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

void setup() {
  Serial.begin(9600);
#if 0
  //コンパイラのマクロ__DATE__,__TIME__を用いて時刻設定。10秒ほどずれる。
  if (getDate(__DATE__) && getTime(__TIME__)) {
     RTC.set(makeTime(tm));
  }
#endif
 
  // initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
  RTC.setAlarm(ALM1_MATCH_DATE, 0001);
  RTC.setAlarm(ALM2_MATCH_DATE, 0001);
  RTC.alarm(ALARM_1);
  RTC.alarm(ALARM_2);
  RTC.alarmInterrupt(ALARM_1, false);
  RTC.alarmInterrupt(ALARM_2, false);
  RTC.squareWave(SQWAVE_NONE);

  setSyncProvider(RTC.get);    // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
  Serial.flush();

  RTC.setAlarm(ALM1_MATCH_SECONDS , 30001);
  RTC.alarm(ALARM_1);
  RTC.alarmInterrupt(ALARM_1, true);

  //setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate)
  //RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 1);    // daydate parameter should be between 1 and 7
  RTC.setAlarm(ALM2_MATCH_MINUTES, 5401);
  RTC.alarm(ALARM_2);                   // ensure RTC interrupt flag is cleared
  RTC.alarmInterrupt(ALARM_2, true);
}

void loop() {
  if(RTC.alarm(ALARM_1)) {  // ensure RTC interrupt flag is cleared  
    digitalClockDisplay(); 
  }
  if(RTC.alarm(ALARM_2)) {  // ensure RTC interrupt flag is cleared
 
  ・・・
  }
}

void digitalClockDisplay() {
  // digital clock display of the time
  setSyncProvider(RTC.get);    // the function to get the time from the RTC
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.print(" ");
  Serial.print(RTC.temperature() / 4.0); //These last few lines are the only other change to the the Time.h example!
  Serial.print((char)223);
  Serial.print('C');
  Serial.println();
}
void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}