прошивка по записи RTC для старых часов rally
This commit is contained in:
135
2024-07-28 20:57:14 +03:00
parent 3f97f3f02a
commit fc069bd63e
5 changed files with 66 additions and 33 deletions

View File

@@ -15,6 +15,7 @@ bool NewSec = false;
bool NewMin = false;
bool NewHour = false;
time_t Loc_t = 0;
time_t RTC_t = 0;
byte last_min = -1;
byte last_5min = -1;
byte last_hour = -1;
@@ -35,6 +36,7 @@ void GPS_RTC_Clock_loop() {
void Sec_Flip(time_t t) { // called from RTC_com
Loc_t = TZ_Sec(t); // convert UTC to Local Time
RTC_t = t;
NewSec = true; // set "Ready to print"
if (ResetMCU) { // set init time
ResetMCU = false;

View File

@@ -31,14 +31,18 @@
#include "ISOWeekNumber.h" // pass to main ino
#include "Local_names.h" // pass to main ino
#include "RTC_com.h"
#include <LiquidCrystal_I2C.h>
extern bool NewSec; // pass to main ino = second ready to print
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 time_t Loc_t, RTC_t; // pass to main ino = Local timestamp
extern bool gps_on;
extern volatile bool GPS_sec, RTC_sec;
extern unsigned long RTCMillis;// call from main ino, GPS_RTC_Clock.h
extern unsigned long RTCMillis, GPSMillis;// call from main ino, GPS_RTC_Clock.h
extern LiquidCrystal_I2C lcd;
void GPS_RTC_Clock_setup();
// call from main ino, GPS_RTC_Clock.h

View File

@@ -27,9 +27,6 @@ unsigned long TMR_FPS;
long FPS;
bool LCD_Simple_clock;
bool GPS_PPS_LCD, RTC_SQW_LCD;
volatile bool fGPS_PPS, fRTC_SQW;
void setup() { // the setup function runs once when you press reset or power the board
// LCD
@@ -38,7 +35,10 @@ void setup() { // the setup function runs once when you
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("DEBUG[setup()]");
delay(500);
DEBUG_PORT.begin(38400);
DEBUG_PORT.flush();
delay(1000);
DEBUG_PORT.println("DEBUG[setup()]");
//Serial.begin(19200); // = 9600, must be same as GPS for debug
//Serial.println(); // flush serial
//Serial.println("-Arduino Reboot-"); // debug
@@ -63,12 +63,12 @@ void loop() { // the loop function runs over and over again forever
GPS_RTC_Clock_loop(); // first in loop
RTCtoLCD();
chkFPS();
//chkFPS();
}
/////////////////////
void RTCtoLCD() {
if (GPS_sec) {
/*if (GPS_sec) {
DEBUG_PORT.println("DEBUG[RTCtoLCD] GPS_sec");
GPS_PPS_LCD = !GPS_PPS_LCD;
lcd.setCursor(2, 0);
@@ -79,7 +79,7 @@ void RTCtoLCD() {
RTC_SQW_LCD = !RTC_SQW_LCD;
lcd.setCursor(5, 0);
lcd.print((RTC_SQW_LCD) ? " " : ":");
}
}*/
//if (!GPS_sec and !RTC_sec) return;
if (!NewSec && !NewMin && !NewHour) return;
char Clock[9];
@@ -88,19 +88,22 @@ void RTCtoLCD() {
if (NewSec) {
DEBUG_PORT.println("DEBUG[RTCtoLCD] newSec");
DEBUG_PORT.println("DEBUG[RTCtoLCD] RTCMillis: " + String(RTCMillis));
DEBUG_PORT.println("DEBUG[RTCtoLCD] RTC_sec: " + String(RTC_sec));
DEBUG_PORT.println("DEBUG[RTCtoLCD] millis: " + String(millis()));
snprintf(segment, sizeof(segment),"%.2u", second(Loc_t));
//DEBUG_PORT.println("DEBUG[RTCtoLCD] RTCMillis: " + String(RTCMillis));
//DEBUG_PORT.println("DEBUG[RTCtoLCD] RTC_sec: " + String(RTC_sec));
//DEBUG_PORT.println("DEBUG[RTCtoLCD] millis: " + String(millis()));
//snprintf(segment, sizeof(segment),"%.2u", second(Loc_t));
snprintf(segment, sizeof(segment),"%.2u", second(RTC_t));
lcd.setCursor(6, 0);
lcd.print(segment);
}
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));
lcd.setCursor(3, 0);
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));
lcd.setCursor(0, 0);
lcd.print(segment);
}

View File

@@ -11,10 +11,11 @@
#include "GPS_com.h"
#include "RTC_com.h" // to set the RTC
#include "Serial_AdjustBaud.h" // for adjust serial port baudrate
#include "LocalDateTime.h"
#define cfg_pin_GPS_PPS 2
#define cfg_pin_GPS_Rx 8
//#define cfg_pin_GPS_Tx 9;
#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
@@ -23,10 +24,11 @@ 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
unsigned long GPSMillis;
// set interrupt flag
void gps_interrupt() {
//Serial.print("DEBUG[INT] GPS interrupt "); Serial.println(micros());
GPSMillis = micros();
GPS_sec = true;
}
@@ -76,12 +78,10 @@ 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;
void GPS_setup() {
DEBUG_PORT.begin(38400);
DEBUG_PORT.println(F("DEBUG[GPS_setup()] start"));
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;
if (detRate(cfg_pin_GPS_Rx) != 38400) {
DEBUG_PORT.println(F("DEBUG[GPS_setup()] baudrate not 38400"));
@@ -113,25 +113,46 @@ void GPS_setup() {
delay(100);
gps.send_P(&gpsPort, (const __FlashStringHelper *) ZDA_on);
delay(100);
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_ON();
}
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) ? " " : ":");
GPS_PPS();
}
GPS_read_seconds(); // continue reading buffer
}
void GPS_PPS() { // do something on the flip of the GPS second
int32_t drift = (RTCMillis - GPSMillis); // % (4294967295UL);
//DEBUG_PORT.println("DEBUG[GPS_PPS()] Start");
if (drift > 900000)
drift -= 1000000;
else if (drift < -900000)
drift += 1000000;
//DEBUG_PORT.println("DEBUG[GPS_loop()] drift: " + String(drift));
lcd.setCursor((16 - sizeof(String(drift))), 1);
lcd.print(drift);
if (drift > 5000 && !gps_on) GPS_ON();
if (gps_on) { // do only when needed
pulse_count += 1;
if (pulse_count > 2) { // 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!
SetRTC(gps_seconds_t); // sync RTC with GPS
SetRTC(TZ_Sec(gps_seconds_t)); // sync RTC with GPS
GPS_OFF();
}
}
@@ -152,10 +173,10 @@ void GPS_read_seconds() {
}
void GPS_ON() {
DEBUG_PORT.println("DEBUG[GPS_ON()] Start");
gps_on = true;
gps_seconds_t = 0; // make shure GPS serial is alive before setting
pulse_count = 0;
//Serial.println("GPS: ON"); // debug
}
void GPS_OFF() {

View File

@@ -22,7 +22,7 @@ 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 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.
bool SyncErr = true;
bool SyncErr = true, RTC_SQW_LCD;
#include <DS3232RTC.h>
DS3232RTC myRTC(0x68); // cfg_
@@ -31,7 +31,7 @@ unsigned long RTCMillis; // blinking timer
void rtc_interrupt() {
//Serial.print("DEBUG[INT] RTC interrupt "); Serial.println(micros());
RTCMillis = millis();
RTCMillis = micros();
RTC_sec = true;
}
@@ -51,34 +51,37 @@ void RTC_setup() {
}
void RTC_loop() {
//Serial.println("DEBUG[RTC_loop()] Start");
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()] RTCMillis: " + String(RTCMillis));
DEBUG_PORT.println("DEBUG[RTC_loop()] millis: " + String(millis()));
//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()] millis: " + String(millis()));
lcd.setCursor(5, 0); RTC_SQW_LCD = !RTC_SQW_LCD;
lcd.print((RTC_SQW_LCD) ? " " : ":");
RTC_sec = false; // clear flag
digitalWrite(ledPin_PPS, LOW); // LED off
time_t now_t = myRTC.get();
if ((last_sync_t + (sync_err_hours * 3600)) < now_t) {
SyncErr = true;
digitalWrite(ledPin_Sync, LOW); // LED off
//digitalWrite(ledPin_Sync, LOW); // LED off
}
Sec_Flip(now_t); // pass current datetime from RTC in UTC
}
else {
NewSec = false;
}
if (millis() > (RTCMillis + 100)) { // do 100ms after PseudoPPS interrupt
/*if (millis() > (RTCMillis + 100)) { // do 100ms after PseudoPPS interrupt
digitalWrite(ledPin_PPS, HIGH); // LED on
}
}*/
}
void SetRTC(time_t t) {
myRTC.set(t + 1); // the GPS NMEA output is 1 second behind at PPS !!
last_sync_t = t;
SyncErr = false;
digitalWrite(ledPin_Sync, HIGH); // LED on
//Serial.println("RTC set by GPS"); // debug
//digitalWrite(ledPin_Sync, HIGH); // LED on
DEBUG_PORT.println("RTC set by GPS"); // debug
}
//End