Hello,
I made some tests with the millis() function against a GPS PPS source and am surprised, that there is a permanent drift of one millisec every 2...4 sec. I expected better accuracy because of the separate 32 kHz crystal on the MKR 1400. In previous projects with Mega2560 I used a extra DS3234 for the 32 kHz clock and was impressed about the accuracy of 1 sec/day.
Here my code:
#include <Arduino_MKRGPS.h>
const byte ppsPin = 6;
volatile bool newSec = false;
volatile int16_t abwPPS = 0;
extern volatile uint32_t _ulTickCount; // from delay.c, static removed
void ISR_pps() {
newSec = true;
abwPPS = _ulTickCount % 1000;
if ( abwPPS > 500 ) abwPPS -= 1000;
_ulTickCount -= abwPPS;
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Starting");
if (!GPS.begin(GPS_MODE_SHIELD)) {
Serial.println("Failed to initialize GPS!");
while (1);
}
pinMode(ppsPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ppsPin), ISR_pps, RISING);
Serial.println("Setup finished");
}
void loop() {
if ( newSec ) {
newSec = false;
Serial.print("millis=");Serial.print(millis());Serial.print(", abwPPS=");Serial.println(abwPPS);
}
}
I also removed the static in the _ulTickCount definition in delay.c to make _ulTickCount available in the main scope:
/** Tick Counter united by ms */
//static volatile uint32_t _ulTickCount=0 ;
volatile uint32_t _ulTickCount=0;
And I deactivated all GPS NMEA messages expect RMC in the begin method of GPS.c from the Arduino_GPS library to reduce the serial interrupt load:
_stream->write("$PUBX,40,GLL,0,0,0,0,0,0*5C\r\n"); _stream->flush();
_stream->write("$PUBX,40,GSV,0,0,0,0,0,0*59\r\n"); _stream->flush();
_stream->write("$PUBX,40,GSA,0,0,0,0,0,0*4E\r\n"); _stream->flush();
_stream->write("$PUBX,40,GGA,0,0,0,0,0,0*5A\r\n"); _stream->flush();
_stream->write("$PUBX,40,ZDA,0,0,0,0,0,0*44\r\n"); _stream->flush();
_stream->write("$PUBX,40,VTG,0,0,0,0,0,0*5E\r\n"); _stream->flush();
Hardware is MKR 1400 with MKR GPS shield with PPS jumper connected.
millis=2086000, abwPPS=0
millis=2087000, abwPPS=1 correction was necessary
millis=2088000, abwPPS=0
millis=2089000, abwPPS=0
millis=2090000, abwPPS=0
millis=2091000, abwPPS=1 correction was necessary
millis=2092000, abwPPS=0
millis=2093000, abwPPS=0
millis=2094000, abwPPS=1 correction was necessary
millis=2095000, abwPPS=0
millis=2096000, abwPPS=0
millis=2097000, abwPPS=0
millis=2098000, abwPPS=1 correction was necessary
millis=2099000, abwPPS=0
millis=2100000, abwPPS=0
millis=2101000, abwPPS=0
Is the MKR1400 crystal realy so much worse the DR3234 clock?