PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

SetTime.ino 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <Wire.h>
  2. #include <TimeLib.h>
  3. #include <DS1307RTC.h>
  4. const char *monthName[12] = {
  5. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  6. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  7. };
  8. tmElements_t tm;
  9. void setup() {
  10. bool parse=false;
  11. bool config=false;
  12. // get the date and time the compiler was run
  13. if (getDate(__DATE__) && getTime(__TIME__)) {
  14. parse = true;
  15. // and configure the RTC with this info
  16. if (RTC.write(tm)) {
  17. config = true;
  18. }
  19. }
  20. Serial.begin(9600);
  21. while (!Serial) ; // wait for Arduino Serial Monitor
  22. delay(200);
  23. if (parse && config) {
  24. Serial.print("DS1307 configured Time=");
  25. Serial.print(__TIME__);
  26. Serial.print(", Date=");
  27. Serial.println(__DATE__);
  28. } else if (parse) {
  29. Serial.println("DS1307 Communication Error :-{");
  30. Serial.println("Please check your circuitry");
  31. } else {
  32. Serial.print("Could not parse info from the compiler, Time=\"");
  33. Serial.print(__TIME__);
  34. Serial.print("\", Date=\"");
  35. Serial.print(__DATE__);
  36. Serial.println("\"");
  37. }
  38. }
  39. void loop() {
  40. }
  41. bool getTime(const char *str)
  42. {
  43. int Hour, Min, Sec;
  44. if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  45. tm.Hour = Hour;
  46. tm.Minute = Min;
  47. tm.Second = Sec;
  48. return true;
  49. }
  50. bool getDate(const char *str)
  51. {
  52. char Month[12];
  53. int Day, Year;
  54. uint8_t monthIndex;
  55. if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  56. for (monthIndex = 0; monthIndex < 12; monthIndex++) {
  57. if (strcmp(Month, monthName[monthIndex]) == 0) break;
  58. }
  59. if (monthIndex >= 12) return false;
  60. tm.Day = Day;
  61. tm.Month = monthIndex + 1;
  62. tm.Year = CalendarYrToTm(Year);
  63. return true;
  64. }