What clocks, there is no trim for TMR0. What PIC are you using?
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.
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.
Mike.
movlw .32 ;subtract 32*256 from accumulator
subwf AccHi,F
skpnc
goto DoneTimer
decfsz AccUp,F
goto DoneTimer
;only gets here if 1 second has passed
movlw low(.1000000) ;add 1 million to the accumulator
addwf AccLo,f
movlw high(.1000000)
skpnc
addlw 1
addwf AccHi,f
skpnc
incf AccUp,F
movlw upper(.1000000)
addwf AccUp,F
DoneTimer bcf INTCON,TMR0IF
I need a 2.5Khz separate frequency to drive a speaker.So I using PWM module.Thats why I cannot use TMR2.
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.
Mike.
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.
Start Remainder Time Plus 1 Million Total Time
1000000 64 0.999936 1000064 0.999936
1000064 128 0.999936 1000128 1.999872
1000128 192 0.999936 1000192 2.999808
1000192 0 1.000192 1000000 4
1000000 64 0.999936 1000064 4.999936
1000064 128 0.999936 1000128 5.999872
1000128 192 0.999936 1000192 6.999808
1000192 0 1.000192 1000000 8
1000000 64 0.999936 1000064 8.999936
1000064 128 0.999936 1000128 9.999872
1000128 192 0.999936 1000192 10.999808
1000192 0 1.000192 1000000 12
1000000 64 0.999936 1000064 12.999936
1000064 128 0.999936 1000128 13.999872
1000128 192 0.999936 1000192 14.999808
1000192 0 1.000192 1000000 16
1000000 64 0.999936 1000064 16.999936
1000064 128 0.999936 1000128 17.999872
1000128 192 0.999936 1000192 18.999808
1000192 0 1.000192 1000000 20
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?