The 8051 Microcontroller
and Embedded Systems
Chapter 9
Counter/Timer Programming
From the text by Mazidi & Mazidi (2000)
1
2
Timer-Counters
• The 8051 has 2 Timer-Counters for:
– Interval timing
– Outside event counting
• Timer 0 and Timer 1
• Each is 16-bit made up of two 8-bit
registers, Timer High and Timer Low.
TH0/TL0 TH1/TL1
3
4
TMOD
• Both counters use the TMOD (Timer Mode)
register for configuration.
5
• M1/M0 select the timer mode 0-3.
Modes 0 and 3 will not be discussed in any detail
as they are seldom used.
• C/T –Determines if the timer is acting as an
internal timer (0) or an external event counter (1)
using pin functions T0 & T1 for counting and
external input.
• Gate: Determines whether a hardware gate input.
Pin functions INT0 & INT1 are used for gate
controls.
• MOV TMOD, #01010001B
6
• As an internal timer, the counter will clock
at a rate of Osc/12.
• If the timer is 16-bits, with a clock of
12Mhz, how long will it take to overflow
starting from 0?
7
• A timer must have ways to start and stop it to
make it useful.
• In software, bits TR0 and TR1 (Timer Run) can be
used for control:
SETB TR0 ;Start timer 0
CLR TR0 ; Stop timer 0
• Gate in TMOD can be used as external hardware
control of the timer. INT0 and INT1 are used as
gate inputs – Normally used for external interrupts
- Discussed further in Ch 11.
8
Timer Mode 1: 16-Bit
• In Mode 1, both TL and TH are used for
counting.
MOV TMOD, #00000001B
• Maximum value of 65,535 or FFFFH
• Timer is loaded with a start value:
MOV TH0, #0FFH
MOV TL0, #00H
• Timer is enabled:
SETB TR0
• Upon overflow, TF0 (timer overflow flag)
is set.
9
10
• Toggle (compliment) an LED connected to
P1.1 every 20mSec with a 12MHz clock.
– Select Mode 1 (timer 0 or 1 may be used)
MOV TMOD, #01H
– Find number of counts required based on
the speed of osc/12:
20mSec/1 uS = 20,000 counts
– Subtract from 65,536
65,536 – 20,000 = 45,536
11
– Convert to Hex and load into timer registers:
MOV TH0, #0B1H
MOV TL0, #0E0H
– Enable the timer
SETB TR0
– Wait until flag is set
Wait: JNB TF0, Wait
12
– Compliment LED
CPL P1.1
– Clear the flag
CLR TF0
– Jump back to RELOAD the timer to wait
another 20mS. If timer is not reloaded,
the full count from FFFF to 0 is used.
13
• Write a code to cause a delay of 50mS for
switch debounce.
14
• Using a 12MHz osc, what is the longest
delay that can be directly timed in mode 1?
• How can the time be extended? Write code
to create a delay of 250mS using a timer
and a register.
15
Mode 2: 8-Bit Reload
• 8-Bit timer where only TLx is used for
counting.
• THx is loaded with the same value as TLx
and is used to reload TLx when it
overflows.
• Negative values may be used to subtract
from maximum (MOV TH1, #-20)
16
• What is the longest time that can directly be
timed using mode 2 with a 12MHz clock?
17
• Using Mode 2 and Timer 1, produce a
frequency of 38KHz on P1.2 using a
12MHz clock.
18
Counter Programming
• Instead of being clocked from the oscillator/12,
timers are clock from external inputs:
Timer 0: Pin T0
Timer 1: Pin T1
• Allows counting of external events.
• Bit C/T for the desired timer is set to 1.
MOV TMOD, #01010000B
19
• Develop a program to toggle the yellow
LED every time 6 bottles pass a sensor
connected to T0.
20
• The counter can be used to keep track of
external events, or as a time base for the
timer (such as a 60Hz input for time
keeping).
21
TCON
• TFx and TRx are contained in the TCON
register (Timer/Counter Counter control
register).
22
Gate Bit
• The Gate for Timer 0/1 can be used to allow
hardware to start and stop the timer. One
use would be to measure the length of a
pulse. Pins INT0/1 are used.
23
24