If you're presetting TMR0 it will be very difficult to keep it accurate, free running mode is best. Roman Black has an article on this. Official Home Page of Roman Black
PS TMR1 has a wonderful connection to the CCP1 special interrupt (mode 14) which can form an excellent timebase for a clock.
If you're presetting TMR0 it will be very difficult to keep it accurate, free running mode is best. Roman Black has an article on this. Official Home Page of Roman Black
PS TMR1 has a wonderful connection to the CCP1 special interrupt (mode 14) which can form an excellent timebase for a clock.
The problem with reloading TMR0 is it resets the prescaler and other such stuff. Allowing it to freerun is the better way to do it. It'll set TMR0IF on overflow and use Romans zero error clock algorithm for getting a 1 second timebase.
Usually you just don't reload a timer with a calculated value. You add the current Timer value + your calculated value + amount of cycle it need to reload your timer.
HTH
TMR0=TMR0 + ReloadValue + SomeCallItFudgeFactor
From the datasheet said:
If the TMR0 register
is written, the increment is inhibited for the following
two instruction cycles. The user can work around this
by writing an adjusted value to the TMR0 register.
If you set the prescaler to 32 then the timer will overflow every 8192 cycles - close to your current 10,000. Use Roman Blacks code to subtract 32 from the middle byte and add the 1,000,000 when the top bytes become zero. This will give you a very accurate clock.
If you set the prescaler to 32 then the timer will overflow every 8192 cycles - close to your current 10,000. Use Roman Blacks code to subtract 32 from the middle byte and add the 1,000,000 when the top bytes become zero. This will give you a very accurate clock.
If you set timer2s postscaler to 10 you will get an interrupt at 250Hz which you could use for the clock timebase. This will not effect your PWM output at all. To turn off the 2.5kHz signal set the pulse width to zero.
If you set timer2s postscaler to 10 you will get an interrupt at 250Hz which you could use for the clock timebase. This will not effect your PWM output at all. To turn off the 2.5kHz signal set the pulse width to zero.
At the moment I'm studying roman blacks original code.I have got stuck with that.Sorry about that.
First it loads accumulator to 100000+256=100256uS.
Every 256uS it generates interrupt.& every 256uS interrupts he subtract 256 from accumulator.
When continue subtracting theres a point coming you cannot subtract anymore.That is 100096uS.So balance is 160uS remaining (100256uS-100096uS =160uS) .
Roman Blacks code simply subtracts 256 from a 24 bit counter by decrementing the middle byte. Whenever the value goes below 256 (top two bytes are zero), he adds 1,000,000 to the 24 bit counter. This means that, on average, the seconds will be be incremented every 1,000,000 cycles. The actual seconds will vary slightly (by up to 255uS) but overtime the errors cancel out and so it is very accurate.
The code I posted above does the same thing except I subtract 8192 by subtracting 32 from the middle byte. This is simply because I didn't like the idea of an interrupt every 256 cycles.
Roman Blacks code simply subtracts 256 from a 24 bit counter by decrementing the middle byte. Whenever the value goes below 256 (top two bytes are zero), he adds 1,000,000 to the 24 bit counter.
The code I posted above does the same thing except I subtract 8192 by subtracting 32 from the middle byte. This is simply because I didn't like the idea of an interrupt every 256 cycles.
This means that, on average, the seconds will be be incremented every 1,000,000 cycles. The actual seconds will vary slightly (by up to 255uS) but overtime the errors cancel out and so it is very accurate.
Hi Mike thanks for spending your time on this.Actually it is really helpful.I'm analizing your results.
One more question.
At start how did you get 64 remainder?From what value did you divide?
Roman Black initialize his accumulator with 100256uS on power up.
hi Suraj,
A related point to the clock 'accuracy', what range of ambient temperatures is the 16F88 working over.
Either the internal osc or the external osc if applicable.