CHAPTER 5 : TIMERS AND I/O
PROGRAMMING
Subtopic
5.1 How the timer in 8051 works
5.2 TMOD SFR and TCON SFR
5.3 Initializing and reading of timer
5.1 How the timer in 8051 works
3
TIMERS
A timer is a specialized type of clock which is used to
measure time intervals. A timer that counts from zero
upwards for measuring time elapsed is often called
a stopwatch. It is a device that counts down from a
specified time interval and used to generate a time
delay, for example, an hourglass is a timer.
4
COUNTER
• A counter is a device that stores (and sometimes displays)
the number of times a particular event or process
occurred, with respect to a clock signal.
• It is used to count the events happening outside the
microcontroller. In electronics, counters can be
implemented quite easily using register-type circuits such
as a flip-flop.
Difference between a Timer and a Counter
The points that differentiate a timer from a counter are
as follows:
Timer Counter
The register incremented for every The register is incremented considering
machine cycle. 1 to 0 transition at its corresponding to
an external input pin (T0, T1).
Maximum count rate is 1/12 of the Maximum count rate is 1/24 of the
oscillator frequency. oscillator frequency.
A timer uses the frequency of the A counter uses an external signal to
internal clock, and generates delay. count pulses.
5.2 TMOD SFR and TCON SFR
7
TMOD Register
TMOD is an 8-bit register in which lower 4 bits are for Timer 0 and
upper 4 bits are for Timer 1.
Lower 2 bits are used to set the timer mode while upper 2 bits is used
to specify the operation.
Timer 0 and Timer 1 are 16 bit wide.
8
TCON Register
Timer Control or TCON Register is used to start or stop the Timers
of 8051 Microcontroller.
It also contains bits to indicate if the Timers has overflowed.
The TCON SFR also consists of Interrupt related bits.
9
8051 Timer
8051 Modes
TIMERS
Timer 0 Timer 1
Mode 0 Mode 0
Mode 1 Mode 1
Mode 2 Mode 2
Mode 3
8051 Microcontroller 10
TIMER 0
OSC ÷12
C / T 0
TL0 TH0 TF0
C / T 1
T 0 PIN
TR 0 INTERRUPT
Gate
INT 0 PIN
TIMER 0 – Mode 0
13 Bit Timer / Counter
OSC ÷12
TL0 TH0
TF0
C / T 0
INTERRUPT
(5 Bit) (8 Bit)
C / T 1
T 0 PIN
TR 0
Gate
INT 0 PIN
Maximum Count = 1FFFh (0001111111111111)
TIMER 0 – Mode 1
16 Bit Timer / Counter
OSC ÷12
TL0 TH0
TF0
C / T 0
INTERRUPT
(8 Bit) (8 Bit)
C / T 1
T 0 PIN
TR 0
Gate
INT 0 PIN
Maximum Count = FFFFh (1111111111111111)
TIMER 0 – Mode 2
8 Bit Timer / Counter with AUTORELOAD
OSC ÷12
TL0 TH0
TF0
C / T 0
INTERRUPT
(8 Bit) (8 Bit)
C / T 1
T 0 PIN
TR 0
Gate
Reload
INT 0 PIN
TH0
(8 Bit)
Maximum Count = FFh (11111111)
TIMER 0 – Mode 3
Two - 8 Bit Timer / Counter
OSC ÷12
TL0
TF0
C / T 0
INTERRUPT
(8 Bit)
C / T 1
T 0 PIN
TR 0
Gate
INT 0 PIN
OSC ÷12 TH0
TF1 INTERRUPT
(8 Bit)
TR1
TIMER 1
OSC ÷12
C / T 0
TL1 TH1 TF1
C / T 1
T 1PIN
TR1 INTERRUPT
Gate
INT 1 PIN
TIMER 1 – Mode 0
13 Bit Timer / Counter
OSC ÷12
TL1 TH1
TF1
C / T 0
INTERRUPT
(5 Bit) (8 Bit)
C / T 1
T 1PIN
TR1
Gate
INT 1 PIN
Maximum Count = 1FFFh (0001111111111111)
TIMER 1 – Mode 1
16 Bit Timer / Counter
OSC ÷12
TL1 TH1
TF1
C / T 0
INTERRUPT
(8 Bit) (8 Bit)
C / T 1
T 1PIN
TR1
Gate
INT 1 PIN
Maximum Count = FFFFh (1111111111111111)
TIMER 1 – Mode 2
8 Bit Timer / Counter with AUTORELOAD
OSC ÷12
TL1 TH1
TF1
C / T 0
INTERRUPT
(8 Bit) (8 Bit)
C / T 1
T 1PIN
TR1
Gate
Reload
INT 1 PIN
TH1
(8 Bit)
Maximum Count = FFh (11111111)
5.3 Initializing and reading of timer
20
Initializing a Timer
How do microcontroller timers work?
• Microcontrollers require clocks so the programs can be
executed in rhythm with the clock. Like the programs
we are writing, as each clock tick passes, instructions
are processed in time with the ticks of the clock.
• The timer and counter functions in the microcontroller
count in sync with the microcontroller clock.
• Timers are used to measure specific time intervals.
But in electrical engineering terms, timers are also
referred to as counters often. The timer is a
component which is extensively used in different
embedded systems. They are used to keep a record
of time for different events occurring in the
embedded systems.