Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
The soft' trim function I mentioned is used to compensate for a crystal which may be slightly off frequency. Any crystal, cardinal, or power-of-2. You do realize that your 3.2768 MHz crystal might actually oscillate at 3,276,400 Hz, right? Or a 16.0 MHz crystal might actually oscillate at 16,000,200 Hz, right? In fact, not one PIC crystal oscillator I've ever measured on my Icom ProII receiver with its 0.5ppm timebase has been "dead on" the correct frequency.The reasoning in these forums was that if you used a cardinal frequency that was not a multiple of 2 then your interrupt would have to have this 'soft' trim function you've mentioned.
If you're talking about inserting a value into a free running timer register during an interrupt then yes, there could be a problem if you don't do it the right way. There are plenty of examples for correctly reloading Timer 0 on a PIC by adding the offset value plus 2 cycles to the free running timer register. I do agree that this isn't the best or easiest method for generating a 1 msec periodic interrupt, and trying to add values to a free running 16 bit timer is a nightmare (don't do it, grin).There would be some error as a new timer countdown was loaded to accommodate the division because the albeit small amount of time the micro took to perform the reload would be an error....missing time as it were. It can be 'trimmed' out yes, manually by observation or counting instructions and adjusting the countdown accordingly.... a whole bunch of ways. Search for odd and evens algorithms if you have some spare time.
This is a misconception. For example, your power-of-2 crystal may actually oscillate at 32,760 Hz which explains why a real time clock derived from that oscillator may be off several seconds per week or month. In short, a power-of-2 crystal isn't necessarily any more accurate than another crystal.There are lots of different techniques of varying complexity to get accuracy...some very complex and mind bending..too hard for me. Personally I preferred the method where you just add a multiple of 2 crystal and let the internal counter run freely....thus avoiding any adjustment in software. Of course this means using up another two pins on your micro especially for an RTC (assuming your micro has a low frequency oscillator).
void interrupt_rtc() // 1 msec Timer 2 interrupts
{ pir1.TMR2IF = 0 // clear timer 2 interrupt flag
[COLOR=Magenta] if(correction_ctr) // if counts remaining this 1 sec period then
{ tmr2 =+ correction_val; // add +1 or -1 Tcy to Timer 2 and
correction_ctr--; // decrement counter
}
[/COLOR]
...
rtc--; // decrement 1000 msec counter
if(rtc == 0) // if 1 second interval then
{ rtc = 1000; // reset 1000 msec counter and
[COLOR=Magenta]correction_ctr = 44; // reset 1 sec correction counter and[/COLOR]
... // perform all once-per-second functions
} //
}