Datasheet
PIC16F818/819
DS39598C-page 62 Preliminary 2002 Microchip Technology Inc.
EXAMPLE 7-3: IMPLEMENTING A REAL-TIME CLOCK USING A TIMER1 INTERRUPT SERVICE
TABLE 7-2: REGISTERS ASSOCIATED WITH TIMER1 AS A TIMER/COUNTER
RTCinit banksel TMR1H
movlw 0x80 ; Preload TMR1 register pair
movwf TMR1H ; for 1 second overflow
clrf TMR1L
movlw b’00001111’ ; Configure for external clock,
movwf T1CON ; Asynchronous operation, external oscillator
clrf secs ; Initialize timekeeping registers
clrf mins
movlw .12
movwf hours
banksel PIE1
bsf PIE1, TMR1IE ; Enable Timer1 interrupt
return
RTCisr banksel TMR1H
bsf TMR1H,7 ; Preload for 1 sec overflow
bcf PIR1,TMR1IF ; Clear interrupt flag
incf secs,F ; Increment seconds
movf secs,w
sublw .60
btfss STATUS,Z ; 60 seconds elapsed?
return ; No, done
clrf seconds ; Clear seconds
incf mins,f ; Increment minutes
movf mins,w
sublw .60
btfss STATUS,Z ; 60 seconds elapsed?
return ; No, done
clrf mins ; Clear minutes
incf hours,f ; Increment hours
movf hours,w
sublw .24
btfss STATUS,Z ; 24 hours elapsed?
return ; No, done
clrf hours ; Clear hours
return ; Done
Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Value on
POR, BOR
Value on
all other
RESETS
0Bh,8Bh,
10Bh,18Bh
INTCON GIE PEIE
TMR0IE INTE RBIE TMR0IF INTF RBIF 0000 000x 0000 000u
0Ch PIR1
— ADIF — — SSPIF CCP1IF TMR2IF TMR1IF -0-- 0000 -0-- 0000
8Ch PIE1
— ADIE — — SSPIE CCP1IE TMR2IE TMR1IE -0-- 0000 -0-- 0000
0Eh TMR1L Holding Register for the Least Significant Byte of the 16-bit TMR1 Register xxxx xxxx uuuu uuuu
0Fh TMR1H Holding Register for the Most Significant Byte of the 16-bit TMR1 Register xxxx xxxx uuuu uuuu
10h T1CON
— — T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON --00 0000 --uu uuuu
Legend: x = unknown, u = unchanged, - = unimplemented, read as ‘0’. Shaded cells are not used by the Timer1 module.