![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
1911
Joined: 28 Feb 2006 Posts: 3 Location: Ireland
|
Timer based Real Time Clock Question / Checking |
Posted: Fri Apr 03, 2009 4:47 am |
|
|
This is my second ever post so please go easy on me.
I am using code posted by ckielstra on a 18F4525 in a clock/timer application
The problem is that I am get some timer driff not much it equates to about 5 minutes fast after 55 days
Would someone be so kind to take a look it the code and see if I am doing anything wrong
Crystal is 4MHz. The capacitors are 33pF (on both sides) 5V power supply . No resistors associated with the frequency components.
I am sorry but I do not have my complier version it hand.
Many thanks
Code: |
#include <18F4525.h>
//RTC variables
#define XTAL_FREQUENCY 4000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4)
// 1 clock tick = 1 instr. cycle = crystal frequency / 4
#USE DELAY (CLOCK=4000000)
#fuses NOWDT,XT ,NOPROTECT,PUT,NOLVP,NOPBADEN,NOXINST,NOBROWNOUT
#INT_TIMER1
void clock_isr()
{
Ticker -= 65536; //Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{
Ticker += TIMER1_FREQUENCY; //Increment ticker by clocks per second
seconds++; //Three local timers for key presses and the like
timeout++;
twoMinTimeOut++;
clock_increment(); //Function to Increment number of seconds and hours ect
}
}
void Initialize_RTC(void)
{
Ticker = TIMER1_FREQUENCY; //Initialize clock counter to number of clocks per second
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); //Initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
// (about 76 times per second)
enable_interrupts( INT_TIMER1 ); // Start RTC
}
void main()
{
Initialize_RTC();
enable_interrupts(GLOBAL);
while (1)
{
//do lots of clock stuff run LCD and alarms and stuff
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Apr 03, 2009 8:19 am |
|
|
Seriously, check your crystal....
This is only an error of 1:15840. 6 parts in 10^5. 60PPM. Now, the first thing to do, is find the quoted accuracy of your crystal, and what loading is specified for this. Off the shelf, many crystals, are only specified with accuracies of 100PPM. In which case, your figure would be quite well 'in spec'. Crystals specifically selected for 'timing' applications, typically have better figures than this (most 'watch' crystals are specified for 20PPM). Add some reasonably temperature control, and you start to push down to 1PPM. Get a high accuracy crystal, and put it into a temperature controlled oven, and you can start pushing down to 0.001PPM (normally specified as 1 in 1E9, as easier to write than tiny fractions of a PPM).
Your figure is totally reasonably for a normal 'off the shelf' crystal, without temperature regulation...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
1911
Joined: 28 Feb 2006 Posts: 3 Location: Ireland
|
|
Posted: Mon Apr 06, 2009 9:41 am |
|
|
Many thanks for you very clear and informative answer .
You are 100% correct.
I had thought the crystal I was using was around ±30 PPM but as it turns out its ±100PPM going to change the crystal and run for another 55 days and see what happens.
Once again Many Thanks |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|