fenri's diary

基本的には勉強し始めたC#のメモ。後は140字で収まらない駄文。

RTC使い方

DS3232RTC.h
使い方

RTLとArduino pro miniを接続
SCL SCL(A5)
SDA SDA(A4)
SQW  INT0(pin2) プルアップ抵抗で常時Highに。

割込がかかるとLOWになる

秒単位の指定をする場合は、ALM1
ALM2は分単位

GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks

割込の中の処理はできるだけ軽く


void setup(void)
{
  //clear all
  RTC.alarmInterrupt(1, false);
  RTC.alarmInterrupt(2, false);
  RTC.oscStopped(true);

  //sync time with RTC module
  Serial.begin(9600);
  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();

  //show current time, sync with 0 second
  digitalClockDisplay();

  //  synctozero();

  //set alarm to fire every minute
  RTC.alarm(1);
  delay(100);

 //INT0の割込(FALLING High→Low)で、alcallを呼ぶ
  attachInterrupt(0, alcall, FALLING);

  //void setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate);
  //void setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate);
  RTC.setAlarm(ALM1_MATCH_MINUTES, 30, 0, 0, 0);

  RTC.alarmInterrupt(1, true);

  digitalClockDisplay();
  Serial.println("setup end");
  Serial.flush();

}

void loop(void)
{
  Serial.println("loop");
  Serial.flush();

  //process clock display and clear interrupt flag as needed
  if (rtcint) {
    rtcint = false;
    digitalClockDisplay();
    RTC.alarm(1);
  }

  //go to power save mode
  enterSleep();
}

void synctozero() {
  //wait until second reaches 0
  while (second() != 0) {
    delay(100);
  }
}

void alcall() {
  //per minute interrupt call
  Serial.println("alcall");
  Serial.flush();
  rtcint = true;
}

void digitalClockDisplay(void)
{
  // digital clock display of the time
  setSyncProvider(RTC.get); //sync time with RTC
  Serial.print(year());
  Serial.print(' ');
  Serial.print(month());
  Serial.print(' ');
  Serial.print(day());
  Serial.print(' ');
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
  Serial.flush();
}

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);
}

void enterSleep(void)
{
  //enter sleep mode to save power
  Serial.println("Sleep Start");
  Serial.flush();

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();

  sleep_disable();
  power_all_enable();
}