ds3231.h 1.9 KB

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