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.

490 lines
8.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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 seconds
  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::getAlarm1Seconds()
  257. {
  258. return bcdtodec(getRegister(DS3231_ALARM1_SECONDS));
  259. }
  260. void DS3231::setAlarm1Seconds(uint8_t seonds)
  261. {
  262. setRegister(DS3231_ALARM1_SECONDS, dectobcd(seconds % 60));
  263. }
  264. uint8_t DS3231::getAlarm1Minutes()
  265. {
  266. return bcdtodec(getRegister(DS3231_ALARM1_MINUTES));
  267. }
  268. void DS3231::setAlarm1Minutes(uint8_t minutes)
  269. {
  270. setRegister(DS3231_ALARM1_MINUTES, dectobcd(minutes % 60));
  271. }
  272. uint8_t DS3231::getAlarm1Hours()
  273. {
  274. return bcdtodec(getRegister(DS3231_ALARM1_HOURS));
  275. }
  276. void DS3231::setAlarm1Hours(uint8_t hours)
  277. {
  278. setRegister(DS3231_ALARM1_HOURS, dectobcd(hours % 24));
  279. }
  280. uint8_t DS3231::getAlarm1Date()
  281. {
  282. return bcdtodec(getRegister(DS3231_ALARM1_DATE));
  283. }
  284. void DS3231::setAlarm1Date(uint8_t date)
  285. {
  286. setRegister(DS3231_ALARM1_DATE, dectobcd(data % 31));
  287. }
  288. void DS3231::setAlarm1Mask(uint8_t mask)
  289. {
  290. setRegister(DS3231_ALARM1_SECONDS, dectobcd(getAlarm1Seconds()) | ((mask & 0b00000001) << 7));
  291. setRegister(DS3231_ALARM1_MINUTES, dectobcd(getAlarm1Minutes()) | ((mask & 0b00000010) << 6));
  292. setRegister(DS3231_ALARM1_HOURS, dectobcd(getAlarm1Hours()) | ((mask & 0b00000100) << 5));
  293. setRegister(DS3231_ALARM1_DATE, dectobcd(getAlarm1Date()) | ((mask & 0b00001000) << 4));
  294. }
  295. uint8_t DS3231::getAlarm2Minutes()
  296. {
  297. return bcdtodec(getRegister(DS3231_ALARM2_MINUTES));
  298. }
  299. void DS3231::setAlarm2Minutes(uint8_t minutes)
  300. {
  301. setRegister(DS3231_ALARM2_MINUTES, dectobcd(minutes % 60));
  302. }
  303. uint8_t DS3231::getAlarm2Hours()
  304. {
  305. return bcdtodec(getRegister(DS3231_ALARM2_HOURS));
  306. }
  307. void DS3231::setAlarm2Hours(uint8_t hours)
  308. {
  309. setRegister(DS3231_ALARM2_HOURS, dectobcd(hours % 24));
  310. }
  311. uint8_t DS3231::getAlarm2Date()
  312. {
  313. return bcdtodec(getRegister(DS3231_ALARM2_DATE));
  314. }
  315. void DS3231::setAlarm2Date(uint8_t date)
  316. {
  317. setRegister(DS3231_ALARM2_DATE, dectobcd(date % 31));
  318. }
  319. void DS3231::setAlarm2Mask(uint8_t mask)
  320. {
  321. setRegister(DS3231_ALARM2_MINUTES, dectobcd(getAlarm2Minutes()) | ((mask & 0b00000001) << 7));
  322. setRegister(DS3231_ALARM2_HOURS, dectobcd(getAlarm2Hours()) | ((mask & 0b00000010) << 6));
  323. setRegister(DS3231_ALARM2_DATE, dectobcd(getAlarm2Date()) | ((mask & 0b00000100) << 5));
  324. }
  325. uint8_t DS3231::getControlRegister()
  326. {
  327. return getRegister(DS3231_CONTROL);
  328. }
  329. void DS3231::setControlRegister(uint8_t value)
  330. {
  331. setRegister(DS3231_CONTROL, value);
  332. }
  333. uint8_t DS3231::getStatusRegister()
  334. {
  335. return getRegister(DS3231_STATUS);
  336. }
  337. void DS3231::setStatusRegister(uint8_t value)
  338. {
  339. setRegister(DS3231_STATUS, value);
  340. }
  341. uint8_t DS3231::getTemperature()
  342. {
  343. int8_t msb_temp;
  344. uint8_t msb_value = getRegister(DS3231_MSB_TEMP);
  345. uint8_t lsb_temp = getRegister(DS3231_LSB_TEMP) >> 6;
  346. float result;
  347. if ((msb_value & 0x80) != 0)
  348. {
  349. msb_temp = msb_value | ~((1 << 8) - 1);
  350. }
  351. else
  352. {
  353. msb_temp = msb_value;
  354. }
  355. result = 0.25 * lsb_temp + msb_temp;
  356. return result;
  357. }
  358. uint8_t DS3231::getRegister(uint8_t address)
  359. {
  360. uint8_t value;
  361. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  362. Wire.write(address);
  363. Wire.endTransmission();
  364. // request 1 byte
  365. Wire.requestFrom(DS3231_I2C_ADDRESS, 1);
  366. // read value
  367. value = Wire.read();
  368. return value;
  369. }
  370. void DS3231::setRegister(uint8_t address, uint8_t value)
  371. {
  372. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  373. // start at address
  374. Wire.write(address);
  375. // write value
  376. Wire.write(value);
  377. Wire.endTransmission();
  378. }
  379. uint8_t DS3231::dectobcd(uint8_t value)
  380. {
  381. return ((value / 10 * 16) + (value % 10));
  382. }
  383. uint8_t DS3231::bcdtodec(uint8_t value)
  384. {
  385. return ((value / 16 * 10) + (value % 16));
  386. }
  387. uint8_t DS3231::getCentury()
  388. {
  389. uint8_t century = 0;
  390. uint8_t month = getRegister(DS3231_MONTH);
  391. // read century from month
  392. century = (month & 0x80) >> 7;
  393. return century;
  394. }
  395. void DS3231::setCentury(uint8_t century)
  396. {
  397. uint8_t month = getMonth();
  398. month = month + century;
  399. setRegister(DS3231_MONTH, month);
  400. }