RTC sync with GPS v1.2
This commit is contained in:
@@ -39,8 +39,8 @@ extern bool NewHour; // pass to main ino = Hour ready to print
|
|||||||
extern time_t Loc_t, RTC_t; // pass to main ino = Local timestamp
|
extern time_t Loc_t, RTC_t; // pass to main ino = Local timestamp
|
||||||
extern bool gps_on;
|
extern bool gps_on;
|
||||||
|
|
||||||
extern volatile bool GPS_sec, RTC_sec;
|
extern volatile bool GPS_sec, RTC_sec, RTC_sync;
|
||||||
extern unsigned long RTCMillis, GPSMillis;// call from main ino, GPS_RTC_Clock.h
|
extern unsigned long RTCMicros, GPSMicros;// call from main ino, GPS_RTC_Clock.h
|
||||||
extern LiquidCrystal_I2C lcd;
|
extern LiquidCrystal_I2C lcd;
|
||||||
|
|
||||||
void GPS_RTC_Clock_setup();
|
void GPS_RTC_Clock_setup();
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ void setup() { // the setup function runs once when you
|
|||||||
lcd.clear();
|
lcd.clear();
|
||||||
lcd.backlight();
|
lcd.backlight();
|
||||||
lcd.setCursor(0,0);
|
lcd.setCursor(0,0);
|
||||||
lcd.print("DEBUG[setup()]");
|
lcd.print("RTC sync GPS");
|
||||||
|
lcd.setCursor(0,1);
|
||||||
|
lcd.print("v1.2");
|
||||||
DEBUG_PORT.begin(38400);
|
DEBUG_PORT.begin(38400);
|
||||||
DEBUG_PORT.flush();
|
DEBUG_PORT.flush();
|
||||||
delay(1000);
|
delay(1000);
|
||||||
@@ -87,24 +89,24 @@ void RTCtoLCD() {
|
|||||||
char message[50];
|
char message[50];
|
||||||
|
|
||||||
if (NewSec) {
|
if (NewSec) {
|
||||||
DEBUG_PORT.println("DEBUG[RTCtoLCD] newSec");
|
//DEBUG_PORT.println("DEBUG[RTCtoLCD] newSec");
|
||||||
//DEBUG_PORT.println("DEBUG[RTCtoLCD] RTCMillis: " + String(RTCMillis));
|
//DEBUG_PORT.println("DEBUG[RTCtoLCD] RTCMicros: " + String(RTCMicros));
|
||||||
//DEBUG_PORT.println("DEBUG[RTCtoLCD] RTC_sec: " + String(RTC_sec));
|
//DEBUG_PORT.println("DEBUG[RTCtoLCD] RTC_sec: " + String(RTC_sec));
|
||||||
//DEBUG_PORT.println("DEBUG[RTCtoLCD] millis: " + String(millis()));
|
//DEBUG_PORT.println("DEBUG[RTCtoLCD] millis: " + String(millis()));
|
||||||
//snprintf(segment, sizeof(segment),"%.2u", second(Loc_t));
|
//snprintf(segment, sizeof(segment),"%.2u", second(Loc_t));
|
||||||
snprintf(segment, sizeof(segment),"%.2u", second(RTC_t));
|
snprintf(segment, sizeof(segment),"%.2u", second(RTC_t));
|
||||||
lcd.setCursor(6, 0);
|
lcd.setCursor(14, 0);
|
||||||
lcd.print(segment);
|
lcd.print(segment);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NewMin) {
|
if (NewMin) {
|
||||||
//snprintf(segment, sizeof(segment),"%.2u", minute(Loc_t));
|
//snprintf(segment, sizeof(segment),"%.2u", minute(Loc_t));
|
||||||
snprintf(segment, sizeof(segment),"%.2u", minute(RTC_t));
|
snprintf(segment, sizeof(segment),"%.2u", minute(RTC_t));
|
||||||
lcd.setCursor(3, 0);
|
lcd.setCursor(11, 0);
|
||||||
lcd.print(segment);
|
lcd.print(segment);
|
||||||
//snprintf(segment, sizeof(segment),"%.2u", hour(Loc_t));
|
//snprintf(segment, sizeof(segment),"%.2u", hour(Loc_t));
|
||||||
snprintf(segment, sizeof(segment),"%.2u", hour(RTC_t));
|
snprintf(segment, sizeof(segment),"%.2u", hour(RTC_t));
|
||||||
lcd.setCursor(0, 0);
|
lcd.setCursor(8, 0);
|
||||||
lcd.print(segment);
|
lcd.print(segment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
58
GPS_com.cpp
58
GPS_com.cpp
@@ -23,12 +23,13 @@ static bool gps_on; // flag for GPS ON/OFF
|
|||||||
static int pulse_count = 0;
|
static int pulse_count = 0;
|
||||||
static time_t gps_seconds_t = 0; // GPS time
|
static time_t gps_seconds_t = 0; // GPS time
|
||||||
static byte gps_sats = 0;
|
static byte gps_sats = 0;
|
||||||
|
static bool GPScolon = false;
|
||||||
volatile bool GPS_sec = false; // flag for GPS-PPS
|
volatile bool GPS_sec = false; // flag for GPS-PPS
|
||||||
unsigned long GPSMillis;
|
unsigned long GPSMicros;
|
||||||
|
|
||||||
// set interrupt flag
|
// set interrupt flag
|
||||||
void gps_interrupt() {
|
void gps_interrupt() {
|
||||||
GPSMillis = micros();
|
GPSMicros = micros();
|
||||||
GPS_sec = true;
|
GPS_sec = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ const char RMC_off [] PROGMEM = "PUBX,40,RMC,0,0,0,0";
|
|||||||
const char ZDA_off [] PROGMEM = "PUBX,40,ZDA,0,0,0,0";
|
const char ZDA_off [] PROGMEM = "PUBX,40,ZDA,0,0,0,0";
|
||||||
const char RMC_on [] PROGMEM = "PUBX,40,RMC,0,1,0,0";
|
const char RMC_on [] PROGMEM = "PUBX,40,RMC,0,1,0,0";
|
||||||
const char ZDA_on [] PROGMEM = "PUBX,40,ZDA,0,1,0,0";
|
const char ZDA_on [] PROGMEM = "PUBX,40,ZDA,0,1,0,0";
|
||||||
bool GPS_PPS_LCD;
|
// bool GPS_PPS_LCD;
|
||||||
|
|
||||||
void GPS_setup() {
|
void GPS_setup() {
|
||||||
DEBUG_PORT.println(F("DEBUG[GPS_setup()] start"));
|
DEBUG_PORT.println(F("DEBUG[GPS_setup()] start"));
|
||||||
@@ -123,32 +124,52 @@ void GPS_loop() {
|
|||||||
|
|
||||||
if (GPS_sec) { // do after GPS PPS interrupt
|
if (GPS_sec) { // do after GPS PPS interrupt
|
||||||
GPS_sec = false; // clear flag
|
GPS_sec = false; // clear flag
|
||||||
|
GPScolon = true;
|
||||||
lcd.setCursor(2, 0); GPS_PPS_LCD = !GPS_PPS_LCD;
|
lcd.setCursor(10, 0);
|
||||||
lcd.print((GPS_PPS_LCD) ? " " : ":");
|
lcd.print(":");
|
||||||
GPS_PPS();
|
GPS_PPS();
|
||||||
|
DEBUG_PORT.println("PPS: " + String(GPSMicros));
|
||||||
|
}
|
||||||
|
else if (micros() - GPSMicros >= 500000 && GPScolon) {
|
||||||
|
lcd.setCursor(10, 0);
|
||||||
|
lcd.print(" ");
|
||||||
|
GPScolon = false;
|
||||||
}
|
}
|
||||||
GPS_read_seconds(); // continue reading buffer
|
GPS_read_seconds(); // continue reading buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPS_PPS() { // do something on the flip of the GPS second
|
void GPS_PPS() { // do something on the flip of the GPS second
|
||||||
int32_t drift = (RTCMillis - GPSMillis); // % (4294967295UL);
|
int32_t drift = (int32_t)(GPSMicros - RTCMicros); // % (4294967295UL);
|
||||||
|
|
||||||
//DEBUG_PORT.println("DEBUG[GPS_PPS()] Start");
|
// Корректировка переполнения (wrap-around)
|
||||||
if (drift > 900000)
|
if (drift > 900000) // Если разница > 0.9 сек
|
||||||
drift -= 1000000;
|
drift -= 1000000; // Корректируем на -1 сек
|
||||||
else if (drift < -900000)
|
else if (drift < -900000) // Если разница < -0.9 сек
|
||||||
drift += 1000000;
|
drift += 1000000; // Корректируем на +1 сек
|
||||||
|
|
||||||
//DEBUG_PORT.println("DEBUG[GPS_loop()] drift: " + String(drift));
|
//DEBUG_PORT.println("DEBUG[GPS_loop()] drift: " + String(drift));
|
||||||
|
|
||||||
lcd.setCursor((16 - sizeof(String(drift))), 1);
|
// Отображение дрейфа в правом нижнем углу LCD (16x2)
|
||||||
lcd.print(drift);
|
String driftStr = String(drift);
|
||||||
|
if (String(drift).length() > 6) driftStr = " error";
|
||||||
|
|
||||||
|
// Вычисляем позицию для выравнивания справа: 16 - длина строки
|
||||||
|
lcd.setCursor(16 - driftStr.length(), 1);
|
||||||
|
lcd.print(driftStr);
|
||||||
|
|
||||||
|
//if (abs(drift) > 20000 && !gps_on) GPS_ON();
|
||||||
|
if (RTC_sync && !gps_on) {
|
||||||
|
GPS_ON();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (drift > 5000 && !gps_on) GPS_ON();
|
|
||||||
if (gps_on) { // do only when needed
|
if (gps_on) { // do only when needed
|
||||||
|
if (pulse_count == 0) {
|
||||||
|
lcd.setCursor(2, 0);
|
||||||
|
lcd.print("S");
|
||||||
|
}
|
||||||
pulse_count += 1;
|
pulse_count += 1;
|
||||||
if (pulse_count > 2) { // skip first PPS-Pulses, to make shure time is from satellite
|
if (pulse_count > 4) { // skip first PPS-Pulses, to make shure time is from satellite
|
||||||
if (gps_seconds_t != 0) { // do only if value is set
|
if (gps_seconds_t != 0) { // do only if value is set
|
||||||
// gps_seconds_t += offset_28y; // debug & testing only!
|
// gps_seconds_t += offset_28y; // debug & testing only!
|
||||||
// gps_seconds_t += offset_26w; // debug & testing only!
|
// gps_seconds_t += offset_26w; // debug & testing only!
|
||||||
@@ -177,12 +198,17 @@ void GPS_ON() {
|
|||||||
gps_on = true;
|
gps_on = true;
|
||||||
gps_seconds_t = 0; // make shure GPS serial is alive before setting
|
gps_seconds_t = 0; // make shure GPS serial is alive before setting
|
||||||
pulse_count = 0;
|
pulse_count = 0;
|
||||||
|
lcd.setCursor(2, 0);
|
||||||
|
lcd.print("S");
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPS_OFF() {
|
void GPS_OFF() {
|
||||||
if (gps_on) { // only if NOT off
|
if (gps_on) { // only if NOT off
|
||||||
gps_on = false;
|
gps_on = false;
|
||||||
|
RTC_sync = false;
|
||||||
//Serial.println("GPS: OFF"); // debug
|
//Serial.println("GPS: OFF"); // debug
|
||||||
|
lcd.setCursor(2, 0);
|
||||||
|
lcd.print(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
64
RTC_com.cpp
64
RTC_com.cpp
@@ -17,29 +17,31 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// add the static modifier to limit visibility of these variables to just this file
|
// add the static modifier to limit visibility of these variables to just this file
|
||||||
static int ledPin_PPS = A6; // Pin for RTC PseudoPPS LED [analog PIN used as digital PIN]
|
//static int ledPin_PPS = A6; // Pin for RTC PseudoPPS LED [analog PIN used as digital PIN]
|
||||||
static int ledPin_Sync = A7; // Pin for valid RTC Sync
|
//static int ledPin_Sync = A7; // Pin for valid RTC Sync
|
||||||
static byte RTC_1HZ_PIN = cfg_pin_RTC_SQW; // Pin 3 = NANO INT1, RTC provides a 1Hz interrupt signal on this Pin
|
static byte RTC_1HZ_PIN = cfg_pin_RTC_SQW; // Pin 3 = NANO INT1, RTC provides a 1Hz interrupt signal on this Pin
|
||||||
static time_t last_sync_t = 0; // last sync
|
static time_t last_sync_t = 0; // last sync
|
||||||
static byte sync_err_hours = 4; // set to 4 for DS3231M, may be set to 8 for DS3231SN. Warning if no GPS sync after n hours, possible 0.1 sec error.
|
static byte sync_err_hours = 4; // set to 4 for DS3231M, may be set to 8 for DS3231SN. Warning if no GPS sync after n hours, possible 0.1 sec error.
|
||||||
bool SyncErr = true, RTC_SQW_LCD;
|
static bool RTCcolon = false;
|
||||||
|
bool SyncErr = true;
|
||||||
|
|
||||||
#include <DS3232RTC.h>
|
#include <DS3232RTC.h>
|
||||||
DS3232RTC myRTC(0x68); // cfg_
|
DS3232RTC myRTC(0x68); // cfg_
|
||||||
volatile bool RTC_sec = false; // flag for PseudoPPS
|
volatile bool RTC_sec = false; // flag for PseudoPPS
|
||||||
unsigned long RTCMillis; // blinking timer
|
volatile bool RTC_sync = false; // flag for RTC sync
|
||||||
|
unsigned long RTCMicros; // blinking timer
|
||||||
|
|
||||||
void rtc_interrupt() {
|
void rtc_interrupt() {
|
||||||
//Serial.print("DEBUG[INT] RTC interrupt "); Serial.println(micros());
|
//Serial.print("DEBUG[INT] RTC interrupt "); Serial.println(micros());
|
||||||
RTCMillis = micros();
|
RTCMicros = micros();
|
||||||
RTC_sec = true;
|
RTC_sec = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RTC_LED_setup() {
|
void RTC_LED_setup() {
|
||||||
pinMode(ledPin_PPS, OUTPUT);
|
//pinMode(ledPin_PPS, OUTPUT);
|
||||||
pinMode(ledPin_Sync, OUTPUT);
|
//pinMode(ledPin_Sync, OUTPUT);
|
||||||
digitalWrite(ledPin_PPS, LOW); // LED off
|
//digitalWrite(ledPin_PPS, LOW); // LED off
|
||||||
digitalWrite(ledPin_Sync, LOW); // LED off
|
//digitalWrite(ledPin_Sync, LOW); // LED off
|
||||||
}
|
}
|
||||||
|
|
||||||
void RTC_setup() {
|
void RTC_setup() {
|
||||||
@@ -53,25 +55,52 @@ void RTC_setup() {
|
|||||||
void RTC_loop() {
|
void RTC_loop() {
|
||||||
if (RTC_sec) { // do after RTC PseudoPPS interrupt, without interrupt NO time/date to display
|
if (RTC_sec) { // do after RTC PseudoPPS interrupt, without interrupt NO time/date to display
|
||||||
//DEBUG_PORT.println("DEBUG[RTC_loop()] RTC_sec: " + String(RTC_sec));
|
//DEBUG_PORT.println("DEBUG[RTC_loop()] RTC_sec: " + String(RTC_sec));
|
||||||
//DEBUG_PORT.println("DEBUG[RTC_loop()] RTCMillis: " + String(RTCMillis));
|
//DEBUG_PORT.println("DEBUG[RTC_loop()] RTCMicros: " + String(RTCMicros));
|
||||||
//DEBUG_PORT.println("DEBUG[RTC_loop()] millis: " + String(millis()));
|
//DEBUG_PORT.println("DEBUG[RTC_loop()] millis: " + String(millis()));
|
||||||
|
|
||||||
lcd.setCursor(5, 0); RTC_SQW_LCD = !RTC_SQW_LCD;
|
lcd.setCursor(13, 0);
|
||||||
lcd.print((RTC_SQW_LCD) ? " " : ":");
|
lcd.print(":");
|
||||||
|
|
||||||
|
RTCcolon = true;
|
||||||
RTC_sec = false; // clear flag
|
RTC_sec = false; // clear flag
|
||||||
digitalWrite(ledPin_PPS, LOW); // LED off
|
|
||||||
|
int32_t drift = (int32_t)(GPSMicros - RTCMicros); // % (4294967295UL);
|
||||||
|
|
||||||
|
// Корректировка переполнения (wrap-around)
|
||||||
|
if (drift > 900000) // Если разница > 0.9 сек
|
||||||
|
drift -= 1000000; // Корректируем на -1 сек
|
||||||
|
else if (drift < -900000) // Если разница < -0.9 сек
|
||||||
|
drift += 1000000; // Корректируем на +1 сек
|
||||||
|
|
||||||
|
// Отображение дрейфа в правом нижнем углу LCD (16x2)
|
||||||
|
String driftStr = String(drift);
|
||||||
|
// Вычисляем позицию для выравнивания справа: 16 - длина строки
|
||||||
|
lcd.setCursor(16 - driftStr.length(), 1);
|
||||||
|
lcd.print(driftStr);
|
||||||
|
|
||||||
|
DEBUG_PORT.println("SQW: " + String(RTCMicros) + "; drift: " + String(drift) + "; newSec: " + String(NewSec));
|
||||||
|
|
||||||
|
if (abs(drift) > 20000 && !gps_on) RTC_sync = true;
|
||||||
|
//digitalWrite(ledPin_PPS, LOW); // LED off
|
||||||
time_t now_t = myRTC.get();
|
time_t now_t = myRTC.get();
|
||||||
if ((last_sync_t + (sync_err_hours * 3600)) < now_t) {
|
if ((last_sync_t + (sync_err_hours * 3600)) < now_t) {
|
||||||
SyncErr = true;
|
SyncErr = true;
|
||||||
//digitalWrite(ledPin_Sync, LOW); // LED off
|
//digitalWrite(ledPin_Sync, LOW); // LED off
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
|
lcd.print("r");
|
||||||
}
|
}
|
||||||
Sec_Flip(now_t); // pass current datetime from RTC in UTC
|
Sec_Flip(now_t); // pass current datetime from RTC in UTC
|
||||||
}
|
}
|
||||||
|
else if (micros() - RTCMicros >= 500000 && RTCcolon) {
|
||||||
|
lcd.setCursor(13, 0);
|
||||||
|
lcd.print(" ");
|
||||||
|
RTCcolon = false;
|
||||||
|
NewSec = false;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
NewSec = false;
|
NewSec = false;
|
||||||
}
|
}
|
||||||
/*if (millis() > (RTCMillis + 100)) { // do 100ms after PseudoPPS interrupt
|
/*if (millis() > (RTCMicros + 100)) { // do 100ms after PseudoPPS interrupt
|
||||||
digitalWrite(ledPin_PPS, HIGH); // LED on
|
digitalWrite(ledPin_PPS, HIGH); // LED on
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
@@ -82,6 +111,13 @@ void SetRTC(time_t t) {
|
|||||||
SyncErr = false;
|
SyncErr = false;
|
||||||
//digitalWrite(ledPin_Sync, HIGH); // LED on
|
//digitalWrite(ledPin_Sync, HIGH); // LED on
|
||||||
DEBUG_PORT.println("RTC set by GPS"); // debug
|
DEBUG_PORT.println("RTC set by GPS"); // debug
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
|
lcd.print("R");
|
||||||
|
|
||||||
|
char segment[9];
|
||||||
|
snprintf(segment, sizeof(segment),"%.2u:%.2u:%.2u", hour(t), minute(t), second(t));
|
||||||
|
lcd.setCursor(0, 1);
|
||||||
|
lcd.print(segment);
|
||||||
}
|
}
|
||||||
|
|
||||||
//End
|
//End
|
||||||
Reference in New Issue
Block a user