ds3231.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "ds3231.h"
  2. #include "i2c.h"
  3. /**
  4. * @brief Инициализация RTC
  5. */
  6. void RTC_Init(void) {
  7. I2C_Start();
  8. I2C_WriteByte(DS3231_I2C_WRADDR);
  9. I2C_WriteByte(DS3231_CONTROL_ADDR);
  10. I2C_WriteByte(DS3231_CONV);
  11. I2C_Stop();
  12. }
  13. /**
  14. * @brief Чтение времени и календаря
  15. */
  16. void RTC_ReadAll(rtc_t * data) {
  17. I2C_Start();
  18. I2C_WriteByte(DS3231_I2C_WRADDR);
  19. I2C_WriteByte(DS3231_TIME_CAL_ADDR);
  20. I2C_Stop();
  21. I2C_Start();
  22. I2C_WriteByte(DS3231_I2C_RDADDR);
  23. I2C_ReadByte(&data->Sec, TWI_ACK);
  24. I2C_ReadByte(&data->Min, TWI_ACK);
  25. I2C_ReadByte(&data->Hr, TWI_ACK);
  26. I2C_ReadByte(&data->WD, TWI_ACK);
  27. I2C_ReadByte(&data->Day, TWI_ACK);
  28. I2C_ReadByte(&data->Mon, TWI_ACK);
  29. I2C_ReadByte(&data->Year, TWI_NACK);
  30. I2C_Stop();
  31. }
  32. /**
  33. * @brief Чтение времени
  34. */
  35. void RTC_ReadTime(rtc_t * data) {
  36. I2C_Start();
  37. I2C_WriteByte(DS3231_I2C_WRADDR);
  38. I2C_WriteByte(DS3231_TIME_CAL_ADDR);
  39. I2C_Stop();
  40. I2C_Start();
  41. I2C_WriteByte(DS3231_I2C_RDADDR);
  42. I2C_ReadByte(&data->Sec, TWI_ACK);
  43. I2C_ReadByte(&data->Min, TWI_ACK);
  44. I2C_ReadByte(&data->Hr, TWI_NACK);
  45. I2C_Stop();
  46. }
  47. /**
  48. * @brief Чтение календаря
  49. */
  50. void RTC_ReadCalendar(rtc_t * data) {
  51. I2C_Start();
  52. I2C_WriteByte(DS3231_I2C_WRADDR);
  53. I2C_WriteByte(DS3231_CALENDAR_ADDR);
  54. I2C_Stop();
  55. I2C_Start();
  56. I2C_WriteByte(DS3231_I2C_RDADDR);
  57. I2C_ReadByte(&data->WD, TWI_ACK);
  58. I2C_ReadByte(&data->Day, TWI_ACK);
  59. I2C_ReadByte(&data->Mon, TWI_ACK);
  60. I2C_ReadByte(&data->Year, TWI_NACK);
  61. I2C_Stop();
  62. }
  63. /**
  64. * @brief Запись времени и календаря
  65. */
  66. void RTC_WriteAll(rtc_t * data) {
  67. I2C_Start();
  68. I2C_WriteByte(DS3231_I2C_WRADDR);
  69. I2C_WriteByte(DS3231_TIME_CAL_ADDR);
  70. I2C_WriteByte(data->Sec);
  71. I2C_WriteByte(data->Min);
  72. I2C_WriteByte(data->Hr);
  73. I2C_WriteByte(data->WD);
  74. I2C_WriteByte(data->Day);
  75. I2C_WriteByte(data->Mon);
  76. I2C_WriteByte(data->Year);
  77. I2C_Stop();
  78. }
  79. /**
  80. * @brief Запись времени
  81. */
  82. void RTC_WriteTime(rtc_t * data) {
  83. I2C_Start();
  84. I2C_WriteByte(DS3231_I2C_WRADDR);
  85. I2C_WriteByte(DS3231_TIME_CAL_ADDR);
  86. I2C_WriteByte(data->Sec);
  87. I2C_WriteByte(data->Min);
  88. I2C_WriteByte(data->Hr);
  89. I2C_Stop();
  90. }
  91. /**
  92. * @brief Запись часов
  93. */
  94. void RTC_WriteHH(rtc_t * data) {
  95. I2C_Start();
  96. I2C_WriteByte(DS3231_I2C_WRADDR);
  97. I2C_WriteByte(DS3231_TIME_CAL_ADDR + 2);
  98. I2C_WriteByte(data->Hr);
  99. I2C_Stop();
  100. }
  101. /**
  102. * @brief Запись часов и минут
  103. */
  104. void RTC_WriteHHMM(rtc_t * data) {
  105. I2C_Start();
  106. I2C_WriteByte(DS3231_I2C_WRADDR);
  107. I2C_WriteByte(DS3231_TIME_CAL_ADDR + 1);
  108. I2C_WriteByte(data->Min);
  109. I2C_WriteByte(data->Hr);
  110. I2C_Stop();
  111. }
  112. /**
  113. * @brief Запись календаря
  114. */
  115. void RTC_WriteCalendar(rtc_t * data) {
  116. I2C_Start();
  117. I2C_WriteByte(DS3231_I2C_WRADDR);
  118. I2C_WriteByte(DS3231_CALENDAR_ADDR);
  119. I2C_WriteByte(data->WD);
  120. I2C_WriteByte(data->Day);
  121. I2C_WriteByte(data->Mon);
  122. I2C_WriteByte(data->Year);
  123. I2C_Stop();
  124. }
  125. /**
  126. * @brief Convert BCD value to Binary
  127. */
  128. uint8_t bcd2bin(uint8_t bcd)
  129. {
  130. return (10 * (bcd >> 4) + (bcd & 0x0f));
  131. }
  132. /**
  133. * @brief Convert Binary value to BCD
  134. */
  135. uint8_t bin2bcd(uint8_t bin)
  136. {
  137. return (((bin / 10 ) << 4) | (bin % 10));
  138. }