RTC sync with GPS v1.2
This commit is contained in:
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 time_t gps_seconds_t = 0; // GPS time
|
||||
static byte gps_sats = 0;
|
||||
static bool GPScolon = false;
|
||||
volatile bool GPS_sec = false; // flag for GPS-PPS
|
||||
unsigned long GPSMillis;
|
||||
unsigned long GPSMicros;
|
||||
|
||||
// set interrupt flag
|
||||
void gps_interrupt() {
|
||||
GPSMillis = micros();
|
||||
GPSMicros = micros();
|
||||
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 RMC_on [] PROGMEM = "PUBX,40,RMC,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() {
|
||||
DEBUG_PORT.println(F("DEBUG[GPS_setup()] start"));
|
||||
@@ -123,32 +124,52 @@ void GPS_loop() {
|
||||
|
||||
if (GPS_sec) { // do after GPS PPS interrupt
|
||||
GPS_sec = false; // clear flag
|
||||
|
||||
lcd.setCursor(2, 0); GPS_PPS_LCD = !GPS_PPS_LCD;
|
||||
lcd.print((GPS_PPS_LCD) ? " " : ":");
|
||||
GPScolon = true;
|
||||
lcd.setCursor(10, 0);
|
||||
lcd.print(":");
|
||||
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
|
||||
}
|
||||
|
||||
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");
|
||||
if (drift > 900000)
|
||||
drift -= 1000000;
|
||||
else if (drift < -900000)
|
||||
drift += 1000000;
|
||||
// Корректировка переполнения (wrap-around)
|
||||
if (drift > 900000) // Если разница > 0.9 сек
|
||||
drift -= 1000000; // Корректируем на -1 сек
|
||||
else if (drift < -900000) // Если разница < -0.9 сек
|
||||
drift += 1000000; // Корректируем на +1 сек
|
||||
|
||||
//DEBUG_PORT.println("DEBUG[GPS_loop()] drift: " + String(drift));
|
||||
|
||||
lcd.setCursor((16 - sizeof(String(drift))), 1);
|
||||
lcd.print(drift);
|
||||
// Отображение дрейфа в правом нижнем углу LCD (16x2)
|
||||
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 (pulse_count == 0) {
|
||||
lcd.setCursor(2, 0);
|
||||
lcd.print("S");
|
||||
}
|
||||
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
|
||||
// gps_seconds_t += offset_28y; // debug & testing only!
|
||||
// gps_seconds_t += offset_26w; // debug & testing only!
|
||||
@@ -177,12 +198,17 @@ void GPS_ON() {
|
||||
gps_on = true;
|
||||
gps_seconds_t = 0; // make shure GPS serial is alive before setting
|
||||
pulse_count = 0;
|
||||
lcd.setCursor(2, 0);
|
||||
lcd.print("S");
|
||||
}
|
||||
|
||||
void GPS_OFF() {
|
||||
if (gps_on) { // only if NOT off
|
||||
gps_on = false;
|
||||
RTC_sync = false;
|
||||
//Serial.println("GPS: OFF"); // debug
|
||||
lcd.setCursor(2, 0);
|
||||
lcd.print(" ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user