# Checking the accuracy of a DS1340Z RTC using its 512Hz output

The data sheet for the DS1340Z suggests that its 512 Hz output can be used to calibrate itself by reading the number of pulses over a given lenght of time (e.g. 512 per second or 30720 per minute(better) = no calibration is needed). I understand the concept and the math. My question is, how can I accuratly read the number of pulses with the arduino (I have the Mega2560). I have noticed the accuracy of the RTC varies slightly with temperature. It would be nice to have it self-calibrate on the fly :D. Thanks in advance for any help with this.

I have no real clue but the first thought that pops in my head for that low of a speed is interrupts

set an external interrupt and increment a counter, BUT who is to say the clock rate on the arduino is off not your rtc

That’s almost impossible.
Calibrating the crystal of the DS1340 agains the crystal of the Arduino seems odd.

If the Arduino would get the time from the internet, and measure the FT/out signal for a day, than it can be calibrated.
Counting the pulses definitely needs an interrupt, as Osgeld wrote.

Thanks for the fast replies. I do know an interrupt is necessary to count the pulses. The problem I'd like to solve is how/where to get an accurate length of time to measure with? Also, I don't have an Ethernet shield/connection for my arduino. I suppose getting the time from the internet would work, but at that point, why do I need the DS1340Z? I'm trying to keep the hardware as simple as possible.

The Arduino is going to be just as if not more inaccurate than the RTC. If you want accuracy try the DS3232 from Maxim. It is + or - 2 ppm from 0 to 40 degrees C, or about a minute per year.

I calibrate RTC chips by connecting a GPS pulse per second signal to a pin with interrupts.

Connect the RTC sqw to another pin with interrupts.

Count pulses for at least an hour to get the accuracy of the RTC to a ppm. The GPS pps is accurate to about one microsecond.

Thanks for the replies guys. I’m going to go with the DS3232. A minute or two a year is accurate enough for me.

Thanx, DJ

You certainly don't need interrupts to count a slow signal like 512Hz - code runs plenty fast enough to poll the signal.

Even better though you can set up one of the timer modules to count input pulses automatically. Timer modules 0 and 1 are counter-timers and have a count-external-pulses mode. However only certain pins can be used - pin 4 to timer0 and pin 5 to timer1. Since timer0 is used for delay(), millis(), etc, it makes sense to use timer1:

``````void setup ()
{
TCCR1A = 0x00 ; // normal CTC mode
TCCR1B = 0x07 ; // select pin T1 (Arduino pin 5) rising edge (use 0x06 for falling edge)
TCNT1 = 0x0000 ; // reset counter

Serial.begin (57600) ;
}

unsigned int prev = 0 ;
unsigned int now  = 0 ;

void loop ()
{
now = TCNT1 ;  // read 16 bit counter
Serial.println (now-prev) ;  // display counts since last time
prev = now ;
delay (1000) ;
}
``````

Actually to quickly get an accurate frequency from an evenly spaced pulse stream you want to both count pulses and record their arrival time - then you can compute "float frequency = 1.0 * N / total_time ;" using floating point. total_time is for all N pulses in seconds. So if you counted 5120 pulses in 10000340 microseconds, you'd compute 5120.0 / 10.00034 = 511.98Hz

Here accuracy is only as good as your Arduino's time base - which on the Uno is appallingly bad as its a ceramic resonator (about 1% variation between units). The Duemilanova had a quartz crystal. It is possibly, with care, to desolder the ceramic resonator on the Uno and put a 16MHz crystal in its place (you need 2 18pF caps too).