PIC TIME2R
PIC TIME2R
PIC TIME2R
Timer: A timer is a device that generates a signal pulse at specified time intervals. A time
interval is a "real-time" measure of time, such as 3 milliseconds. These devices are extremely
useful in systems in which a particular action, such as sampling an input signal or generating an
Internally, a simple timer may consist of a register, counter, and an extremely simple controller.
The register holds a count value representing the number of clock cycles that equals the desired
real-time value. This number can be computed using the simple formula:
For example, to obtain a duration of 3 milliseconds from a clock cycle of 10 nanoseconds (100
MHz), we must count (3x10-6 s / 10x10-9 s/cycle) = 300 cycles. The counter is initially loaded
with the count value, and then counts down on every clock cycle until 0 is reached, at which
point an output signal is generated, the count value is reloaded, and the process repeats itself.
A counter is nearly identical to a timer, except that instead of counting clock cycles (pulses on
the clock signal), a counter counts pulses on some other input signal.
PIC Timers
• Some can connect to external clock, some to the processor clock, some to either.
• The device has three readable and writeable hardware timers that can increment automatically
Timer 0
• Timer 1
• Timer 2
Timer0:
Prescaler:
• Prescaler is a name for the part of a microcontroller which divides oscillator clock before it will
• Number which divides a clock is defined through first three bits in OPTION register.
• The highest divisor is 256. This actually means that only at every 256th clock, timer value
to 255.
• During each transition from 255 to zero, T0IF bit in INTCON register is set.
– If interrupts are allowed to occur, this can be taken advantage of in generating interrupts and
– It is up to programmer to reset T0IF bit in interrupt routine, so that new interrupt or new
Figure 5-1 is a block diagram of the Timer0 module and the prescaler shared with the WDT.
Additional information on the Timer0 module is available in the PICmicro™ Mid-Range MCU
Family Reference Manual (DS33023). Timer mode is selected by clearing bit T0CS
(OPTION_REG<5>). In Timer mode, the Timer0 module will increment every instruction cycle
(without prescaler). If the TMR0 register is written, the increment is inhibited for the following
two instruction cycles. The user can work around this by writing an adjusted value to the TMR0
register.
Counter mode is selected by setting bit T0CS (OPTION_REG<5>). In Counter mode, Timer0
will increment either on every rising, or falling edge of pin RA4/T0CKI. The incrementing edge
is determined by the Timer0 Source Edge Select bit, T0SE (OPTION_REG<4>). Clearing bit
T0SE selects the rising edge. The prescaler is mutually exclusively shared between the Timer0
module and the Watchdog Timer. The prescaler is not readable or writable.
Timer0 Interrupt
The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h. This
overflow sets bit T0IF (INTCON<2>). The interrupt can be masked by clearing bit T0IE
(INTCON<5>). Bit T0IF must be cleared in software by the Timer0 module Interrupt Service
Routine before re-enabling this interrupt. The TMR0 interrupt cannot awaken the processor from
When no prescaler is used, the external clock input is the same as the prescaler output. The
synchronization of T0CKI with the internal phase clocks is accomplished by sampling the
prescaler output on the Q2 and Q4 cycles of the internal phase clocks. Therefore, it is necessary
for T0CKI to be high for at least 2Tosc (and a small RC delay of 20 ns) and low for at least
2Tosc (and a small RC delay of 20 ns). Refer to the electrical specification of the desired device.
Prescaler:
There is only one prescaler available, which is mutually exclusively shared between the Timer0
module and the Watchdog Timer. A prescaler assignment for theTimer0 module means that there
is no prescaler for the Watchdog Timer, and vice-versa. This prescaler is not readable or writable
(see Figure 5-1). The PSA and PS2:PS0 bits (OPTION_REG<3:0>) determine the prescaler
assignment and prescale ratio. When assigned to the Timer0 module, all instructions writing to
the TMR0 register (e.g. CLRF 1, MOVWF 1, BSF 1,x....etc.) will clear the prescaler. When
assigned to WDT, a CLRWDT instruction will clear the prescaler along with the Watchdog
Timer1:
• 1, 2, 4, 8 programmable prescaler
• Interrupt on overflow
The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and
TMR1L), which are readable and writable. The TMR1 Register pair (TMR1H:TMR1L)
increments from 0000h to FFFFh and rolls over to 0000h. The TMR1 Interrupt, if enabled, is
generated on overflow, which is latched in interrupt flag bit TMR1IF (PIR1<0>). This interrupt
• As a timer
• As a counter
The operating mode is determined by the clock select bit, TMR1CS (T1CON<1>).
In Timer mode, Timer1 increments every instruction cycle. In Counter mode, it increments on
every rising edge of the external clock input. Timer1 can be enabled/disabled by setting/clearing
control bit TMR1ON (T1CON<0>). Timer1 also has an internal “RESET input”. This RESET
can be generated by either of the two CCP modules (Section 8.0). Register 6-1 shows the Timer1
control register. When the Timer1 oscillator is enabled (T1OSCEN is set), the RC1/T1OSI/CCP2
and RC0/T1OSO/T1CKI pins become inputs. That is, the TRISC<1:0> value is ignored, and
these pins read as ‘0’. Additional information on timer modules is available in the PICmicro™
1 = Oscillator is enabled
0 = Oscillator is shut-off (the oscillator inverter is turned off to eliminate power drain)
When TMR1CS = 1:
When TMR1CS = 0:
This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.
bit 1 TMR1CS: Timer1 Clock Source Select bit
1 = Enables Timer1
0 = Stops Timer1
Timer mode is selected by clearing the TMR1CS (T1CON<1>) bit. In this mode, the input clock
to the timer is FOSC/4. The synchronize control bit T1SYNC (T1CON<2>) has no effect, since
setting of the TMR1CS bit. When Timer1 is being incremented via an external source,
increments occur on a rising edge. After Timer1 is enabled in Counter mode, the module must
rising edge of clock input on pin RC1/T1OSI/CCP2, when bit T1OSCEN is set, or on pin
RC0/T1OSO/T1CKI, when bit T1OSCEN is cleared. If T1SYNC is cleared, then the external
clock input is synchronized with internal phase clocks. The synchronization is done after the
during SLEEP mode, Timer1 will not increment even if the external clock is present, since the
TIMER2
• Interrupt on overflow
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
Figure timer 2
1 = Timer2 is on
0 = Timer2 is off
01 = Prescaler is 4
1x = Prescaler is 16
The prescaler and postscaler counters are cleared when any of the following occurs:
• any device RESET (POR, MCLR Reset, WDT Reset, or BOR) TMR2 is not cleared when
T2CON is written.