hi,
The original code uses TMR0 interrupt.
With a 3.2768mHz crystal, the PIC divides this by 4, giving 819,200Hz.
The 809,200 is divided by 128, giving a Tmr0 input of 6400Hz.
As Tmr0 is a 8 bit timer it divides the 6400Hz by 256, giving a 25Hz output, which raises a Tmr0 interrupt every 0.04Secs.
A software counter, counts 25 of these Interrupts in order to give a One second pulse for the real time clock display.
The problem is that using a 4MHz crystal means that a precise 1 second cannot be calculated by using integer maths in the program.
I wonder if you couldn't use the CCP module "special event trigger" mode instead of TMR0 (with 4-MHz Fosc)? It uses the 16-bit TMR1 module which could be setup to roll-over at 40000 microsecond intervals and would allow using the original CLKIN code with only a minor change (for interrupt flags);
Code:
;********* CLOCK UPDATING
CLKIN: btfss ALARMSTATUS,STROBE ; clock only used if strobe active
return ; allowing elapsed time since first zone triggered to be shown
; btfss INTCON,2 ; has timer rollover occurred?
btfss PIR1,CCP1IF ; 40-ms rollover? yes, skip, else
return ; done
; bcf INTCON,2 ; yes, clear timer flag
bcf PIR1,CCP1IF ; clear CCP1 interrupt flag bit
decfsz CLKCNT,F ; increment clock routine. Is it = 0?
return ; no
You would however need to add some initialization code;
If I'm not mistaken, there's a TMR2 solution, too. Setting up TMR2 with prescale 16, postscale 10, and PR2 = 249 will produce the same 40000-usec overflows that the CLKIN routine is looking for. Again, CLKIN is modified to handle the correct timer interrupt flags and you would need to initialize the TMR2 module;
Code:
;********* CLOCK UPDATING (TMR2)
CLKIN: btfss ALARMSTATUS,STROBE ; clock only used if strobe active
return ; allowing elapsed time since first zone triggered to be shown
; btfss INTCON,2 ; has timer rollover occurred?
btfss PIR1,TMR2IF ; 40-ms rollover? yes, skip, else
return ; done
; bcf INTCON,2 ; yes, clear timer flag
bcf PIR1,TMR2IF ; clear TMR2 interrupt flag bit
decfsz CLKCNT,F ; increment clock routine. Is it = 0?
return ; no