Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.72 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. //V1 - original release
  4. //V2 - added new setting menu/gui
  5. // added 12/24 hour operation
  6. // changed minimum settable year to 2013
  7. // added feature to quickly reset seconds to '00' by pressing the down button
  8. //V2.1 added temperature to display screen
  9. //V2.2 added li-ion battery percentage
  10. #include <Wire.h>
  11. #include <LiquidCrystal.h>
  12. #include <EEPROM.h>
  13. #include <OneWire.h>
  14. #include <DallasTemperature.h>
  15.  
  16. #define DS3231_I2C_ADDRESS 0x68
  17. #define DS3231_TEMPERATURE_ADDR 0x11
  18. #define ONE_WIRE_BUS 12
  19.  
  20. LiquidCrystal lcd(11, 10, 9, 8, 7, 6);
  21. //LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  22. // Setup a oneWire instance to communicate with any OneWire devices
  23. // (not just Maxim/Dallas temperature ICs)
  24. OneWire oneWire(ONE_WIRE_BUS);
  25.  
  26. int seconds;
  27. int minutes;
  28. int hours;
  29. int adjHours;
  30. int dayOfWeek;
  31. int dayOfMonth;
  32. int month;
  33. int year;
  34.  
  35. //int farenheit;
  36.  
  37. int nextButton = 2;
  38. int upButton = 3;
  39. int downButton = 4;
  40.  
  41. int buttonCount = 0; //start at zero starts clock in run mode
  42.  
  43. bool display12HrMode;
  44. int address = 0;
  45. byte savedMode;
  46.  
  47.  
  48. ///////////////////////// setup portion temerature sensor D18B20
  49.  
  50. // Pass our oneWire reference to Dallas Temperature.
  51. DallasTemperature sensors(&oneWire);
  52.  
  53. //blink led when battery low
  54. // constants won't change. Used here to set a pin number :
  55. const int ledPin = 13; // the number of the LED pin
  56.  
  57. // Variables will change :
  58. int ledState = LOW; // ledState used to set the LED
  59.  
  60. // Generally, you should use "unsigned long" for variables that hold time
  61. // The value will quickly become too large for an int to store
  62. unsigned long previousMillis = 0; // will store last time device was updated
  63.  
  64. // constants won't change :
  65. const long interval = 1000; // interval at which to run timed code
  66.  
  67. void lcdDisplaytemp()
  68. {
  69. int tempC = DS3231_get_treg(); // Reads the temperature as an int, to save memory
  70. int tempF = (tempC * 1.72 + 32); // changed multiplier from 1.8 to 1.65 to try and calibrate for fahrenheit
  71.  
  72. // lcd.print(tempC); // print centigrade to lcd
  73. // lcd.print("C ");
  74. lcd.print(tempF); //print fahrenheit to lcd
  75. lcd.print("F ");
  76.  
  77. }
  78.  
  79. float DS3231_get_treg()
  80. {
  81. int rv; // Reads the temperature as an int, to save memory
  82. uint8_t temp_msb, temp_lsb;
  83. int8_t nint;
  84.  
  85. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  86. Wire.write(DS3231_TEMPERATURE_ADDR);
  87. Wire.endTransmission();
  88.  
  89. Wire.requestFrom(DS3231_I2C_ADDRESS, 2);
  90. temp_msb = Wire.read();
  91. temp_lsb = Wire.read() >> 6;
  92.  
  93. if ((temp_msb & 0x80) != 0)
  94. nint = temp_msb | ~((1 << 8) - 1); // if negative get two's complement
  95. else
  96. nint = temp_msb;
  97.  
  98. rv = 0.25 * temp_lsb + nint;
  99.  
  100. return rv;
  101. }
  102.  
  103.  
  104. void setup()
  105. {
  106.  
  107. analogWrite(5,255);// Power on backlight by default
  108. // analogWrite(17,255); //Power for RTC // only for version 1.0
  109. pinMode (nextButton, INPUT);
  110. pinMode (upButton, INPUT);
  111. pinMode (downButton, INPUT);
  112. pinMode(ledPin, OUTPUT); // set the digital pin as output
  113. Wire.begin();
  114. lcd.begin(16, 2);
  115.  
  116.  
  117. ////////////////////////////////
  118. /* force time setting:
  119. seconds = 00;
  120. minutes = 00;
  121. hours = 00;
  122. dayOfWeek = 1;
  123. dayOfMonth = 01;
  124. month = 01;
  125. year = 13;
  126. initChrono();//just set the time once on your RTC
  127. */
  128. ///////////////////////////////
  129.  
  130. lcd.begin(16, 2); // tells Arduino the LCD dimensions
  131.  
  132. //read 12/24 hr setting from EEPROM
  133. savedMode = EEPROM.read(address);
  134. if(savedMode == 0) { //0 = 12 hr mode //1 = 24 hr mode
  135. display12HrMode = true; //savedMode value stored in EEPRMOM is 0 , so set clock to display 12 hr mode
  136. Serial.print("time mode is 12 hr mode");
  137. }
  138. else {
  139. display12HrMode = true; //savedMode value stored in EEPRMOM is 1 , so set clock to display 24 hr mode
  140. Serial.print("time mode is 24 hr mode");
  141. }
  142.  
  143. ////////////////////////////////////Temp Sensor
  144. // start serial port
  145. Serial.begin(9600);
  146. Serial.println("Dallas Temperature IC Control Library Demo");
  147.  
  148. // Start up the library
  149. sensors.begin();
  150.  
  151.  
  152.  
  153. ////////////////////////////////////////////////
  154.  
  155. }
  156.  
  157.  
  158. void loop()
  159. {
  160. //poll the DS1307 for the date and time
  161. get_time();
  162. get_date();
  163.  
  164. // lcd.clear(); // clear LCD screen //do I need this? is this causing the 'blink'?
  165. lcd.setCursor(0,0);
  166.  
  167. /////////////////////////////////////////////////////
  168.  
  169. //display set menus if nextButton is pressed
  170. if (digitalRead(nextButton) == HIGH) {
  171.  
  172. buttonCount = buttonCount + 1;
  173.  
  174. if (buttonCount == 8) {
  175. buttonCount = 0;
  176. }
  177. delay(100);
  178. }
  179.  
  180.  
  181.  
  182. if(buttonCount == 0) {
  183. //first line - TIME
  184. if (display12HrMode == false) { //display 24 hour format
  185.  
  186. //display hours
  187. if(hours < 10) {
  188. lcd.print("0");
  189. }
  190. else {
  191. lcd.print(" ");
  192. }
  193.  
  194. lcd.print(hours);
  195. lcd.print(":");
  196.  
  197. //display minutes
  198. if(minutes < 10)
  199. {
  200. lcd.print("0");
  201. }
  202. lcd.print(minutes);
  203. lcd.print(":");
  204.  
  205. //display seconds
  206. if(seconds < 10) {
  207. lcd.print("0");
  208. }
  209. lcd.print(seconds);
  210. }
  211. else { //display 12 hour format
  212.  
  213. //display hours
  214. switch(hours){
  215. case 0:
  216. lcd.print("12");
  217. break;
  218. case 1:
  219. lcd.print(" 1");
  220. break;
  221. case 2:
  222. lcd.print(" 2");
  223. break;
  224. case 3:
  225. lcd.print(" 3");
  226. break;
  227. case 4:
  228. lcd.print(" 4");
  229. break;
  230. case 5:
  231. lcd.print(" 5");
  232. break;
  233. case 6:
  234. lcd.print(" 6");
  235. break;
  236. case 7:
  237. lcd.print(" 7");
  238. break;
  239. case 8:
  240. lcd.print(" 8");
  241. break;
  242. case 9:
  243. lcd.print(" 9");
  244. break;
  245. case 10:
  246. lcd.print("10");
  247. break;
  248. case 11:
  249. lcd.print("11");
  250. break;
  251. case 12:
  252. lcd.print("12");
  253. break;
  254. case 13:
  255. lcd.print(" 1");
  256. break;
  257. case 14:
  258. lcd.print(" 2");
  259. break;
  260. case 15:
  261. lcd.print(" 3");
  262. break;
  263. case 16:
  264. lcd.print(" 4");
  265. break;
  266. case 17:
  267. lcd.print(" 5");
  268. break;
  269. case 18:
  270. lcd.print(" 6");
  271. break;
  272. case 19:
  273. lcd.print(" 7");
  274. break;
  275. case 20:
  276. lcd.print(" 8");
  277. break;
  278. case 21:
  279. lcd.print(" 9");
  280. break;
  281. case 22:
  282. lcd.print("10");
  283. break;
  284. case 23:
  285. lcd.print("11");
  286. break;
  287. }
  288.  
  289. lcd.print(":");
  290.  
  291. //display minutes
  292. if(minutes < 10)
  293. {
  294. lcd.print("0");
  295. }
  296. lcd.print(minutes);
  297. lcd.print(":");
  298.  
  299. //display seconds
  300. if(seconds < 10) {
  301. lcd.print("0");
  302. }
  303. lcd.print(seconds);
  304.  
  305. //display AM/PM
  306. if(hours < 12) {
  307. lcd.print(" AM ");
  308. }
  309. else
  310. {
  311. lcd.print(" PM ");
  312. }
  313. }
  314. //Turn the display backlight off between the hours of 1am and 7am
  315.  
  316. if (hours >= 1 && hours < 7)
  317. {
  318. analogWrite(9,0);
  319. }
  320. else
  321. {
  322. analogWrite(9,250);
  323. }
  324.  
  325. lcdDisplaytemp();
  326.  
  327. /*
  328. /////////////////////////////////////////////////////Temp sensor
  329. // call sensors.requestTemperatures() to issue a global temperature
  330. // request to all devices on the bus
  331.  
  332. // unsigned long currentMillis = millis();
  333. unsigned long currentMillis = millis();
  334. if(currentMillis - previousMillis > interval) {
  335.  
  336. previousMillis = currentMillis;
  337.  
  338.  
  339.  
  340. double celsius = sensors.getTempCByIndex(0);
  341. int farenheit = 1.8 * celsius + 32;
  342.  
  343. Serial.print(" Requesting temperatures...");
  344. sensors.requestTemperatures(); // Send the command to get temperatures
  345. Serial.println("DONE");
  346.  
  347. Serial.print("Temperature is: ");
  348. // Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
  349. Serial.print(farenheit);
  350. lcd.print("");
  351. lcd.print(farenheit);
  352. lcd.print("F ");
  353. }
  354. */
  355.  
  356. // delay(200);
  357. ////////////////////////////////////////////////////
  358. //second line - date
  359. //display day of week
  360. lcd.setCursor(2, 1);
  361. switch(dayOfWeek){
  362. case 1:
  363. lcd.print("Sun");
  364. break;
  365. case 2:
  366. lcd.print("Mon");
  367. break;
  368. case 3:
  369. lcd.print("Tue");
  370. break;
  371. case 4:
  372. lcd.print("Wed");
  373. break;
  374. case 5:
  375. lcd.print("Thu");
  376. break;
  377. case 6:
  378. lcd.print("Fri");
  379. break;
  380. case 7:
  381. lcd.print("Sat");
  382. break;
  383. }
  384.  
  385. lcd.print(" ");
  386.  
  387. //display month
  388. if(month < 10){
  389. lcd.print("0");
  390. }
  391.  
  392. lcd.print(month);
  393. lcd.print("/");
  394. if(dayOfMonth < 10) {
  395. lcd.print("0");
  396. }
  397.  
  398. lcd.print(dayOfMonth);
  399. lcd.print("/");
  400.  
  401. //display year
  402. if(year < 10){
  403. lcd.print("0");
  404. }
  405. lcd.print(year);
  406.  
  407.  
  408. }
  409.  
  410.  
  411. //hours
  412. if (buttonCount == 1) {
  413. lcd.clear();
  414. lcd.setCursor(0,0);
  415. lcd.print("HOURS:");
  416. lcd.setCursor(0,1);
  417.  
  418. if (display12HrMode == false) { //display 24 hour format
  419. if (hours < 10) {
  420. lcd.print("0");
  421. }
  422. lcd.print(hours);
  423. }
  424.  
  425. else { //in 12 hr mode
  426. switch(hours){
  427. case 0:
  428. lcd.print("12 AM");
  429. break;
  430. case 1:
  431. lcd.print("1 AM");
  432. break;
  433. case 2:
  434. lcd.print("2 AM");
  435. break;
  436. case 3:
  437. lcd.print("3 AM");
  438. break;
  439. case 4:
  440. lcd.print("4 AM");
  441. break;
  442. case 5:
  443. lcd.print("5 AM");
  444. break;
  445. case 6:
  446. lcd.print("6 AM");
  447. break;
  448. case 7:
  449. lcd.print("7 AM");
  450. break;
  451. case 8:
  452. lcd.print("8 AM");
  453. break;
  454. case 9:
  455. lcd.print("9 AM");
  456. break;
  457. case 10:
  458. lcd.print("10 AM");
  459. break;
  460. case 11:
  461. lcd.print("11 AM");
  462. break;
  463. case 12:
  464. lcd.print("12 PM");
  465. break;
  466. case 13:
  467. lcd.print("1 PM");
  468. break;
  469. case 14:
  470. lcd.print("2 PM");
  471. break;
  472. case 15:
  473. lcd.print("3 PM");
  474. break;
  475. case 16:
  476. lcd.print("4 PM");
  477. break;
  478. case 17:
  479. lcd.print("5 PM");
  480. break;
  481. case 18:
  482. lcd.print("6 PM");
  483. break;
  484. case 19:
  485. lcd.print("7 PM");
  486. break;
  487. case 20:
  488. lcd.print("8 PM");
  489. break;
  490. case 21:
  491. lcd.print("9 PM");
  492. break;
  493. case 22:
  494. lcd.print("10 PM");
  495. break;
  496. case 23:
  497. lcd.print("11 PM");
  498. break;
  499. }
  500. }
  501.  
  502. delay(200);
  503. }
  504. //mins
  505. if (buttonCount == 2) {
  506. lcd.clear();
  507. lcd.setCursor(0,0);
  508. lcd.print("MINUTES:");
  509. lcd.setCursor(0,1);
  510. if(minutes < 10) {
  511. lcd.print("0");
  512. }
  513. lcd.print(minutes);
  514. delay(200);
  515. }
  516. //day of week
  517. if (buttonCount == 3) {
  518. lcd.clear();
  519. lcd.setCursor(0,0);
  520. lcd.print("DAY OF WEEK:");
  521. lcd.setCursor(0,1);
  522. switch(dayOfWeek){
  523. case 1:
  524. lcd.print("Sun");
  525. break;
  526. case 2:
  527. lcd.print("Mon");
  528. break;
  529. case 3:
  530. lcd.print("Tue");
  531. break;
  532. case 4:
  533. lcd.print("Wed");
  534. break;
  535. case 5:
  536. lcd.print("Thu");
  537. break;
  538. case 6:
  539. lcd.print("Fri");
  540. break;
  541. case 7:
  542. lcd.print("Sat");
  543. break;
  544. }
  545. delay(200);
  546. }
  547. //month
  548. if (buttonCount == 4) {
  549. lcd.clear();
  550. lcd.setCursor(0,0);
  551. lcd.print("MONTH:");
  552. lcd.setCursor(0,1);
  553. if(month < 10) {
  554. lcd.print("0");
  555. }
  556. lcd.print(month);
  557. delay(200);
  558. }
  559. //day of month
  560. if (buttonCount == 5) {
  561. lcd.clear();
  562. lcd.setCursor(0,0);
  563. lcd.print("DAY OF MONTH:");
  564. lcd.setCursor(0,1);
  565. if(dayOfMonth < 10) {
  566. lcd.print("0");
  567. }
  568. lcd.print(dayOfMonth);
  569. delay(200);
  570. }
  571. //year
  572. if (buttonCount == 6) {
  573. lcd.clear();
  574. lcd.setCursor(0,0);
  575. lcd.print("YEAR:");
  576. lcd.setCursor(0,1);
  577. lcd.print("20");
  578. lcd.print(year);
  579. delay(200);
  580. }
  581. if (buttonCount == 7)
  582. {
  583. lcd.clear();
  584. lcd.setCursor(0,0);
  585. lcd.print("Backlight Toggle");
  586. delay(200);
  587. }
  588.  
  589.  
  590. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  591. //pressing 'up' button should increment field being set
  592. //hours
  593. if ((digitalRead(upButton) == HIGH) && (buttonCount == 1)) {
  594.  
  595. hours++;
  596. if (hours > 23) hours = 0;
  597.  
  598. //send update to RTC
  599. set_time();
  600. }
  601.  
  602. //mins
  603. else if ((digitalRead(upButton) == HIGH) && (buttonCount == 2)) {
  604.  
  605. minutes++;
  606. if (minutes > 59) minutes = 0;
  607.  
  608. //reset seconds to '0'
  609. seconds = 0;
  610.  
  611. //send update to RTC
  612. set_time();
  613. }
  614.  
  615. //day of week
  616. else if ((digitalRead(upButton) == HIGH) && (buttonCount == 3)) {
  617.  
  618. dayOfWeek++;
  619. if(dayOfWeek > 7) dayOfWeek = 1;
  620.  
  621. //send update to RTC
  622. set_date();
  623. }
  624.  
  625. //month
  626. else if ((digitalRead(upButton) == HIGH) && (buttonCount == 4)) {
  627.  
  628. month++;
  629. if (month > 12) month = 1;
  630.  
  631. //send update to RTC
  632. set_date();
  633. }
  634.  
  635. //day of month
  636. else if ((digitalRead(upButton) == HIGH) && (buttonCount == 5)) {
  637.  
  638. dayOfMonth++;
  639.  
  640. //if feb
  641. if (month == 2) {
  642. if (dayOfMonth > 28) dayOfMonth = 1;
  643. }
  644.  
  645. //if leap year
  646. //still to do
  647.  
  648. //if month has 30 days: Apr, Jun, Sep, Nov
  649. if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
  650. if (dayOfMonth > 30) dayOfMonth = 1;
  651. }
  652.  
  653. //if month has 31 days: Jan, Mar, May, Jul, Aug, Oct, Dec
  654. if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10)) {
  655. if (dayOfMonth > 31) dayOfMonth = 1;
  656. }
  657.  
  658. //send update to RTC
  659. set_date();
  660. }
  661.  
  662. //year
  663. else if ((digitalRead(upButton) == HIGH) && (buttonCount == 6)) {
  664.  
  665. year++;
  666.  
  667. //send update to RTC
  668. set_date();
  669. }
  670. else if ((digitalRead(upButton) == HIGH) && (buttonCount == 7))
  671. {
  672. analogWrite(9,255);
  673. }
  674.  
  675. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  676. //pressing 'down' button should decrement field being set
  677. //hours
  678. if ((digitalRead(downButton) == HIGH) && (buttonCount == 1)) {
  679.  
  680. if (hours == 0) hours = 23;
  681. else if ((hours > 0) && (hours <= 23)) hours--;
  682.  
  683. //send update to RTC
  684. set_time();
  685. }
  686.  
  687. //mins
  688. else if ((digitalRead(downButton) == HIGH) && (buttonCount == 2)) {
  689.  
  690. if (minutes == 0) minutes = 59;
  691. else if ((minutes > 0) && (minutes <= 59)) minutes--;
  692.  
  693. //reset seconds to '0'
  694. seconds = 0;
  695.  
  696. //send update to RTC
  697. set_time();
  698. }
  699.  
  700. //day of week
  701. else if ((digitalRead(downButton) == HIGH) && (buttonCount == 3)) {
  702.  
  703. dayOfWeek--;
  704. if(dayOfWeek < 1 ) dayOfWeek = 7;
  705.  
  706. //send update to RTC
  707. set_date();
  708. }
  709.  
  710. //month
  711. else if ((digitalRead(downButton) == HIGH) && (buttonCount == 4)) {
  712.  
  713. month--;
  714. if (month < 1) month = 12;
  715.  
  716. //send update to RTC
  717. set_date();
  718. }
  719.  
  720. //day of month
  721. else if ((digitalRead(downButton) == HIGH) && (buttonCount == 5)) {
  722.  
  723. dayOfMonth--;
  724.  
  725. //if feb
  726. if (month == 2) {
  727. if (dayOfMonth < 1) dayOfMonth = 28;
  728. }
  729.  
  730. //if leap year
  731. //still to do
  732.  
  733. //if month has 30 days: Apr, Jun, Sep, Nov
  734. if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
  735. if (dayOfMonth < 1) dayOfMonth = 30;
  736. }
  737.  
  738. //if month has 31 days: Jan, Mar, May, Jul, Aug, Oct, Dec
  739. if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10)) {
  740. if (dayOfMonth < 1) dayOfMonth = 31;
  741. }
  742.  
  743. //send update to RTC
  744. set_date();
  745. }
  746.  
  747. //year
  748. else if ((digitalRead(downButton) == HIGH) && (buttonCount == 6)) {
  749.  
  750. year--;
  751. if (year < 13) year = 13;
  752.  
  753. //send update to RTC
  754. set_date();
  755. }
  756. else if ((digitalRead(downButton) == HIGH) && (buttonCount == 7))
  757. {
  758. analogWrite(9,0);
  759. }
  760.  
  761. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  762. //button error handling
  763. if ((digitalRead(upButton) == HIGH) && (digitalRead(downButton) == HIGH) && (buttonCount != 0)) {
  764.  
  765. lcd.setCursor(0,1);
  766. lcd.print("BOOM! ");
  767.  
  768. delay(500);
  769. }
  770.  
  771.  
  772. ////////////////////////////////////////
  773. //set seconds to '00' shortcut
  774. if ((digitalRead(downButton) == HIGH) && (buttonCount == 0)) {
  775.  
  776. //reset seconds to '0'
  777. seconds = 0;
  778.  
  779. //send update to RTC
  780. set_time();
  781. }
  782.  
  783.  
  784. //toggle 12/24 mode
  785. if ((digitalRead(upButton) == HIGH) && (buttonCount == 0)) {
  786.  
  787. //toggle 12/24 mode
  788. display12HrMode = !display12HrMode;
  789.  
  790. if (display12HrMode == false) {
  791.  
  792. savedMode = 1; //24 hr mode
  793. //send update to EEPROM for 24 hr mode
  794. EEPROM.write(address, savedMode); //this sets the savedMode in EEPROM to true //24 hr mode
  795.  
  796. }
  797.  
  798. else {
  799. savedMode = 0; //12 hr mode
  800. //send update to EEPROM for 12 hour mode
  801. EEPROM.write(address, savedMode); //this sets the savedMode in EEPROM to fasle //12 hr mode
  802.  
  803. }
  804. }
  805.  
  806.  
  807. } //end of loop
  808.  
  809. /////////////
  810. //DS3231 RTC interface
  811. void initChrono()
  812. {
  813. set_time();
  814. set_date();
  815. }
  816.  
  817. void set_date()
  818. {
  819. Wire.beginTransmission(104);
  820. Wire.write(3);
  821. Wire.write(decToBcd(dayOfWeek));
  822. Wire.write(decToBcd(dayOfMonth));
  823. Wire.write(decToBcd(month));
  824. Wire.write(decToBcd(year));
  825. Wire.endTransmission();
  826. }
  827.  
  828. void get_date()
  829. {
  830. Wire.beginTransmission(104);
  831. Wire.write(3);//set register to 3 (day)
  832. Wire.endTransmission();
  833. Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
  834. dayOfWeek = bcdToDec(Wire.read());
  835. dayOfMonth = bcdToDec(Wire.read());
  836. month = bcdToDec(Wire.read());
  837. year = bcdToDec(Wire.read());
  838. }
  839.  
  840. void set_time()
  841. {
  842. Wire.beginTransmission(104);
  843. Wire.write(0);
  844. Wire.write(decToBcd(seconds));
  845. Wire.write(decToBcd(minutes));
  846. Wire.write(decToBcd(hours));
  847. //Wire.write(DS3231_TEMPERATURE_ADDR); //get temperature
  848. Wire.endTransmission();
  849. }
  850.  
  851. void get_time()
  852. {
  853. Wire.beginTransmission(104);
  854. Wire.write(0);//set register to 0
  855. Wire.endTransmission();
  856. Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
  857. seconds = bcdToDec(Wire.read() & 0x7f);
  858. minutes = bcdToDec(Wire.read());
  859. hours = bcdToDec(Wire.read() & 0x3f);
  860. }
  861.  
  862. ///////////////////////////////////////////////////////////////////////
  863.  
  864. byte decToBcd(byte val)
  865. {
  866. return ( (val/10*16) + (val%10) );
  867. }
  868.  
  869. byte bcdToDec(byte val)
  870. {
  871. return ( (val/16*10) + (val%16) );
  872. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement