リアルタイムクロックによる割り込み処理
他のサイトを参考にしても微妙に動かなかったのでメモ。
- loop()関数の中でif(RTC.alarm(ALARM_1))でフラグ検出してリセット
- コンパイラのマクロ__DATE__,__TIME__を用いて時刻設定する
HiLetgo 3個セット DS3231 AT24C32 時計モジュール リアル時間時計モジュール IICモジュール RTCモジュール Arduinoに対応 [並行輸入品]
- 出版社/メーカー: HiLetgo
- メディア: おもちゃ&ホビー
- この商品を含むブログを見る
#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) != 3) return false;tm.Hour = Hour;tm.Minute = Min;tm.Second = Sec + 12; //★遅れ分補正★★★return true;}// __DATE__パース用関数const char *monthName[12] = {"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) != 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;}
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 flagsRTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);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 RTCif (timeStatus() != timeSet)Serial.println("Unable to sync with the RTC");elseSerial.println("RTC has set the system time");Serial.flush();
RTC.setAlarm(ALM1_MATCH_SECONDS , 30, 0, 0, 1);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 7RTC.setAlarm(ALM2_MATCH_MINUTES, 54, 0, 1);RTC.alarm(ALARM_2); // ensure RTC interrupt flag is clearedRTC.alarmInterrupt(ALARM_2, true);}
void loop() {if(RTC.alarm(ALARM_1)) { // ensure RTC interrupt flag is cleareddigitalClockDisplay();}if(RTC.alarm(ALARM_2)) { // ensure RTC interrupt flag is cleared・・・}}
void digitalClockDisplay() {// digital clock display of the timesetSyncProvider(RTC.get); // the function to get the time from the RTCSerial.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 0Serial.print(":");if (digits < 10)Serial.print('0');Serial.print(digits);}