PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

88 rindas
2.5KB

  1. /*
  2. * TimeGPS.pde
  3. * example code illustrating time synced from a GPS
  4. *
  5. */
  6. #include <TimeLib.h>
  7. #include <TinyGPS.h> // http://arduiniana.org/libraries/TinyGPS/
  8. #include <SoftwareSerial.h>
  9. // TinyGPS and SoftwareSerial libraries are the work of Mikal Hart
  10. SoftwareSerial SerialGPS = SoftwareSerial(10, 11); // receive on pin 10
  11. TinyGPS gps;
  12. // To use a hardware serial port, which is far more efficient than
  13. // SoftwareSerial, uncomment this line and remove SoftwareSerial
  14. //#define SerialGPS Serial1
  15. // Offset hours from gps time (UTC)
  16. const int offset = 1; // Central European Time
  17. //const int offset = -5; // Eastern Standard Time (USA)
  18. //const int offset = -4; // Eastern Daylight Time (USA)
  19. //const int offset = -8; // Pacific Standard Time (USA)
  20. //const int offset = -7; // Pacific Daylight Time (USA)
  21. // Ideally, it should be possible to learn the time zone
  22. // based on the GPS position data. However, that would
  23. // require a complex library, probably incorporating some
  24. // sort of database using Eric Muller's time zone shape
  25. // maps, at http://efele.net/maps/tz/
  26. time_t prevDisplay = 0; // when the digital clock was displayed
  27. void setup()
  28. {
  29. Serial.begin(9600);
  30. while (!Serial) ; // Needed for Leonardo only
  31. SerialGPS.begin(4800);
  32. Serial.println("Waiting for GPS time ... ");
  33. }
  34. void loop()
  35. {
  36. while (SerialGPS.available()) {
  37. if (gps.encode(SerialGPS.read())) { // process gps messages
  38. // when TinyGPS reports new data...
  39. unsigned long age;
  40. int Year;
  41. byte Month, Day, Hour, Minute, Second;
  42. gps.crack_datetime(&Year, &Month, &Day, &Hour, &Minute, &Second, NULL, &age);
  43. if (age < 500) {
  44. // set the Time to the latest GPS reading
  45. setTime(Hour, Minute, Second, Day, Month, Year);
  46. adjustTime(offset * SECS_PER_HOUR);
  47. }
  48. }
  49. }
  50. if (timeStatus()!= timeNotSet) {
  51. if (now() != prevDisplay) { //update the display only if the time has changed
  52. prevDisplay = now();
  53. digitalClockDisplay();
  54. }
  55. }
  56. }
  57. void digitalClockDisplay(){
  58. // digital clock display of the time
  59. Serial.print(hour());
  60. printDigits(minute());
  61. printDigits(second());
  62. Serial.print(" ");
  63. Serial.print(day());
  64. Serial.print(" ");
  65. Serial.print(month());
  66. Serial.print(" ");
  67. Serial.print(year());
  68. Serial.println();
  69. }
  70. void printDigits(int digits) {
  71. // utility function for digital clock display: prints preceding colon and leading 0
  72. Serial.print(":");
  73. if(digits < 10)
  74. Serial.print('0');
  75. Serial.print(digits);
  76. }