Skip to content

Timer 1 and Timer 2

After Timer 0 (8/16 bit, T0CON, delay with polling or ISR), Topic 8 continues with Timer1 and Timer2. Timer1 drives CCP capture and compare; Timer2 is the PWM time base with period register PR2. Both appear in Practice 3 (frequency meter), Practice 4/6 (PWM, stepper motor), and the PWM module.

Timer1/2 interrupts require PEIE + PIE1 plus GIE — unlike Timer0 which uses only INTCON (Interrupts).

FeatureDetail
RegistersTMR1H:TMR1L, T1CON
Resolution16 bit (65536 counts)
Prescaler1, 2, 4, 8 (T1CKPS1:0)
ClockFosc/4 internal or external on T1CKI (RC0)
InterruptTMR1IE in PIE1, flag TMR1IF in PIR1
Time = (4 / Fosc) × (65536 − TMR1H:TMR1L) × Prescaler

Same idea as Timer0 in 16 bit, but max prescaler 1:8 (not 1:256). For long delays use high prescaler or chain overflows in the ISR.

Bit / fieldFunction
TMR1ON1 = Timer1 active
T1CKPS1:0Prescaler 1:1, 1:2, 1:4, 1:8
T1OSCENExternal low-power oscillator (LP)
T1SYNCSync in counter mode
T1CS0 = Fosc/4; 1 = external source

The CCP module uses Timer1 as time reference:

CCP modeWhat it measures / doesPractice
CaptureLatches TMR1H:L on edge at RC2Pulse width, frequency
CompareTriggers when TMR1 == CCPR1H:LTimed signals

In Practice 3 (frequency meter), Timer1 often measures the time window while Timer0 counts pulses.

config_tmr1:
MOVLW B'00000001' ; TMR1ON=1, prescaler 1:1 (adjust as needed)
MOVWF T1CON
MOVLW HIGH(.65536-.1000) ; reload for N=1000 counts (example)
MOVWF TMR1H
MOVLW LOW(.65536-.1000)
MOVWF TMR1L
BCF PIR1, TMR1IF
BSF PIE1, TMR1IE
BSF INTCON, PEIE
BSF INTCON, GIE
RETURN

Timer2 is an internal 8-bit timer only with period register PR2:

FeatureDetail
CounterTMR2 (8 bit, 0 → PR2 → reset)
PeriodPR2 defines match / overflow
Prescaler1, 4, 16 (T2CKPS1:0)
Postscaler1:1 to 1:16 (TOUTPS3:0) — delays interrupt
InterruptTMR2IE / TMR2IF in PIE1/PIR1
Bit / fieldFunction
TMR2ON1 = Timer2 active
T2CKPS1:0Prescaler 1, 4, 16
TOUTPS3:0Postscaler for TMR2IF interrupt
PWM_Period ≈ (4 / Fosc) × (PR2 + 1) × T2_Prescaler

Duty percentage:

Duty ≈ (CCPR1L : extensions) / (4 × (PR2 + 1)) × 100%

Full detail in PWM and CCP module.

config_tmr2:
MOVLW D'249' ; PR2 → defines period
MOVWF PR2
MOVLW B'00000111' ; TMR2ON, prescaler 1:16 (adjust)
MOVWF T2CON
CLRF TMR2
BCF PIR1, TMR2IF
BSF PIE1, TMR2IE
BSF INTCON, PEIE
BSF INTCON, GIE
RETURN
Timer0Timer1Timer2
Bits8 or 16168 (+ PR2)
Max prescaler1:2561:81:16 (+ post)
IE / IFINTCONPIE1/PIR1PIE1/PIR1
Key useDelay, RA4 counterCCP capture/comparePWM, multiplexing
Practice3, 533, 4, 6

The PIC18F4550 includes Timer3 (similar to Timer1). See the datasheet for advanced projects; the UNEXPO course focuses on Timer0–2.

MistakeConsequence
Timer1/2 without PEIEISR never runs even if TMR1IE=1
Confuse PR2 with TMR2PR2 = desired period; TMR2 counts up to PR2
PWM without active Timer2No waveform on RC2 from CCP1
Wrong prescaler in formulaIncorrect calculated times

Which timer does CCP PWM use?

Timer2 with PR2 as period.

Is Timer1 8 or 16 bits?

16 bits — used in CCP capture and compare.

Which registers enable Timer2 interrupt?

TMR2IE (PIE1), PEIE and GIE (INTCON); clear TMR2IF (PIR1) in the ISR.

Ejercicio · Parcial

Pick the timer

To count pulses on RA4 for 1 s then read the total: Timer0 as counter or Timer2? Who marks the 1 s window?

Interactive exam

Timer 1 and Timer 2

Based on: Clase Tema 8. Timer.pdf

0 of 0 answered

Which timer does the CCP PWM module use?
Timer1 is...
PWM period mainly depends on...
Timer1 needs PEIE because...
PR2 in Timer2 defines...
Typical max prescaler: Timer0 vs Timer1...