Using a RTC (DS1302) to get microseconds

Hi,

For a time keeping project, we are trying to measure microseconds passed as accurately as possible, to check the accuracy of a mechanical watch. We used the micros() function before, but to avoid errors due to calculations carried out in the code, we decided that we need an external reference. Is it possible to get measurement accurate to a few microseconds using an RTC, especially DS1302(what we could find in our country)?

Cheers, B

No

You could use the internal hardware timer if it is in the micro second range. A HW timer counts to 16 in one micro. I managed to measure real small timings with it.

see - http://arduino.cc/forum/index.php/topic,96971.0.html - (4 pages about measuring pulsewidth down to 1/16 uSec.!

We used the micros() function before, but to avoid errors due to calculations carried out in the code, we decided that we need an external reference.

You cannot get accuracy to the microsecond with an RTC, especially not with a cheap one like the DS1302. Even the more accurate models like the DS3231 (temperature compensated) have error rates of a few ppm. If that's enough, you can use the interrupt line of such a chip to trigger an interrupt on the Arduino (don't use an UNO or Mega2560 because these don't have crystals but ceramic resonators) and measure the microseconds there. You will only get greater accuracy if you run your measurement a few hours or more, else the internal counter (which the micros() function uses) is much more accurate.

Why do the calculations generate errors? If this is the case, fix your code. The micros() function won't loose accuracy because you're doing more or more intensive calculations (it's done mostly in hardware).

robtillaart: You could use the internal hardware timer if it is in the micro second range. A HW timer counts to 16 in one micro. I managed to measure real small timings with it.

see - http://arduino.cc/forum/index.php/topic,96971.0.html - (4 pages about measuring pulsewidth down to 1/16 uSec.!

I'm glad somebody finally listened to duaneb, I was getting worried. The ICF is truly the only way to get precise measurements, everything else is less precise unless you play cycle counting games and whatnot. I try to never write to the timers, I also strive to not have interrupts disabled as these things always add jitter to the result. For example, writing to the timer registers doesn't reset the prescaler, so if the prescaler were set to say 16, the timer could increment as soon as the next clock cycle. There is a way to also clear the prescaler and do it in a synchronized way, but even that has consequences on other operations since there is only one prescaler that is shared by all the timers. I know that sounds wrong, but that's how it works. By not writing to the timer or only allowing the hardware itself to do so, you avoid these issues.

It all comes down to how precise and repeatable it needs to be. I have implemented a project much like what these guys are doing and I used a PIC with a standard crystal. I was able to tweak the load caps until I had the thing running within 1ppm at room temp by comparing it to a quality quartz watch with a known error rate. These guys don't need that kind of precision, many mechanical watches can't even achieve the precision of a resonator. Even the best watches (chronometers) are still no better than an off the shelf crystal.

To get back to the problem at hand. I don't see why they couldn't synchronize their micro to the square wave output of an RTC to achieve what they want. They most likely are interested in very short term measurements. I use the code (actually something similar on a PIC) that I've been waving around the forum lately to capture the period of a waveform since this is what I was most interested in when tinkering with clocks and watches. Since it uses the ICF, it eliminates the noisy overhead of servicing pin change ints or trying to watch a pin in software.

EDIT: If they can accurately determine the amount of error in their Uno's resonator, I believe they can still use it to do what they want. If you know the amount of error in the signal, then you can compensate for it. Over the short term, even hours or days if the temperature is constant, the drift of the Uno 3's resonator is very low (10ish ppm). We need to know how long of a period they want to measure the timepiece over though. Since we're not building a GPS receiver, a small amount of drift, jitter or phase noise is going to mean little in the scheme of things; especially when you're timing a mechanical watch. Even if the Uno's resonator is off by 1000ppm, it will be consistently off and software can compensate for it. You'd simply add or subtract 1 for every 1000uS measured. In my experience the Uno's resonator is at least one order of magnitude more accurate than that.