I'm checking the accuracy of my arduino clock against a (ChronoDot) DS3231 TCXO running a 1Hz interrupt using the attached code. Results show +/-1 ms difference every few seconds. Latency in the code, arduino millis calculation issue or crystal? Please comment.
TIA.
//ChronoDot test [ds3231]
//NOTE: 10k pullup resistor on SQW line
#include <Wire.h>
#define INT_PIN 3 //chronodot sqw wire
extern volatile unsigned long timer0_millis;
volatile unsigned long t, now, prev;
void setup() {
//init wire & serial
Wire.begin();
Serial.begin(9600);
Wire.beginTransmission(104);
Wire.send(0x0e); //select control register
//set sqw at specified Hz of 0=1Hz
Wire.send(0);
Wire.endTransmission();
//init interrupt
pinMode(INT_PIN, INPUT);
attachInterrupt(1, OneHzInterrupt, FALLING); //0 = digital pin 3
}
void OneHzInterrupt(void) {
now = timer0_millis;
t = now - prev;
prev = now;
}
void loop() {
Serial.println(t);
delay(1000);
}