Device = 18F452
Clock = 20
Dim TMR2IE As PIE1.1, // TMR2 interrupt enable
TMR2IF As PIR1.1, // TMR2 overflow flag
TMR2ON As T2CON.2, // Enables TMR2 to begin incrementing
mS As Word, // mS register
S As Byte, // Seconds register
M As Byte // Minutes register
Interrupt TMR2_Interrupt()
Save(0) // Back up system variables
If TMR2IF = 1 Then // Check if the interrupt was from TMR2
TMR2IF = 0 // Clear the TMR2 interrupt flag
Inc(mS) // Increment the mS counter
If mS = 1000 Then
mS = 0
Inc(S)
If S = 60 Then
S = 0
Inc(Min)
EndIf
EndIf
EndIf //
Restore // Restore system variables
End Interrupt
Private Sub TMR2_Initialize()
TMR2ON = 0 // Disable TMR2
TMR2IE = 0 // Turn off TMR2 interrupts
T2CON.0 = 1 // 00 = Prescaler is 1
T2CON.1 = 0 // 01 = Prescaler is 4
// 1x = Prescaler is 16
PR2 = 249 // TMR2 Period register PR2
T2CON.3 = 0 // 0000 = 1:1 postscale
T2CON.4 = 0 // 0001 = 1:2 postscale
T2CON.5 = 1 // 0010 = 1:3 postscale...
T2CON.6 = 0 // 1111 = 1:16 postscale
TMR2 = 0 // Reset TMR2 Value
TMR2IE = 1 // Enable TMR2 interrupts
TMR2ON = 1 // Enable TMR2 to increment
Enable(TMR2_Interrupt)
End Sub
// Start Of Program...
mS = 0 // Reset Timer registers
S = 0
M = 0
TMR2_Initialize // Setup and enable TMR2