You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.0 KiB

6 years ago
  1. #ifndef DS3231_h
  2. #define DS3231_h
  3. #include <Arduino.h>
  4. /**
  5. TODO: alarms
  6. */
  7. class DS3231
  8. {
  9. public:
  10. // begin function
  11. void begin();
  12. // get seconds
  13. uint8_t getSeconds();
  14. // set seconds
  15. void setSeconds(uint8_t seconds);
  16. // increment seconds
  17. void incrementSeconds();
  18. // decrement seconds
  19. void decrementSeconds();
  20. // get minutes
  21. uint8_t getMinutes();
  22. // set minutes
  23. void setMinutes(uint8_t minutes);
  24. // increment minutes
  25. void incrementMinutes();
  26. // decrement minutes
  27. void decrementMinutes();
  28. // get hours
  29. uint8_t getHours();
  30. // set hours
  31. void setHours(uint8_t hours);
  32. // increment hours
  33. void incrementHours();
  34. // decrement hours
  35. void decrementHours();
  36. // get day
  37. uint8_t getDay();
  38. // set day
  39. void setDay(uint8_t day);
  40. // get date
  41. uint8_t getDate();
  42. // set date
  43. void setDate(uint8_t date);
  44. // increment date
  45. void incrementDate();
  46. // decrement date
  47. void decrementDate();
  48. // get month
  49. uint8_t getMonth();
  50. // set month
  51. void setMonth(uint8_t month);
  52. // increment month
  53. void incrementMonth();
  54. // decrement month
  55. void decrementMonth();
  56. // get year
  57. int16_t getYear();
  58. // set year
  59. void setYear(int16_t year);
  60. // increment year
  61. void incrementYear();
  62. // decrement year
  63. void decrementYear();
  64. // get control register
  65. uint8_t getControlRegister();
  66. // set control register
  67. void setControlRegister(uint8_t value);
  68. // set status register
  69. uint8_t getStatusRegister();
  70. // set status register
  71. void setStatusRegister(uint8_t value);
  72. // get temperature
  73. uint8_t getTemperature();
  74. // helpers
  75. protected:
  76. // get register
  77. uint8_t getRegister(uint8_t address);
  78. // set register
  79. void setRegister(uint8_t address, uint8_t value);
  80. // decimal to binary coded decimal
  81. uint8_t dectobcd(uint8_t value);
  82. // binary coded decimal to decimal
  83. uint8_t bcdtodec(uint8_t value);
  84. // get century value
  85. uint8_t getCentury();
  86. // set century value
  87. void setCentury(uint8_t century);
  88. };
  89. // I2C address
  90. #define DS3231_I2C_ADDRESS 0x68
  91. // I2C registers
  92. #define DS3231_SECONDS 0x00
  93. #define DS3231_MINUTES 0x01
  94. #define DS3231_HOURS 0x02
  95. #define DS3231_DAY 0x03
  96. #define DS3231_DATE 0x04
  97. #define DS3231_MONTH 0x05
  98. #define DS3231_YEAR 0x06
  99. #define DS3231_ALARM1_SECONDS 0x07
  100. #define DS3231_ALARM1_MINUTES 0x08
  101. #define DS3231_ALARM1_HOURS 0x09
  102. #define DS3231_ALARM1_DATE 0x0A
  103. #define DS3231_ALARM2_MINUTES 0x0B
  104. #define DS3231_ALARM2_HOURS 0x0C
  105. #define DS3231_ALARM1_DATE 0x0D
  106. #define DS3231_CONTROL 0x0E
  107. #define DS3231_STATUS 0x0F
  108. #define DS3231_AGING_OFFSET 0x10
  109. #define DS3231_MSB_TEMP 0x11
  110. #define DS3231_LSB_TEMP 0x12
  111. // control registers
  112. #define DS3231_CONTROL_A1IE 0x01
  113. #define DS3231_CONTROL_A2IE 0x02
  114. #define DS3231_CONTROL_INTCN 0x04
  115. #define DS3231_CONTROL_RS1 0x08
  116. #define DS3231_CONTROL_RS2 0x10
  117. #define DS3231_CONTROL_CONV 0x20
  118. #define DS3231_CONTROL_BBSQW 0x40
  119. #define DS3231_CONTROL_EOSC 0x80
  120. // status registers
  121. #define DS3231_STATUSL_A1F 0x01
  122. #define DS3231_STATUSL_A2F 0x02
  123. #define DS3231_STATUSL_BSY 0x04
  124. #define DS3231_STATUSL_EN32KHZ 0x08
  125. #define DS3231_STATUSL_OSF 0x80
  126. #endif