ds3231.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #ifndef DS3231_H
  3. #define DS3231_H
  4. /* i2c slave address of the DS3231 chip */
  5. #define DS3231_I2C_WRADDR 0xD0
  6. #define DS3231_I2C_RDADDR 0xD1
  7. /* timekeeping registers */
  8. #define DS3231_TIME_CAL_ADDR 0x00
  9. #define DS3231_CALENDAR_ADDR 0x03
  10. #define DS3231_ALARM1_ADDR 0x07
  11. #define DS3231_ALARM2_ADDR 0x0B
  12. #define DS3231_CONTROL_ADDR 0x0E
  13. #define DS3231_STATUS_ADDR 0x0F
  14. #define DS3231_AGING_OFFSET_ADDR 0x10
  15. #define DS3231_TEMPERATURE_ADDR 0x11
  16. // control register bits
  17. #define DS3231_A1IE 0x01
  18. #define DS3231_A2IE 0x02
  19. #define DS3231_INTCN 0x04
  20. #define DS3231_RS1 0x08
  21. #define DS3231_RS2 0x10
  22. #define DS3231_CONV 0x20
  23. #define DS3231_BBSQW 0x40
  24. #define DS3231_OSC_OSC 0x80
  25. /* square-wave output frequency */
  26. #define DS3231_1HZ 0x00
  27. #define DS3231_1024HZ DS3231_RS1
  28. #define DS3231_4096HZ DS3231_RS2
  29. #define DS3231_8192HZ (DS3231_RS1 | DS3231_RS2)
  30. /* status register bits */
  31. #define DS3231_A1F 0x01
  32. #define DS3231_A2F 0x02
  33. #define DS3231_OSF 0x80
  34. /* RTC Status */
  35. typedef enum {
  36. RTC_OK = 0x00,
  37. RTC_TWI_TimeOut,
  38. RTC_TWI_Error
  39. } rtc_status_t;
  40. /**
  41. * @brief Clock sructure.
  42. * @note structure of ds3231 data.
  43. */
  44. typedef struct {
  45. /**
  46. * @brief Seconds
  47. */
  48. uint8_t Sec;
  49. /**
  50. * @brief Minutes
  51. */
  52. uint8_t Min;
  53. /**
  54. * @brief Hours
  55. */
  56. uint8_t Hr;
  57. /**
  58. * @brief Week Day
  59. */
  60. uint8_t WD;
  61. /**
  62. * @brief Day of Month
  63. */
  64. uint8_t Day;
  65. /**
  66. * @brief Month
  67. */
  68. uint8_t Mon;
  69. /**
  70. * @brief Year
  71. */
  72. uint8_t Year;
  73. } rtc_t;
  74. /*
  75. void RTC_Init(void);
  76. void RTC_ReadAll(rtc_t * data);
  77. void RTC_ReadTime(rtc_t * data);
  78. void RTC_ReadCalendar(rtc_t * data);
  79. void RTC_WriteAll(rtc_t * data);
  80. void RTC_WriteTime(rtc_t * data);
  81. void RTC_WriteHHMM(rtc_t * data);
  82. void RTC_WriteCalendar(rtc_t * data);
  83. uint8_t bcd2bin(uint8_t bcd);
  84. uint8_t bin2bcd(uint8_t bin);
  85. */
  86. #endif // DS3231_H