Lecture 7 Timers
Lecture 7 Timers
16F877
RGM 2020 1
Timer Operation
xxxx
TMR1
2
Timer Operation
TMR1 FFFA
11 1 1 1 1 1 1 1 1 1 1 1 01 0
1. Load counter with preset value
3
Timer Operation
TMR1 FFFA
11 1 1 1 1 1 1 1 1 1 1 1 01 0
1. Load counter with preset value
2. Start counter
4
Timing pulse 1
Timer Operation
TMR1 FFFB
11 1 1 1 1 1 1 1 1 1 1 1 01 1
1. Load counter with preset value
2. Start counter
5
Timing pulse 2
Timer Operation
FFFC
TMR1
11 1 1 1 1 1 1 1 1 1 1 1 10 0
1. Load counter with preset value
2. Start counter
6
Timing pulse 3
Timer Operation
FFFD
TMR1
11 1 1 1 1 1 1 1 1 1 1 1 10 1
1. Load counter with preset value
2. Start counter
7
Timing pulse 4
Timer Operation
FFFE
TMR1
11 1 1 1 1 1 1 1 1 1 1 1 11 0
1. Load counter with preset value
2. Start counter
8
Timing pulse 5
Timer Operation
FFFF
TMR1
11 1 1 1 1 1 1 1 1 1 1 1 11 1
1. Load counter with preset value
2. Start counter
9
Timing pulse 6
Timer Operation
0000
TMR1
00 0 0 0 0 0 0 0 0 0 0 0 00 0
1. Load counter with preset value
2. Start counter
3. On overflow: Overflow flag is set
Interrupt generated
10
Source of Pulses
OSCILLATOR 4:1
Timer
Value of source 0
select bit
1
Counter
Prescaler Postscaler
TIMER
1:4 1:8
12
Timers on the PIC16F877
•Timer 0
•Timer 1
•Timer 2
13
Timer 0
• Timer 0 is an 8-bit timer/counter module.
• Overflow of the counter from FFh to 00h
14
Timer 0 Associated SFRs
TMR0 (01h and 101h)
• Used to hold the count value of the timer.
15
Timer 0 Associated SFRs
OPTION_REG (81h and 181h)
TIMER/COUNTER Prescale
selection assignment
Counter Prescale
edge bits
selection
16
Timer 1
• Timer 1 is a 16-bit timer/counter module.
• Overflow of the counter from FFFFh to 0000h
T1CON (10h)
• Contains bits that control operation of Timer 1
PIE1 (8Ch)
• Contains Timer 1 interrupt enable bit
PIR1 (8Ch)
• Contains Timer 1 interrupt flag. 18
Timer 1 Associated SFRs
T1CON (81h and 181h)
Timer
enable bit
Prescale bits
TIMER/COUNTER
selection
Oscillator Synchronization
enable bit enable bit
19
Timer 2
• Timer 2 is an 8-bit period timer – operates only as
timer.
• Has associated period register.
PR2 (92h)
• Holds the period value.
T2CON (12h)
• Contains bits that control operation of Timer 2
PIE1 (8Ch)
• Contains Timer 2 interrupt enable bit
PIR1 (8Ch)
• Contains Timer 2 interrupt flag.
21
Timer 2 Associated SFRs
T2CON (12h)
Prescale bits
Postscale bits Timer
enable bit
22
Interrupt Bits
INTCON
GIE PEIE TMR0IE INTE RBIE TMR0IF INTF RBIF
Timer 0
interrupt
Global enable bit TMR0 interrupt
Interrupt
flag
Enable bits
23
Interrupt Bits
PIE1 (8Ch)
24
Running the Timers
1. Select the clock source (Timer 0 and Timer 1)
2. Select clock edge in counter mode (Timer 0)
3. Set prescale and postscale values and assignments
4. Set synchronisation and oscillator enable (Timer 1)
5. Set the period value (Timer 2)
6. Load the starting value into timer register(s).
7. Enable interrupts if required
8. Put on the timer (Timer 1 and Timer 2).
9. Wait for overflow/match interrupt or poll timer
flag.
25
Tutorial 6
Exercise 4 Q 2
26
Laboratory 7
Truck speedometer
27