diff --git a/GPS_RTC_Clock.h b/GPS_RTC_Clock.h index 9855932..6918982 100644 --- a/GPS_RTC_Clock.h +++ b/GPS_RTC_Clock.h @@ -37,6 +37,7 @@ extern bool NewMin; // pass to main ino = minute ready to print extern bool NewHour; // pass to main ino = Hour ready to print extern time_t Loc_t; // pass to main ino = Local timestamp +extern volatile unsigned long tmr_SQW, tmr_PPS; // call from main ino, GPS_RTC_Clock.h void GPS_RTC_Clock_setup(); diff --git a/GPS_RTC_Clock.ino b/GPS_RTC_Clock.ino index 74aaf17..e699e80 100644 --- a/GPS_RTC_Clock.ino +++ b/GPS_RTC_Clock.ino @@ -22,13 +22,15 @@ LiquidCrystal_I2C lcd(0x27, 16, 2); char TempBuf[5] = "99.9"; // demo value char HumiBuf[3] = "99"; // demo value +unsigned long TMR_FPS; + bool LCD_Simple_clock; void RTCtoLCD(bool ClockType = LCD_Simple_clock) { - if (millis() < 2000) ClockType = true; - else if (!NewSec && !NewMin && !NewHour) return; + if (!NewSec && !NewMin && !NewHour) return; char Clock[9]; char segment[3]; + char message[50]; if (ClockType) { snprintf(Clock, sizeof(Clock), "%.2u:%.2u:%.2u", hour(Loc_t), minute(Loc_t), second(Loc_t)); @@ -40,16 +42,22 @@ void RTCtoLCD(bool ClockType = LCD_Simple_clock) { lcd.setCursor(6, 0); lcd.print(segment); } + if (NewMin) { snprintf(segment, sizeof(segment),"%.2u", minute(Loc_t)); lcd.setCursor(3, 0); lcd.print(segment); - } - if (NewHour) { snprintf(segment, sizeof(segment),"%.2u", hour(Loc_t)); lcd.setCursor(0, 0); lcd.print(segment); } + //Serial.print("DEBUG[RTCtoLCD] millis(): "); Serial.println(micros()); +// long drift = tmr_PPS - tmr_SQW; + //Serial.println("DEBUG[RTCtoLCD] Difference PPS - SQW: " + String(drift) + "[" + String(tmr_PPS) + "]" + "[" + String(tmr_SQW) + "]"); +// lcd.setCursor(15, 1); +// lcd.print((micros() - tmr_PPS < 2000000) ? "P" : "p"); // Выводим "P" или "p" на 16 позиции в зависимости от условия +// snprintf(message, sizeof(message), "micros: %d, tmr_PPS: %d", millis(), tmr_PPS); +// Serial.println(message); } void setup() { // the setup function runs once when you press reset or power the board @@ -85,8 +93,27 @@ void loop() { // the loop function runs over and over again forever RTCtoLCD(); PrintSec(); PrintHour(); + chkFPS(); } +///////////////////// + +void chkFPS() { + int FPS; + if (millis() - TMR_FPS > 1000) { + Serial.println(TMR_FPS); + Serial.println(millis()); + TMR_FPS = millis(); + Serial.print("DEBUG[chkFPS] "); Serial.println(FPS); + FPS = 0; + return; + } + FPS++; +} + + + + void PrintSec() { // print time if new second if (NewSec) { // NewSec = false; // remove flag, do only once every sec diff --git a/GPS_com.cpp b/GPS_com.cpp index a3776b7..19e07cf 100644 --- a/GPS_com.cpp +++ b/GPS_com.cpp @@ -10,10 +10,11 @@ #include // https://github.com/PaulStoffregen/Time #include "GPS_com.h" #include "RTC_com.h" // to set the RTC +#include "Serial_AdjustBaud.h" // for adjust serial port baudrate -#define cfg_pin_GPS_PPS 2; -//#define cfg_pin_GPS_Rx 4; -//#define cfg_pin_GPS_Tx 5; +#define cfg_pin_GPS_PPS 2 +#define cfg_pin_GPS_Rx 8 +//#define cfg_pin_GPS_Tx 9; // add the static modifier to limit visibility of these variables to just this file static byte GPS_PPS_PIN = cfg_pin_GPS_PPS; // Pin 2 = NANO INT0, GPS PPS interrupt signal on this Pin @@ -22,9 +23,12 @@ static int pulse_count = 0; static time_t gps_seconds_t = 0; // GPS time static byte gps_sats = 0; volatile bool GPS_sec = false; // flag for GPS-PPS +volatile unsigned long tmr_PPS; // set interrupt flag void gps_interrupt() { + tmr_PPS = micros(); + Serial.print("DEBUG[INT] GPS interrupt: "); Serial.println(tmr_PPS); //Serial.print("DEBUG[INT] GPS interrupt "); Serial.println(micros()); GPS_sec = true; } @@ -32,7 +36,7 @@ void gps_interrupt() { NMEAGPS gps; // This parses the GPS characters gps_fix fix; // This holds on to the latest values -#include +//#include #include // use only for soft-serial //#define gpsPort Serial //#define GPS_PORT_NAME "Serial" @@ -67,11 +71,18 @@ const long POSIX_Y2K_offset = 946684800; // = 10957 days = 30 years, NeoTime (G const long offset_28y = 883612800; // = 10227 days = 28 years, simple test for 2038 bug const long offset_26w = 15724800; // = 182 days = 26 weeks, simple test for timezone const long offset_135 = 123456; // = 135 seconds, simple test for timezone +const char baud19200 [] PROGMEM = "PUBX,41,1,3,3,19200,0"; + void GPS_setup() { pinMode(GPS_PPS_PIN, INPUT_PULLUP); // enable pullup on interrupt pin attachInterrupt(digitalPinToInterrupt(GPS_PPS_PIN), gps_interrupt, RISING); // 100ms HIGH at start of second GPS_sec = false; - gpsPort.begin(19200); // set PC to same baudrate for debug messages + gpsPort.begin(9600); // set PC to same baudrate for debug messages + gps.send_P(&gpsPort, (const __FlashStringHelper *) baud19200); + gpsPort.flush(); + delay(100); + gpsPort.end(); + gpsPort.begin(19200); GPS_ON(); } @@ -91,6 +102,7 @@ void GPS_PPS() { // do something on the flip of the GPS second // gps_seconds_t += offset_28y; // debug & testing only! // gps_seconds_t += offset_26w; // debug & testing only! // gps_seconds_t += offset_135; + Serial.println("DEBUG[GPS_PPS] difference PPS - SQW: " + String(tmr_PPS - tmr_SQW)); SetRTC(gps_seconds_t); // sync RTC with GPS GPS_OFF(); } diff --git a/RTC_com.cpp b/RTC_com.cpp index 7c57b15..40872c7 100644 --- a/RTC_com.cpp +++ b/RTC_com.cpp @@ -24,8 +24,10 @@ bool SyncErr = true; DS3232RTC myRTC(0x68); // cfg_ volatile bool RTC_sec = false; // flag for PseudoPPS static unsigned long RTCMillis; // blinking timer +volatile unsigned long tmr_SQW; void rtc_interrupt() { + tmr_SQW = micros(); //Serial.print("DEBUG[INT] RTC interrupt "); Serial.println(micros()); RTCMillis = millis(); RTC_sec = true; diff --git a/Serial_AdjustBaud.h b/Serial_AdjustBaud.h new file mode 100644 index 0000000..235c1c8 --- /dev/null +++ b/Serial_AdjustBaud.h @@ -0,0 +1,49 @@ +/** + * Функция для вычисления и возврата скорости передачи данных (baud rate) на основе полученного входного пина. + * + * @param recpin входной пин для определения скорости передачи данных + * + * @return рассчитанная скорость передачи данных на основе измеренной ширины нулевого бита + */ + +long detRate(int recpin){ + long baud, x, rate = 10000; + + // Цикл для измерения ширины нулевого бита + for (int i = 0; i < 15; i++){ + // Ожидание начала низкого бита + while(digitalRead(recpin) == 1) + x = pulseIn(recpin, LOW); // Измерение ширины следующего нулевого бита + rate = x < rate ? x : rate; // Обновление минимальной ширины нулевого бита + } + + // Определение скорости передачи данных на основе измеренной ширины нулевого бита + if (rate < 12) + baud = 115200; + else if (rate < 20) + baud = 57600; + else if (rate < 29) + baud = 38400; + else if (rate < 40) + baud = 28800; + else if (rate < 60) + baud = 19200; + else if (rate < 80) + baud = 14400; + else if (rate < 150) + baud = 9600; + else if (rate < 300) + baud = 4800; + else if (rate < 600) + baud = 2400; + else if (rate < 1200) + baud = 1200; + else if (rate < 2400) + baud = 600; + else if (rate < 4800) + baud = 300; + else + baud = 0; + + return baud; // Возврат рассчитанной скорости передачи данных +}