Mars Clock
Moderators: adafruit_support_bill, adafruit

Re: Mars Clock

by adafruit_support_rick on Sat Jan 05, 2013 1:06 pm

odometer wrote:I don't get why try alter the speed of an RTC or Arduino clock. Is that math really that hard?!


Not suggesting that you alter the speed of the clock. I'm suggesting you alter the math used to determine the rate of timer interrupts.

For example, I've got a project where I've set the timer to interrupt every 4ms. To do that, you need to load the timer comparison register with some number. When the timer count matches the compare register, the timer interrupts and resets the count to 0.

The timer also has a prescaler, which divides the 16MHz clock rate by a selectable factor so that you don't overrun the timer count too quickly. In this case I've set the prescaler to divide by 256. So, here's how you get the value to load into the comparison register to get 4ms interrupts:

(16,000,000Hz / 256 prescaler / 1000ms) * 4ms = 250 ticks.

To get 4 Mars milliseconds, you would substitute 16,439,860. Unfortunately, that doesn't work out so well, because you wind up with 256.78, which is too big for an 8-bit counter. So you have to play around with the numbers a little to get something that works, and maybe use a 16-bit timer instead. You don't even have to track whole milliseconds - the Arduino counts by 1.024 milliseconds.

So, it's nothing to do with avoiding math. In fact, the math is probably even trickier. The point is that the computer is *already* tracking time. So why not simply track time the way you want to in the first place, instead of tracking time in one system and then converting it to another.

odometer wrote:It's really not a good idea to use 1027 milliseconds to approximate 1.027491... seconds. If you do, the clock will be out two minutes in three days.


I'm not. I used as many decimal places of 1.0274912510416665 as would fit in my HP calculator to come up with 16,439,860MarsHz.

Here is is from Excel: 16439860.0166666000000000. The clock rate is the highest precision number in the computer. You'll do better by applying your conversion factor there than by applying it to derivatives like us or ms.

adafruit_support_rick
 
Posts: 12938
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY