Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. // Arduino DS3232RTC Library
  2. // https://github.com/JChristensen/DS3232RTC
  3. // Copyright (C) 2018 by Jack Christensen and licensed under
  4. // GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
  5. //
  6. // Example sketch to demonstrate usage of the alarm interrupt
  7. // for alarm 1 and alarm 2.
  8. //
  9. // Notes:
  10. // Using the INT/SQW pin for alarms is mutually exclusive with using
  11. // it to output a square wave. However, alarms can still be set when
  12. // a square wave is output, but then the alarm() function will need
  13. // to be used to determine if an alarm has triggered. Even though
  14. // the DS3231 power-on default for the INT/SQW pin is as an alarm
  15. // output, it's good practice to call RTC.squareWave(SQWAVE_NONE)
  16. // before setting alarms.
  17. //
  18. // I recommend calling RTC.alarm() before RTC.alarmInterrupt()
  19. // to ensure the RTC's alarm flag is cleared.
  20. //
  21. // The RTC's time is updated on the falling edge of the 1Hz square
  22. // wave (whether it is output or not). However, the Arduino Time
  23. // library has no knowledge of this, as its time is set asynchronously
  24. // with the RTC via I2C. So on average, it will be 500ms slow
  25. // immediately after setting its time from the RTC. This is seen
  26. // in the sketch output as a one-second difference because the
  27. // time returned by now() has not yet rolled to the next second.
  28. //
  29. // Tested with Arduino 1.8.5, Arduino Uno, DS3231.
  30. // Connect RTC SDA to Arduino pin A4.
  31. // Connect RTC SCL to Arduino pin A5.
  32. // Connect RTC INT/SQW to Arduino pin 2.
  33. //
  34. // Jack Christensen 01Jan2018
  35.  
  36. #include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
  37. #include <Streaming.h> // http://arduiniana.org/libraries/streaming/
  38. #include "LowPower.h"
  39. #include "PinChangeInterrupt.h"
  40. const uint8_t SQW_PIN(10); // connect this pin to DS3231 INT/SQW pin
  41.  
  42. void setup()
  43. {
  44. Serial.begin(115200);
  45.  
  46. // initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
  47. RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
  48. //RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
  49. RTC.alarm(ALARM_1);
  50. //RTC.alarm(ALARM_2);
  51. RTC.alarmInterrupt(ALARM_1, false);
  52. //RTC.alarmInterrupt(ALARM_2, false);
  53. RTC.squareWave(SQWAVE_NONE);
  54.  
  55. // setSyncProvider() causes the Time library to synchronize with the
  56. // external RTC by calling RTC.get() every five minutes by default.
  57. setSyncProvider(RTC.get);
  58. Serial << "RTC Sync";
  59. if (timeStatus() != timeSet)
  60. {
  61. Serial << " FAIL!";
  62. }
  63. Serial << endl;
  64.  
  65. printDateTime(RTC.get());
  66. Serial << " --> Current RTC time\n";
  67.  
  68. // configure an interrupt on the falling edge from the SQW pin
  69. pinMode(SQW_PIN, INPUT_PULLUP);
  70. attachPCINT(digitalPinToPCINT(SQW_PIN), wake, FALLING);
  71.  
  72. // set alarm 1 for 20 seconds after every minute
  73. RTC.setAlarm(ALM1_MATCH_SECONDS, 5, 0, 0, 1); // daydate parameter should be between 1 and 7
  74. RTC.alarm(ALARM_1); // ensure RTC interrupt flag is cleared
  75. RTC.alarmInterrupt(ALARM_1, true);
  76.  
  77. // set alarm 2 for every minute
  78. // RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 1); // daydate parameter should be between 1 and 7
  79. // RTC.alarm(ALARM_2); // ensure RTC interrupt flag is cleared
  80. // RTC.alarmInterrupt(ALARM_2, true);
  81. }
  82.  
  83. volatile boolean alarmIsrWasCalled = false;
  84.  
  85. void wake()
  86. {
  87. alarmIsrWasCalled = true;
  88. }
  89.  
  90. void loop()
  91. {
  92. delay(200);
  93. LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  94. if (alarmIsrWasCalled)
  95. {
  96. if (RTC.alarm(ALARM_1))
  97. {
  98. printDateTime( RTC.get() );
  99. Serial << " --> Alarm 1\n";
  100. }
  101.  
  102. alarmIsrWasCalled = false;
  103. }
  104.  
  105. Serial.println("Kirceeee ljutt");
  106. }
  107.  
  108. void printDateTime(time_t t)
  109. {
  110. Serial << ((day(t)<10) ? "0" : "") << _DEC(day(t));
  111. Serial << monthShortStr(month(t)) << _DEC(year(t)) << ' ';
  112. Serial << ((hour(t)<10) ? "0" : "") << _DEC(hour(t)) << ':';
  113. Serial << ((minute(t)<10) ? "0" : "") << _DEC(minute(t)) << ':';
  114. Serial << ((second(t)<10) ? "0" : "") << _DEC(second(t));
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement