Journal #6521

Posted 13 years ago2010-04-25 05:06:58 UTC
The perfect clock: A theoretical approach

For those that showed interest in my previous journal.
Warning: Long post ahoy!

As I have commented in a previous journal, for several years already I wanted to make a digital clock with a PIC microcontroller and I never really got around to actually build it. Not because I didn't know how, just because I was lazy or didn't have the time. Last year at uni, while not paying attention to maths class (should have, but instead I did this) I spent my afternoons trying to at least think how the assembly code would be. I will later look up the code I've already written and include it here. This is what I have observed, and I hope others will find it useful.

Ever since I had to program one in school, one of the most elemental flaws in a simple clock design is error propagation - which might not matter when the clock is only a decorative item in projects that don't live more than a couple of minutes inside the lab, but is kind of an important issue if what you're making is a clock that will (hopefully) stay for years on that shelf in plain sight. And dammit you want it decently accurate.

Here I shall describe a simple setup that should run (at least) on either a PIC 16F84 or a PIC 16F877 (tested on those, should run on other 16F- models as well).

This microprocessor should be running on a 4MHz crystal oscillator, to achieve the simple number of 1MIPS (1Million instructions in 1 second). That's just mine, do your own calculations for other oscillator frequencies.

The basic design can't be simpler. First we need to enable timer-based hardware interrupts, by setting INTCON to 1X1XX000. Then the Prescaler has to be set to 1:128 by setting OPTION_REG to 00000110, to scale down those interrupts to a longer, more practical interval. This should leave us with an interrupt every 8192 oscillator cycles. Which at 1MHz last 1 microsecond each, so 8192 cycles should last us 8.192 milliseconds between interrupts.

Hardware-based timer interrupts are obviously periodical. If they are fired every 8.192ms, you count enough interrupts and you should be close enough to a full second, right? Not quite, because 8.192ms x 122 = 999.424ms. That's almost one second. Almost. If you just increment the seconds-count register after 122 interrupts, your clock will be 1 second ahead for every about 14 minutes. Doesn't sound so good now, eh?

Well, you could find out how many seconds does it take to be one full second ahead, and substract one second at that point. Fine, that would WORK. It's EASY. And it's the only solution I've ever found on the internet. But it's a horrible hack and it will look HORRIBLE if you happen to be looking at it at that time. But go ahead if you like, do that and stop reading here.

I'll keep going for those that want their seconds more uniformly spaced. What if we count 123 interrupts? Well, no. It turns out that we'd be off the other way: That's 1007.616ms. A larger error, with longer seconds - leading to the clock being 1 second behind every about 5 minutes of running. That's even worse.

So if we want our seconds more uniformly timed (and our clock not looking like it has a bug), we'll have to use a combination of 122 and 123 cycles. Now, counting 122 interrupts gets us 0.999424 seconds. If we add up enough of these "short" seconds, the leftover time should someday be enough that a "long" second (123 cycles) wouldn't be too noticed. It turns out that magic number is 13:

13 "short" seconds = 8.192ms x 122 interrupts x 13 = 12992.512ms = 12.992512 actual seconds

If at this point we make the next second a "long" second (of 1.007616 actual seconds), our 14 seconds will last exactly 14.000128 actual seconds. That's much better, we have actually trimmed the error to about a four-millionth. But it is still not good enough for me.

At this point we can repeat the above process. We have a cycle of 14.000128 seconds. We could check how many of these are needed to be able to easily substract the leftover error. Notice it happens to end in 128, which is a power of 2 and thus easier to add up. So if we count 64 of these 14000.128ms cycles, it adds up to 896008.192ms (that's a lot of seconds). If only we could trim those 8.192ms off the 64th cycle...

Wait a minute, don't the timer interrupts last 8.192ms? And remember that for every 13 "short" seconds, the 14th happened to be 8.192ms longer? I think we're lucky. Here, we make the first 63 14-second cycles "normal", and the 64th has to be purely 14 122-interrupt seconds. What does this mean? Well, this:

(8.192ms x 122i x 13 + 8.192ms x 123i) x 63 + 8.192ms x 122i x 14 = 896000ms
(Green="short" second, Red="Long" second)

Cue the sunshine, because what we have here, gentlemen, is 896 perfectly rounded seconds - or 14 minutes and 56 seconds.

Now we can rejoice watching others' sub-perfect, sloppy, whole-second-substracting clocks while enjoying our beautifully smooth clocks with perfect cycles of 14:56.

With this, we have successfully removed the software error in the time calculations, leaving only the time shift caused by (possible) manufactural imperfections in the oscillator crystal.

Now you take care of date-keeping and Daylight Savings Time.

9 Comments

Commented 13 years ago2010-04-25 05:32:28 UTC Comment #61920
Great project :).
Commented 13 years ago2010-04-25 06:09:46 UTC Comment #61918
The numbers....they attack my eyes and assault my brain... ;)
Commented 13 years ago2010-04-25 06:18:39 UTC Comment #61921
Well for me it was kinda like a "tick-tock" sensation =)).
Commented 13 years ago2010-04-25 06:21:00 UTC Comment #61917
I started out thinking that this post would be full of EE rubbish but it's actually just maths (which I am much more comfortable with) :P
Commented 13 years ago2010-04-25 11:15:39 UTC Comment #61916
holy damn dude. If i show this to my professor he's going to flip a shit. We just finished the 8bit and 16bit timers.. (we're using the 16F877 PIC)AND we run at 4mhz. only thing we did differently with a 1 second count was to use option reg at xxxx0010 using a 64bit count, and we set the interrupts to 61 or something. Don't remember the actual code or anything, but we program in BASIC and not assembly, god that must be rough.
Commented 13 years ago2010-04-25 16:04:16 UTC Comment #61922
I was going to post the code too, but I couldn't remember where I saved it. Now that I do, I can post it if you'd like to see it.

I prefer coding in Assembly, I just don't trust higher-level compilers to do what I want ;) Besides, I remember being told that the C compiler (provided by PIC) translates to long unnecessary assembly code. So what better than doing the assembly yourself :)
Commented 13 years ago2010-04-26 09:12:11 UTC Comment #61919
tl;dr

couldn't resist sorry.. actually sounds pretty cool. Wish i was good at maths and logic so i could program too..

)

Commented 13 years ago2010-04-26 19:10:53 UTC Comment #61915
Your crystal oscillator is going to change frequencies with the temperature should you decide to take it to Central America or Antarctica and leave it outside, heh.

Perfect time gone :(
Commented 13 years ago2010-04-27 00:27:59 UTC Comment #61923
It would be silly of you to carry it bare. You should totally buy the warmer and cooler accessories for the crystal oscillator, they're only $49.95 - and if you order now, you get the anti-dust clear acryllic box for the whole clock.. absolutely FREE!

You must log in to post a comment. You can login or register a new account.