NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Basic Electronics » accurate time for a real time clock
March 12, 2013 by cwmilost |
i been seeing lots of people having problems not having accurate clocks using nerdkits. tip that i believe works well is using timer1 instead of timer0. this has advantages for getting a better resolution. this is because timer1 is 16 bits instead of 8 bits like timer0 . you can use a 256 prescaler instead of 1024 to get less round off error when it divides the clock cycle since it not a floating point register. 14745600/256, or 57600 increments per second you can use this to interrupt every 1 sec. you can fine tune 57600 by adding to it to slow clock down or subtract from it to speed clock up. this is an easy way to get more accurate timing for anything. you will probalbly have to use a different value than 57638 depending on your circuit.try it out i want to see how accurate people can get there real time clocks by using this approach. try note: this code the interrupt fires every 1 sec not every 1/100 th of a second
|
---|---|
April 07, 2013 by Blackwatch42nd |
CW, I've been looking at the same problem and came up with almost the same way of fine tuning the clock. I stuck with the prescale of 1024 and went to timer1 to use 14400 as the the trigger varying this number to accurize the clock. I found mine runs a little fast using this number. Here's the code I'm using:
But, I've a couple of observations. As you might notice, I'm also tracking Hours and Minutes but do so within the interrupt routine. Could not decide if this would effect the timing but don't think so. Opinions? Also notice line 7. In the downloaded code there were no line feeds and this makes it hard to read and pares. There were several "#include" statements, one after another, leading me to believe that the file may be corrupted. Can someone check that? Thanx Let me know what you think. John |
April 08, 2013 by pcbolt |
Hi John - The code inside the interrupt should be fine. It should execute very quickly. If you are ever really curious about how long it will take to execute you could look at the assembly listing (files that end with *.ass) which can be generated by tweaking the makefile or by typing "make filename.ass". You then just need to count the number of code lines inside the ISR and multiply by 68 nSec. Some assembly instructions take twice as long but you get a general idea. In the "realtimeclock.c" listing the only header file you don't have included is <stdlib.h>. Not sure you really need it in your code though. |
April 08, 2013 by pcbolt |
Hi John - The code inside the interrupt should be fine. It should execute very quickly. If you are ever really curious about how long it will take to execute you could look at the assembly listing (files that end with *.ass) which can be generated by tweaking the makefile or by typing "make filename.ass". You then just need to count the number of code lines inside the ISR and multiply by 68 nSec. Some assembly instructions take twice as long but you get a general idea. In the "realtimeclock.c" listing the only header file you don't have included is <stdlib.h>. Not sure you really need it in your code though. |
April 08, 2013 by pcbolt |
Sorry for the double post. One other thing...the clock is only as accurate as the oscillator which is subject to changes due to temperature, shock, etc. |
Please log in to post a reply.
Did you know that you can connect to certain car computers via the OBD-II port with a microcontroller? Learn more...
|