Unit 2 - Counter-Timer (Compatibility Mode)
Unit 2 - Counter-Timer (Compatibility Mode)
Chapter 9
Counter/Timer Programming
1
2
Timer-Counters
• The 8051 has 2 Timer-Counters for:
– Interval timing
– Outside event counting
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.
7
• A timer must have ways to start and stop it to
make it useful.
12
– Compliment LED
CPL P1.1
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?
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