PIC18FXX20
EXAMPLE 12-1:
IMPLEMENTING A REAL-TIME CLOCK USING A TIMER1 INTERRUPT SERVICE
RTCinit
movlw
movwf
clrf
0x80
TMR1H
TMR1L
; Preload TMR1 register pair
; for 1 second overflow
movlw
movwf
clrf
b’00001111’
T1OSC
secs
; Configure for external clock,
; Asynchronous operation, external oscillator
; Initialize timekeeping registers
;
clrf
mins
movlw
movwf
bsf
.12
hours
PIE1, TMR1IE
; Enable Timer1 interrupt
return
RTCisr
bsf
bcf
incf
movlw
TMR1H,7
PIR1,TMR1IF
secs,F
; Preload for 1 sec overflow
; Clear interrupt flag
; Increment seconds
.59
; 60 seconds elapsed?
cpfsgt secs
return
; No, done
clrf
incf
movlw
secs
mins,F
.59
; Clear seconds
; Increment minutes
; 60 minutes elapsed?
cpfsgt mins
return
; No, done
clrf
incf
movlw
mins
hours,F
.23
; clear minutes
; Increment hours
; 24 hours elapsed?
cpfsgt hours
return
; No, done
movlw
movwf
return
.01
hours
; Reset hours to 1
; Done
TABLE 12-2: REGISTERS ASSOCIATED WITH TIMER1 AS A TIMER/COUNTER
Value on
all other
RESETS
Value on
Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
POR, BOR
INTCON GIE/GIEH PEIE/GIEL TMR0IE
INT0IE
TXIF
TXIE
TXIP
RBIE
SSPIF
SSPIE
SSPIP
TMR0IF
INT0IF
RBIF
0000 0000 0000 0000
PIR1
PSPIF
PSPIE
PSPIP
ADIF
ADIE
ADIP
RCIF
RCIE
RCIP
CCP1IF TMR2IF TMR1IF 0000 0000 0000 0000
CCP1IE TMR2IE TMR1IE 0000 0000 0000 0000
CCP1IP TMR2IP TMR1IP 0111 1111 0111 1111
PIE1
IPR1
TMR1L
Holding Register for the Least Significant Byte of the 16-bit TMR1 Register
xxxx xxxx uuuu uuuu
TMR1H Holding Register for the Most Significant Byte of the 16-bit TMR1 Register
T1CON RD16
Legend: x= unknown, u= unchanged, - = unimplemented, read as '0'. Shaded cells are not used by the Timer1 module.
xxxx xxxx uuuu uuuu
—
T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON 0-00 0000 u-uu uuuu
2003 Microchip Technology Inc.
Advance Information
DS39609A-page 139