PIC16F818/819
EXAMPLE 7-3:
IMPLEMENTING A REAL-TIME CLOCK USING A TIMER1 INTERRUPT SERVICE
RTCinit
BANKSEL
TMR1H
MOVLW
MOVWF
CLRF
0x80
TMR1H
TMR1L
; Preload TMR1 register pair
; for 1 second overflow
MOVLW
MOVWF
CLRF
b’00001111’
T1CON
secs
; Configure for external clock,
; Asynchronous operation, external oscillator
; Initialize timekeeping registers
CLRF
mins
MOVLW
MOVWF
BANKSEL
BSF
.12
hours
PIE1
PIE1, TMR1IE
; Enable Timer1 interrupt
RETURN
BANKSEL
BSF
BCF
INCF
RTCisr
TMR1H
TMR1H, 7
PIR1, TMR1IF
secs, F
secs, w
.60
; Preload for 1 sec overflow
; Clear interrupt flag
; Increment seconds
MOVF
SUBLW
BTFSS
RETURN
CLRF
INCF
MOVF
SUBLW
BTFSS
RETURN
CLRF
INCF
MOVF
STATUS, Z
; 60 seconds elapsed?
; No, done
; Clear seconds
; Increment minutes
seconds
mins, f
mins, w
.60
STATUS, Z
; 60 seconds elapsed?
; No, done
; Clear minutes
; Increment hours
mins
hours, f
hours, w
.24
SUBLW
BTFSS
RETURN
CLRF
STATUS, Z
; 24 hours elapsed?
; No, done
; Clear hours
; Done
hours
RETURN
TABLE 7-2:
REGISTERS ASSOCIATED WITH TIMER1 AS A TIMER/COUNTER
Value on
all other
Resets
Value on
POR, BOR
Address
Name
Bit 7 Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
0Bh,8Bh, INTCON GIE PEIE TMR0IE
10Bh,18Bh
INTE
RBIE
TMR0IF
INTF
RBIF
0000 000x 0000 000u
0Ch
PIR1
PIE1
—
—
ADIF
ADIE
—
—
—
—
SSPIF
SSPIE
CCP1IF TMR2IF TMR1IF -0-- 0000 -0-- 0000
CCP1IE TMR2IE TMR1IE -0-- 0000 -0-- 0000
8Ch
0Eh
TMR1L Holding Register for the Least Significant Byte of the 16-bit TMR1 Register
TMR1H Holding Register for the Most Significant Byte of the 16-bit TMR1 Register
xxxx xxxx uuuu uuuu
xxxx xxxx uuuu uuuu
0Fh
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.
DS39598E-page 62
2004 Microchip Technology Inc.