Guest User

Untitled

a guest
Sep 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Time.h>
  3. #include <DS1307RTC.h>
  4.  
  5. const char *monthName[12] = {
  6. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  7. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  8. };
  9.  
  10. tmElements_t tm;
  11.  
  12. void setup() {
  13. bool parse=false;
  14. bool config=false;
  15.  
  16. // get the date and time the compiler was run
  17. if (getDate(__DATE__) && getTime(__TIME__)) {
  18. parse = true;
  19. // and configure the RTC with this info
  20. if (RTC.write(tm)) {
  21. config = true;
  22. }
  23. }
  24.  
  25. Serial.begin(9600);
  26. while (!Serial) ; // wait for Arduino Serial Monitor
  27. delay(200);
  28. if (parse && config) {
  29. Serial.print("DS1307 configured Time=");
  30. Serial.print(__TIME__);
  31. Serial.print(", Date=");
  32. Serial.println(__DATE__);
  33. } else if (parse) {
  34. Serial.println("DS1307 Communication Error :-{");
  35. Serial.println("Please check your circuitry");
  36. } else {
  37. Serial.print("Could not parse info from the compiler, Time=\"");
  38. Serial.print(__TIME__);
  39. Serial.print("\", Date=\"");
  40. Serial.print(__DATE__);
  41. Serial.println("\"");
  42. }
  43. }
  44.  
  45. void loop() {
  46. }
  47.  
  48. bool getTime(const char *str)
  49. {
  50. int Hour, Min, Sec;
  51.  
  52. if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  53. tm.Hour = Hour;
  54. tm.Minute = Min;
  55. tm.Second = Sec;
  56. return true;
  57. }
  58.  
  59. bool getDate(const char *str)
  60. {
  61. char Month[12];
  62. int Day, Year;
  63. uint8_t monthIndex;
  64.  
  65. if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  66. for (monthIndex = 0; monthIndex < 12; monthIndex++) {
  67. if (strcmp(Month, monthName[monthIndex]) == 0) break;
  68. }
  69. if (monthIndex >= 12) return false;
  70. tm.Day = Day;
  71. tm.Month = monthIndex + 1;
  72. tm.Year = CalendarYrToTm(Year);
  73. return true;
  74. }
  75. [출처] [IoT-MODLINK] RTC모듈을 이용한 실시간 시간확인! (IoT 길라잡이) |작성자 CHULMING
Add Comment
Please, Sign In to add comment