You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
6.0 KiB

5 years ago
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <DS3231.h>
  4. void DS3231::begin()
  5. {
  6. Wire.begin();
  7. }
  8. uint8_t DS3231::getSeconds()
  9. {
  10. uint8_t seconds;
  11. // read minutes
  12. seconds = getRegister(DS3231_SECONDS);
  13. return bcdtodec(seconds);
  14. }
  15. void DS3231::setSeconds(uint8_t seconds)
  16. {
  17. // write seconds
  18. setRegister(DS3231_SECONDS, dectobcd(seconds));
  19. }
  20. void DS3231::incrementSeconds()
  21. {
  22. uint8_t seconds = getSeconds();
  23. if (seconds == 58)
  24. {
  25. seconds = 0;
  26. }
  27. else
  28. {
  29. seconds += 2;
  30. }
  31. setSeconds(seconds);
  32. }
  33. void DS3231::decrementSeconds()
  34. {
  35. uint8_t seconds = getSeconds();
  36. if (seconds == 1)
  37. {
  38. seconds = 59;
  39. }
  40. else
  41. {
  42. seconds -= 2;
  43. }
  44. setSeconds(seconds);
  45. }
  46. uint8_t DS3231::getMinutes()
  47. {
  48. uint8_t minutes;
  49. // read minutes
  50. minutes = getRegister(DS3231_MINUTES);
  51. return bcdtodec(minutes);
  52. }
  53. void DS3231::setMinutes(uint8_t minutes)
  54. {
  55. // write minutes
  56. setRegister(DS3231_MINUTES, dectobcd(minutes));
  57. }
  58. void DS3231::incrementMinutes()
  59. {
  60. uint8_t minutes = getMinutes();
  61. if (minutes == 59)
  62. {
  63. minutes = 0;
  64. }
  65. else
  66. {
  67. minutes++;
  68. }
  69. setMinutes(minutes);
  70. }
  71. void DS3231::decrementMinutes()
  72. {
  73. uint8_t minutes = getMinutes();
  74. if (minutes == 0)
  75. {
  76. minutes = 59;
  77. }
  78. else
  79. {
  80. minutes--;
  81. }
  82. setMinutes(minutes);
  83. }
  84. uint8_t DS3231::getHours()
  85. {
  86. uint8_t hours;
  87. // read hours
  88. hours = getRegister(DS3231_HOURS);
  89. return bcdtodec(hours);
  90. }
  91. void DS3231::setHours(uint8_t hours)
  92. {
  93. // write hours
  94. setRegister(DS3231_HOURS, dectobcd(hours));
  95. }
  96. void DS3231::incrementHours()
  97. {
  98. uint8_t hours = getHours();
  99. if (hours == 23)
  100. {
  101. hours = 0;
  102. }
  103. else
  104. {
  105. hours++;
  106. }
  107. setHours(hours);
  108. }
  109. void DS3231::decrementHours()
  110. {
  111. uint8_t hours = getHours();
  112. if (hours == 0)
  113. {
  114. hours = 23;
  115. }
  116. else
  117. {
  118. hours--;
  119. }
  120. setHours(hours);
  121. }
  122. uint8_t DS3231::getDay()
  123. {
  124. uint8_t day;
  125. // read day
  126. day = getRegister(DS3231_DAY);
  127. return bcdtodec(day);
  128. }
  129. void DS3231::setDay(uint8_t day)
  130. {
  131. // write day
  132. setRegister(DS3231_DAY, dectobcd(day));
  133. }
  134. uint8_t DS3231::getDate()
  135. {
  136. uint8_t date;
  137. // read date
  138. date = getRegister(DS3231_DATE);
  139. return bcdtodec(date);
  140. }
  141. void DS3231::setDate(uint8_t date)
  142. {
  143. // write date
  144. setRegister(DS3231_DATE, dectobcd(date));
  145. }
  146. void DS3231::incrementDate()
  147. {
  148. uint8_t date = getDate();
  149. date++;
  150. if (date > 31)
  151. {
  152. date = 1;
  153. }
  154. setDate(date);
  155. }
  156. void DS3231::decrementDate()
  157. {
  158. uint8_t date = getDate();
  159. date--;
  160. if (date < 1)
  161. {
  162. date = 31;
  163. }
  164. setDate(date);
  165. }
  166. uint8_t DS3231::getMonth()
  167. {
  168. uint8_t month;
  169. // read month
  170. month = getRegister(DS3231_MONTH);
  171. // read month, ingore century;
  172. return bcdtodec(month & 0x1F);
  173. }
  174. void DS3231::setMonth(uint8_t month)
  175. {
  176. uint8_t century = getCentury();
  177. month = dectobcd(month);
  178. if (century == 1)
  179. {
  180. month = month + 0x80;
  181. }
  182. // write month
  183. setRegister(DS3231_MONTH, month);
  184. }
  185. void DS3231::incrementMonth()
  186. {
  187. uint8_t month = getMonth();
  188. month++;
  189. if (month > 12)
  190. {
  191. month = 1;
  192. }
  193. setMonth(month);
  194. }
  195. void DS3231::decrementMonth()
  196. {
  197. uint8_t month = getMonth();
  198. month--;
  199. if (month < 1)
  200. {
  201. month = 12;
  202. }
  203. setMonth(month);
  204. }
  205. int16_t DS3231::getYear()
  206. {
  207. uint8_t century = getCentury();
  208. int16_t year;
  209. // read year
  210. year = getRegister(DS3231_YEAR);
  211. year = bcdtodec(year);
  212. if (century == 1)
  213. {
  214. year = 2000 + year;
  215. }
  216. else
  217. {
  218. year = 1900 + year;
  219. }
  220. return year;
  221. }
  222. void DS3231::setYear(int16_t year)
  223. {
  224. if (year > 1999)
  225. {
  226. year = year - 2000;
  227. setCentury(0x80);
  228. }
  229. else
  230. {
  231. year = year - 1900;
  232. setCentury(0);
  233. }
  234. setRegister(DS3231_YEAR, dectobcd(year));
  235. }
  236. void DS3231::incrementYear()
  237. {
  238. int16_t year = getYear();
  239. year++;
  240. if (year > 2099)
  241. {
  242. year = 1900;
  243. }
  244. setYear(year);
  245. }
  246. void DS3231::decrementYear()
  247. {
  248. int16_t year = getYear();
  249. year--;
  250. if (year < 1900)
  251. {
  252. year = 2099;
  253. }
  254. setYear(year);
  255. }
  256. uint8_t DS3231::getControlRegister()
  257. {
  258. return bcdtodec(getRegister(DS3231_CONTROL));
  259. }
  260. void DS3231::setControlRegister(uint8_t value)
  261. {
  262. setRegister(DS3231_CONTROL, dectobcd(value));
  263. }
  264. uint8_t DS3231::getStatusRegister()
  265. {
  266. return bcdtodec(getRegister(DS3231_STATUS));
  267. }
  268. void DS3231::setStatusRegister(uint8_t value)
  269. {
  270. setRegister(DS3231_STATUS, dectobcd(value));
  271. }
  272. uint8_t DS3231::getTemperature()
  273. {
  274. int8_t msb_temp;
  275. uint8_t msb_value = getRegister(DS3231_MSB_TEMP);
  276. uint8_t lsb_temp = getRegister(DS3231_LSB_TEMP) >> 6;
  277. float result;
  278. if ((msb_value & 0x80) != 0)
  279. {
  280. msb_temp = msb_value | ~((1 << 8) - 1);
  281. }
  282. else
  283. {
  284. msb_temp = msb_value;
  285. }
  286. result = 0.25 * lsb_temp + msb_temp;
  287. return result;
  288. }
  289. uint8_t DS3231::getRegister(uint8_t address)
  290. {
  291. uint8_t value;
  292. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  293. Wire.write(address);
  294. Wire.endTransmission();
  295. // request 1 byte
  296. Wire.requestFrom(DS3231_I2C_ADDRESS, 1);
  297. // read value
  298. value = Wire.read();
  299. return value;
  300. }
  301. void DS3231::setRegister(uint8_t address, uint8_t value)
  302. {
  303. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  304. // start at address
  305. Wire.write(address);
  306. // write value
  307. Wire.write(value);
  308. Wire.endTransmission();
  309. }
  310. uint8_t DS3231::dectobcd(uint8_t value)
  311. {
  312. return ((value / 10 * 16) + (value % 10));
  313. }
  314. uint8_t DS3231::bcdtodec(uint8_t value)
  315. {
  316. return ((value / 16 * 10) + (value % 16));
  317. }
  318. uint8_t DS3231::getCentury()
  319. {
  320. uint8_t century = 0;
  321. uint8_t month = getRegister(DS3231_MONTH);
  322. // read century from month
  323. century = (month & 0x80) >> 7;
  324. return century;
  325. }
  326. void DS3231::setCentury(uint8_t century)
  327. {
  328. uint8_t month = getMonth();
  329. month = month + century;
  330. setRegister(DS3231_MONTH, month);
  331. }