Pages: [1]   Go Down
Author Topic: timing accuracy test  (Read 919 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Jr. Member
**
Karma: 4
Posts: 92
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
//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);
}
Logged


0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The way the code is implemented timer0_millis increments by 1 every 1.024ms then there is a "correction" every 41 milliseconds and it actually increments by 2. Another variable timer0_fract increments by 3 until it reaches 125 indicating it counted 24 times out of 1000 (3/125-->24/1000).

Think of it like this table:
Actual Time | timer0_millis | timer0_fract
1.024ms13
2.048ms26
3.072ms39
.........
41.984ms41123
43.008ms431
44.032ms444
45.056ms457

Thus the correction leads to a hiccup between 41 and 43.

http://www.ruggedcircuits.com
Logged

Pages: [1]   Go Up
Jump to: