ds3231.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #ifndef DS3231_H
  3. #define DS3231_H
  4. /* i2c slave address of the DS3231 chip */
  5. #define DS3231_I2C_ADDR 0xD0
  6. /* timekeeping registers */
  7. #define DS3231_ADDR_TIME 0x00
  8. #define DS3231_ADDR_CALENDAR 0x03
  9. #define DS3231_ADDR_ALARM1 0x07
  10. #define DS3231_ADDR_ALARM2 0x0B
  11. #define DS3231_ADDR_CONTROL 0x0E
  12. #define DS3231_ADDR_STATUS 0x0F
  13. #define DS3231_ADDR_AGING_OFFSET 0x10
  14. #define DS3231_ADDR_TEMPERATURE 0x11
  15. /* control register bits */
  16. #define DS3231_A1IE 0x01
  17. #define DS3231_A2IE 0x02
  18. #define DS3231_INTCN 0x04
  19. #define DS3231_RS1 0x08
  20. #define DS3231_RS2 0x10
  21. #define DS3231_CONV 0x20
  22. #define DS3231_BBSQW 0x40
  23. #define DS3231_OSC_OSC 0x80
  24. /* control/status register bits */
  25. #define DS3231_A1F 0x01
  26. #define DS3231_A2F 0x02
  27. #define DS3231_BSY 0x04
  28. #define DS3231_EN32kHz 0x08
  29. #define DS3231_OSF 0x80
  30. /* square-wave output frequency */
  31. #define DS3231_1HZ 0x00
  32. #define DS3231_1024HZ DS3231_RS1
  33. #define DS3231_4096HZ DS3231_RS2
  34. #define DS3231_8192HZ (DS3231_RS1 | DS3231_RS2)
  35. /* number of bytes */
  36. #define DS3231_SIZE_TIME 3
  37. #define DS3231_SIZE_CALENDAR 4
  38. #define DS3231_SIZE_ALARM1 4
  39. #define DS3231_SIZE_ALARM2 3
  40. #define DS3231_SIZE_CONTROL 1
  41. #define DS3231_SIZE_STATUS 1
  42. #define DS3231_SIZE_AGOFFS 1
  43. #define DS3231_SIZE_TEMPERATURE 2
  44. #define DS3231_SIZE_ALL 19
  45. /* RTC Status */
  46. typedef enum {
  47. RTC_OK = 0x00,
  48. RTC_TWI_TimeOut,
  49. RTC_TWI_Error
  50. } rtc_status_t;
  51. /**
  52. * @brief Clock sructure.
  53. * @note structure of ds3231 data.
  54. */
  55. typedef struct {
  56. /**
  57. * @brief 00h: Seconds
  58. */
  59. uint8_t Sec;
  60. /**
  61. * @brief 01h: Minutes
  62. */
  63. uint8_t Min;
  64. /**
  65. * @brief 02h: Hours
  66. */
  67. uint8_t Hr;
  68. /**
  69. * @brief 03h: Week Day
  70. */
  71. uint8_t WD;
  72. /**
  73. * @brief 04h: Day of Month
  74. */
  75. uint8_t Day;
  76. /**
  77. * @brief 05h: Month
  78. */
  79. uint8_t Mon;
  80. /**
  81. * @brief 06h: Year
  82. */
  83. uint8_t Year;
  84. /**
  85. * @brief 07h: Alarm 1 Seconds
  86. */
  87. uint8_t A1SS;
  88. /**
  89. * @brief 08h: Alarm 1 Minutes
  90. */
  91. uint8_t A1MM;
  92. /**
  93. * @brief 09h: Alarm 1 Hours
  94. */
  95. uint8_t A1HH;
  96. /**
  97. * @brief 0Ah: Alarm 1 Day / Date
  98. */
  99. uint8_t A1DD;
  100. /**
  101. * @brief 0Bh: Alarm 2 Minutes
  102. */
  103. uint8_t A2MM;
  104. /**
  105. * @brief 0Ch: Alarm 2 Hours
  106. */
  107. uint8_t A2HH;
  108. /**
  109. * @brief 0Dh: Alarm 2 Day / Date
  110. */
  111. uint8_t A2DD;
  112. /**
  113. * @brief 0Eh: Control
  114. */
  115. uint8_t Ctrl;
  116. /**
  117. * @brief 0Fh: Control/Status
  118. */
  119. uint8_t CtSt;
  120. /**
  121. * @brief 10h: Aging Offset
  122. */
  123. uint8_t AgOffset;
  124. /**
  125. * @brief 11h: MSB of Temp
  126. */
  127. uint8_t TempH;
  128. /**
  129. * @brief 12h: LSB of Temp
  130. */
  131. uint8_t TempL;
  132. } rtc_t;
  133. void RTC_Init(void);
  134. void RTC_ReadAll(rtc_t * data);
  135. void RTC_WriteTimeCalendar(rtc_t * data);
  136. void RTC_WriteTime(rtc_t * data);
  137. void RTC_WriteHHMM(rtc_t * data);
  138. void RTC_WriteCalendar(rtc_t * data);
  139. uint8_t bcd2bin(uint8_t bcd);
  140. uint8_t bin2bcd(uint8_t bin);
  141. #endif // DS3231_H