ds3231.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef _DS_3231_H_
  2. #define _DS_3231_H_
  3. #define DS3231_ADDR 0x68
  4. typedef union {
  5. struct {
  6. char Value : 7;
  7. char : 1;
  8. };
  9. char AS_BYTE;
  10. } T_SECOND;
  11. typedef union {
  12. struct {
  13. char Value : 7;
  14. char : 1;
  15. };
  16. char AS_BYTE;
  17. } T_MINUTE;
  18. typedef union {
  19. struct {
  20. char Value : 6;
  21. char n24 : 1;
  22. char : 1;
  23. };
  24. char AS_BYTE;
  25. } T_HOUR;
  26. typedef union {
  27. struct {
  28. char Value : 3;
  29. char : 5;
  30. };
  31. char AS_BYTE;
  32. } T_DAY;
  33. typedef union {
  34. struct {
  35. char Value : 6;
  36. char : 2;
  37. };
  38. char AS_BYTE;
  39. } T_DATE;
  40. typedef union {
  41. struct {
  42. char Value : 5;
  43. char : 2;
  44. char Century : 1;
  45. };
  46. char AS_BYTE;
  47. } T_MONTH;
  48. typedef union {
  49. struct {
  50. char Value : 8;
  51. };
  52. char AS_BYTE;
  53. } T_YEAR;
  54. typedef union {
  55. struct {
  56. char Value : 7;
  57. char M1 : 1;
  58. };
  59. char AS_BYTE;
  60. } A_SECOND;
  61. typedef union {
  62. struct {
  63. char Value : 7;
  64. char M2 : 1;
  65. };
  66. char AS_BYTE;
  67. } A_MINUTE;
  68. typedef union {
  69. struct {
  70. char Value : 6;
  71. char n24 : 1;
  72. char M3 : 1;
  73. };
  74. char AS_BYTE;
  75. } A_HOUR;
  76. typedef union {
  77. struct {
  78. char Value : 6;
  79. char nDT : 1;
  80. char M4 : 1;
  81. };
  82. char AS_BYTE;
  83. } A_DAY_DATE;
  84. typedef union {
  85. struct {
  86. char A1IE : 1;
  87. char A2IE : 1;
  88. char INTCN : 1;
  89. char RS1 : 1;
  90. char RS2 : 1;
  91. char CONV : 1;
  92. char BBSQW : 1;
  93. char nEOSC : 1;
  94. };
  95. char AS_BYTE;
  96. } CONTROL_1;
  97. typedef union {
  98. struct {
  99. char A1F : 1;
  100. char A2F : 1;
  101. char BSY : 1;
  102. char EN32KHZ : 1;
  103. char : 3;
  104. char OSF : 1;
  105. };
  106. char AS_BYTE;
  107. } CONTROL_2;
  108. typedef union {
  109. struct {
  110. char Value : 7;
  111. char Sign : 1;
  112. };
  113. char AS_BYTE;
  114. } AGING_OFFSET;
  115. typedef union {
  116. struct {
  117. char Value : 7;
  118. char Sign : 1;
  119. };
  120. char AS_BYTE;
  121. } TEMP_MSB;
  122. typedef union {
  123. struct {
  124. char : 6;
  125. char Value : 2;
  126. };
  127. char AS_BYTE;
  128. } TEMP_LSB;
  129. typedef union {
  130. struct {
  131. T_SECOND Second; // 0x00
  132. T_MINUTE Minute; // 0x01
  133. T_HOUR Hour; // 0x02
  134. T_DAY Day; // 0x03
  135. T_DATE Date; // 0x04
  136. T_MONTH Month; // 0x05
  137. T_YEAR Year; // 0x06
  138. A_SECOND A1_Second; // 0x07
  139. A_MINUTE A1_Minute; // 0x08
  140. A_HOUR A1_Hour; // 0x09
  141. A_DAY_DATE A1_Day_Date; // 0x0A
  142. A_MINUTE A2_Minute; // 0x0B
  143. A_HOUR A2_Hour; // 0x0C
  144. A_DAY_DATE A2_Day_Date; // 0x0D
  145. CONTROL_1 Control_1; // 0x0E
  146. CONTROL_2 Control_2; // 0x0F
  147. AGING_OFFSET Aging_Offset; // 0x10
  148. TEMP_MSB Temp_MSB; // 0x11
  149. TEMP_LSB Temp_LSB; // 0x12
  150. };
  151. char AS_BYTE[0x13];
  152. } DS3231_REGS;
  153. enum {
  154. SUNDAY = 0,
  155. MONDAY = 1,
  156. TUESDAY = 2,
  157. WEDNESDAY = 3,
  158. THURSDAY = 4,
  159. FRIDAY = 5,
  160. SATURDAY = 6
  161. };
  162. void DS3231_Init(void (*Callback)());
  163. void DS3231_SetTime(int Second, int Minute, int Hour);
  164. void DS3231_SetDate(int Day, int Date, int Month, int Year, int Century);
  165. void DS3231_GetTime(int *Second, int *Minute, int *Hour);
  166. void DS3231_GetDate(int *Day, int *Date, int *Month, int *Year);
  167. bool IsDst(int Day, int Date, int Month, int Hour_24);
  168. #endif