DS3231_Serial_Hard.pde 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // DS3231_Serial_Hard
  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. // retrieve time- and date-data for you to manipulate.
  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. // Init a Time-data structure
  34. Time t;
  35. void setup()
  36. {
  37. // Setup Serial connection
  38. Serial.begin(115200);
  39. // Initialize the rtc object
  40. rtc.begin();
  41. // The following lines can be uncommented to set the date and time
  42. //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY
  43. //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
  44. //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014
  45. }
  46. void loop()
  47. {
  48. // Get data from the DS3231
  49. t = rtc.getTime();
  50. // Send date over serial connection
  51. Serial.print("Today is the ");
  52. Serial.print(t.date, DEC);
  53. Serial.print(". day of ");
  54. Serial.print(rtc.getMonthStr());
  55. Serial.print(" in the year ");
  56. Serial.print(t.year, DEC);
  57. Serial.println(".");
  58. // Send Day-of-Week and time
  59. Serial.print("It is the ");
  60. Serial.print(t.dow, DEC);
  61. Serial.print(". day of the week (counting monday as the 1th), and it has passed ");
  62. Serial.print(t.hour, DEC);
  63. Serial.print(" hour(s), ");
  64. Serial.print(t.min, DEC);
  65. Serial.print(" minute(s) and ");
  66. Serial.print(t.sec, DEC);
  67. Serial.println(" second(s) since midnight.");
  68. // Send a divider for readability
  69. Serial.println(" - - - - - - - - - - - - - - - - - - - - -");
  70. // Wait one second before repeating :)
  71. delay (1000);
  72. }