DS3231_UnixTime.pde 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // DS3231_UnixTime
  2. // Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
  3. // web: http://www.RinkyDinkElectronics.com/
  4. //
  5. // A quick demo of how to use my DS3231-library to
  6. // convert date and time to UnixTime
  7. //
  8. // To use the hardware I2C (TWI) interface of the chipKit you must connect
  9. // the pins as follows:
  10. //
  11. // chipKit Uno32/uC32:
  12. // ----------------------
  13. // DS3231: SDA pin -> Analog 4
  14. // SCL pin -> Analog 5
  15. // *** Please note that JP6 and JP8 must be in the I2C position (closest to the analog pins)
  16. //
  17. // chipKit Max32:
  18. // ----------------------
  19. // DS3231: SDA pin -> Digital 20 (the pin labeled SDA)
  20. // SCL pin -> Digital 21 (the pin labeled SCL)
  21. //
  22. // The chipKit boards does not have pull-up resistors on the hardware I2C interface
  23. // so external pull-up resistors on the data and clock signals are required.
  24. //
  25. // You can connect the DS3231 to any available pin but if you use any
  26. // other than what is described above the library will fall back to
  27. // a software-based, TWI-like protocol which will require exclusive access
  28. // to the pins used.
  29. //
  30. #include <DS3231.h>
  31. // Init the DS3231 using the hardware interface
  32. DS3231 rtc(SDA, SCL);
  33. Time t;
  34. void setup()
  35. {
  36. // Setup Serial connection
  37. Serial.begin(115200);
  38. // Initialize the rtc object
  39. rtc.begin();
  40. }
  41. void loop()
  42. {
  43. // Send Current time
  44. Serial.print("Current Time.............................: ");
  45. Serial.print(rtc.getDOWStr());
  46. Serial.print(" ");
  47. Serial.print(rtc.getDateStr());
  48. Serial.print(" -- ");
  49. Serial.println(rtc.getTimeStr());
  50. // Send Unixtime
  51. // ** Note that there may be a one second difference between the current time **
  52. // ** and current unixtime show if the second changes between the two calls **
  53. Serial.print("Current Unixtime.........................: ");
  54. Serial.println(rtc.getUnixTime(rtc.getTime()));
  55. // Send Unixtime for 00:00:00 on January 1th 2014
  56. Serial.print("Unixtime for 00:00:00 on January 1th 2014: ");
  57. t.hour = 0;
  58. t.min = 0;
  59. t.sec = 0;
  60. t.year = 2014;
  61. t.mon = 1;
  62. t.date = 1;
  63. Serial.println(rtc.getUnixTime(t));
  64. // Wait indefinitely
  65. while (1) {};
  66. }