PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

72 Zeilen
1.8KB

  1. /*
  2. * TimeRTC.pde
  3. * example code illustrating Time library with Real Time Clock.
  4. *
  5. * This example requires Markus Lange's Arduino Due RTC Library
  6. * https://github.com/MarkusLange/Arduino-Due-RTC-Library
  7. */
  8. #include <TimeLib.h>
  9. #include <rtc_clock.h>
  10. // Select the Slowclock source
  11. //RTC_clock rtc_clock(RC);
  12. RTC_clock rtc_clock(XTAL);
  13. void setup() {
  14. Serial.begin(9600);
  15. rtc_clock.init();
  16. if (rtc_clock.date_already_set() == 0) {
  17. // Unfortunately, the Arduino Due hardware does not seem to
  18. // be designed to maintain the RTC clock state when the
  19. // board resets. Markus described it thusly: "Uhh the Due
  20. // does reset with the NRSTB pin. This resets the full chip
  21. // with all backup regions including RTC, RTT and SC. Only
  22. // if the reset is done with the NRST pin will these regions
  23. // stay with their old values."
  24. rtc_clock.set_time(__TIME__);
  25. rtc_clock.set_date(__DATE__);
  26. // However, this might work on other unofficial SAM3X boards
  27. // with different reset circuitry than Arduino Due?
  28. }
  29. setSyncProvider(getArduinoDueTime);
  30. if(timeStatus()!= timeSet)
  31. Serial.println("Unable to sync with the RTC");
  32. else
  33. Serial.println("RTC has set the system time");
  34. }
  35. time_t getArduinoDueTime()
  36. {
  37. return rtc_clock.unixtime();
  38. }
  39. void loop()
  40. {
  41. digitalClockDisplay();
  42. delay(1000);
  43. }
  44. void digitalClockDisplay(){
  45. // digital clock display of the time
  46. Serial.print(hour());
  47. printDigits(minute());
  48. printDigits(second());
  49. Serial.print(" ");
  50. Serial.print(day());
  51. Serial.print(" ");
  52. Serial.print(month());
  53. Serial.print(" ");
  54. Serial.print(year());
  55. Serial.println();
  56. }
  57. void printDigits(int digits){
  58. // utility function for digital clock display: prints preceding colon and leading 0
  59. Serial.print(":");
  60. if(digits < 10)
  61. Serial.print('0');
  62. Serial.print(digits);
  63. }