Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

59 rader
1.2 KiB

  1. #include "Rtc.h"
  2. #include "Pins.h"
  3. #include <Wire.h>
  4. #include <MD_DS3231.h>
  5. #define LOGGER_NAME "Rtc"
  6. namespace rtc {
  7. namespace details {
  8. void readTimeFromRegisters(tmElements_t &time) {
  9. time.Second = RTC.s;
  10. time.Minute = RTC.m;
  11. time.Hour = RTC.h;
  12. time.Wday = RTC.dow;
  13. time.Day = RTC.dd;
  14. time.Month = RTC.mm;
  15. time.Year = CalendarYrToTm(RTC.yyyy);
  16. }
  17. void writeTimeToRegisters(tmElements_t &time) {
  18. RTC.s = time.Second;
  19. RTC.m = time.Minute;
  20. RTC.h = time.Hour;
  21. RTC.dow = time.Wday;
  22. RTC.dd = time.Day;
  23. RTC.mm = time.Month;
  24. RTC.yyyy = tmYearToCalendar(time.Year);
  25. }
  26. }
  27. void setup() {
  28. RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock
  29. RTC.control(DS3231_INT_ENABLE, DS3231_OFF); //INTCN OFF
  30. }
  31. void getTime(tmElements_t &time) {
  32. RTC.readTime();
  33. details::readTimeFromRegisters(time);
  34. }
  35. void setTime(tmElements_t &time) {
  36. details::writeTimeToRegisters(time);
  37. RTC.writeTime();
  38. }
  39. void setAlarm(tmElements_t &time) {
  40. details::writeTimeToRegisters(time);
  41. RTC.writeAlarm1(DS3231_ALM_S);
  42. RTC.control(DS3231_A1_FLAG, DS3231_OFF);
  43. RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON
  44. RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
  45. }
  46. }