Professional Documents
Culture Documents
Timer/Counter
Presented by :
AKASH GUPTA 111257
TMOD Register
Modes of Operation
TCON Register
Counters
Introduction
Introduction
The 8051 comes equipped with two timers, both of
which may be controlled, set, read, and configured
individually.
We have:
TH0 TL0
Timer 0
TH1 TL1
Timer 1
TMOD Register
TMOD Register
Timer mode register = TMOD
An 8-bit register
lower 4 bits : Timer 0 Mode setting (0000 : not used)
upper 4 bits : Timer 1 Mode setting (0000 : not used)
Not bit-addressable
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
BIT NAME EXPLANATION OF THE FUNCTION TIMER
Functions of 7 GATE1
When this bit is set the timer will only run
when INT1 (P3.3) is high. When this bit is
clear the timer will run regardless of the
1
1 : Counter operation
(clock : Tx input pin)
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
Gate
Every timer has a mean of starting and stopping.
GATE = 0
Internal control
The start and stop of the timer are controlled by the software
(Set/clear the TR)
GATE = 1
External control
The hardware way of starting and stopping the timer by
software and an external source.
M0,M1
M0 and M1 select the timer mode for timers 0 & 1.
0 0 0 13-bit Timer
0 1 1 16-bit Timer
The counting:
TLx is incremented from 0(00h) to 255(FFh).
When TLx is incremented from 255, it resets to 0 and
causes THx to be incremented by 1.
Hence we have a maximum count of 65,025 (255*255)
machine cycles.
Mode 1- Steps
1. Choose mode 1 timer 0
MOV TMOD,#01H
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
XTAL
oscillator 12
C/T = 0
Timer
overflow
flag
TH TL TF
TR
4. After TH0 is loaded with the 8-bit value, the 8051 gives a copy of it
to TL0.
TL0=TH0=38H
Mode 2 Steps
5. Start the timer.
SETB TR0
7. When TL0 rolls over from FFH to 00, the 8051 set TF0=1. Also, TL0 is reloaded
automatically with the value kept by the TH0.
TL0= FEH, FFH, 00H (Now TF0=1)
The 8051 auto reload TL0=TH0=38H.
Clr TF0
Go to Step 6 (i.e., TL0 is incrementing continuously).
Note that we must clear TF0 when TL0 rolls over. Thus, we can monitor TF0 in
next process.
XTAL
oscillator 12
C/T = 0
reload
TR1 TH1
Timer Mode 3 (Split Timer)
When Timer 0 is placed in mode 3, it essentially becomes two separate 8-
bit timers.
A good possible use of using split timer mode is if you need to have two
separate timers and, additionally, a baud rate generator.
In such case you can use the real Timer 1 as a baud rate generator
and use TH0/TL0 as two separate timers.
TCON Register
TCON SFR
Finally, there is one more SFR that controls the two
timers and provides valuable information about them.
(MSB) (LSB)
Timer 1 Timer 0
Counter Mode 1
16-bit counter (TH0 and TL0)
TH0-TL0 is incremented when TR0 is set to 1 and an external
pulse (in T0) occurs.
When the counter (TH0-TL0) reaches its maximum of FFFFH, it
rolls over to 0000, and TF0 is raised.
Programmers should monitor TF0 continuously and stop the
counter 0.
Programmers can set the initial value of TH0-TL0 and let TF0=1
as an indicator to show a special condition. (ex: 100 people
have come).
Timer 0 with External Input
(Mode 1)
overflow
flag
Timer 0
external TH0 TL0 TF0
input Pin
3.4
TF0 goes high when
FFFF 0
C/T = 1
TR0
Counter Mode 2
8-bit counter.
It allows only values of 00 to FFH to be loaded into TH0
Auto-reloading
TL0 is incremented if TR0=1 and external pulse occurs.
Thank You!