uuu000

MD_DS1307_LCD_Time

Sep 27th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #include <Wire.h>
  5. #include <Time.h>
  6. #include <DS1307RTC.h>
  7.  
  8. const char *monthName[12] = {
  9. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  10. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  11. };
  12.  
  13. tmElements_t tm;
  14.  
  15. void setup()
  16. {
  17. bool parse=false;
  18. bool config=false;
  19.  
  20. // получаем дату и время на момент компиляции
  21. if (getDate(__DATE__) && getTime(__TIME__))
  22. {
  23. parse = true;
  24. // и конфигурируем RTC используя эту информацию
  25. if (RTC.write(tm))
  26. {
  27. config = true;
  28. }
  29. }
  30.  
  31. Serial.begin(9600);
  32. while (!Serial) ; // ожидаем ответа порта
  33. delay(200);
  34. if (parse && config)
  35. {
  36. Serial.print("DS1307 configured Time=");
  37. Serial.print(__TIME__);
  38. Serial.print(", Date=");
  39. Serial.println(__DATE__);
  40. }
  41. else if (parse)
  42. {
  43. Serial.println("DS1307 Communication Error :-{");
  44. Serial.println("Please check your circuitry");
  45. }
  46. else
  47. {
  48. Serial.print("Could not parse info from the compiler, Time=\"");
  49. Serial.print(__TIME__);
  50. Serial.print("\", Date=\"");
  51. Serial.print(__DATE__);
  52. Serial.println("\"");
  53. }
  54. }
  55.  
  56. void loop()
  57. {
  58. }
  59.  
  60. bool getTime(const char *str)
  61. {
  62. int Hour, Min, Sec;
  63.  
  64. if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  65. tm.Hour = Hour;
  66. tm.Minute = Min;
  67. tm.Second = Sec;
  68. return true;
  69. }
  70.  
  71. bool getDate(const char *str)
  72. {
  73. char Month[12];
  74. int Day, Year;
  75. uint8_t monthIndex;
  76.  
  77. if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  78. for (monthIndex = 0; monthIndex < 12; monthIndex++)
  79. {
  80. if (strcmp(Month, monthName[monthIndex]) == 0) break;
  81. }
  82. if (monthIndex >= 12) return false;
  83. tm.Day = Day;
  84. tm.Month = monthIndex + 1;
  85. tm.Year = CalendarYrToTm(Year);
  86. return true;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment