Timers and Counters Class1 1
Timers and Counters Class1 1
XTAL
oscillator
÷ 12
2
Timer
timer input
XTAL
÷ 12
oscillator
C/T = 0
TH0 TL0
C/T = 1
counter input
T0 Pin
1:start
Pin 3.4
TR0 TF0
0:stop
5
Timer/Counter 1
timer input
XTAL
÷ 12
oscillator
C/T = 0
TH1 TL1
C/T = 1
counter input
T1 Pin
Pin 3.5 1:start
TR1 TF1
0:stop
6
Registers Used in Timer/Counter
7
Basic Registers of the Timer
8
Timer Registers
TH0 TL0
Timer 0
TH1 TL1
Timer 1
9
TCON Register (1/2)
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
10
TCON Register (2/2)
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
11
Equivalent Instructions for the Timer
Control Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
13
TMOD Register
15
Gate
• Every timer has a mean of starting and stopping.
– GATE=0
• Internal control
• The start and stop of the timer are controlled by software.
• Set/clear the TR0 (or TR1) for start/stop timer.
– GATE=1
• External control
• The hardware way of starting and stopping the timer by software
and an external source.
• Timer/counter is enabled only while the INT0 (or INT1) pin has an
1 to 0 transition and the TR0 (or TR1) control pin is set.
• INT0: P3.2, pin 12; INT1: P3.3, pin 13.
16
M1, M0
timer 1 timer 0
20
Mode 1 Programming
XTAL
oscillator ÷ 12 like MC for
89C51
C/T = 0
TH TL TF
Start timer
TF goes high overflow
TR when FFFF 0 flag
21
Steps of Mode 1 (1/3)
22
Steps of Mode 1 (2/3)
5. The 8051 starts to count up by incrementing the TH0-
TL0.
– TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
25
Timer Delay Calculation for XTAL =
11.0592 MHz
26
Find Timer Values
31