18F Code Snippet - TMR2

Here's an extract from the 18F452 datasheet;
Timer2 is an 8-bit timer with a prescaler and a postscaler. It
can be used as the PWM time base for the PWM mode of the CCP module(s). The TMR2
register is readable and writable and is cleared on any device Reset. The input
clock (FOSC/4) has a prescale option of 1:1, 1:4 or 1:16, selected by control
bits T2CKPS1:T2CKPS0 (T2CON<1:0>).
The Timer2 module has an 8-bit period register, PR2. Timer2
increments from 00h until it matches PR2 and then resets to 00h on the next
increment cycle. PR2 is a readable and writable register. The PR2 register is
initialized to FFh upon Reset.
The match output of TMR2 goes through a 4-bit postscaler (which
gives a 1:1 to 1:16 scaling inclusive) to generate a TMR2 interrupt (latched in
flag bit, TMR2IF (PIR1<1>)). Timer2 can be shut-off by clearing control bit,
TMR2ON (T2CON<2>), to minimize power consumption.
So basically the
TMR2 will continue to cycle until it equals the register PR2, how fast it cycles
depends on the pre and post scaler settings. At this stage an
overflow flag is triggered, and the interrupt will be called. Great for small
delays and PWM generation. Here's an example of how to set it up, the settings
for PR2 and prescalers were found
using the TMR2 calculator

Note the PIC's power supply/oscillator are not shown
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
Last_mS As Word, // mS polling register
Signal_Pin As PORTB.0 // Signal output to frequency meter
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
EndIf //
Restore // Restore system variables
End Interrupt
Private Sub TMR2_Initialize()
TMR2ON = 0 // Disable TMR2
TMR2IE = 0 // Turn off TMR2 interrupts
PR2 = 249 // TMR2 Period register PR2
T2Con = %01111011 // T2CON 0:1 = Prescale
// 00 = Prescaler is 1:1
// 01 = Prescaler is 1:4
// 1x = Prescaler is 1:16
// 3:6 = Postscale
// 0000 = 1:1 postscale
// 0001 = 1:2 postscale
// 0010 = 1:3 postscale...
// 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 mS register
Last_mS = 0 // Reset mS polling register
TMR2_Initialize // Setup and enable TMR2
Low(Signal_Pin) // Make the signal an output, and set it low
While 1 = 1 // Create an infinite loop
Repeat // Poll the mS register and wait for a change
Until mS <> Last_mS //
Last_mS = mS // Update the polling register
Signal_Pin = 1 // Create a pulse for the frequency counter
Signal_Pin = 0
Wend

 | Site Tutorial Index |
|  | 16F PIC Examples |
|  | 18F PIC Examples |
| |  | 7 Segment Displays |
| |  | 7 Segment Displays |
| |  | RS232 and UART |
| |  | Code Snippets |
| | |  | Internal Timers |
| | |  | Interrupts |
|  | Handy Tips |