Timers and Counters
Timers and Counters
6 TR1 Timer 1 run control bit. Set to 1 by orogram to enable timer to count: cleared to 0 by
program to halt timer. Does not reset timer
TFO Timer 0 Overflow flag. Set when timer rolls from all ones to zero. Cleared when processor
vectors to execute interrupt service routine located at program address 000Bh.
TRO Timer 0 run control bit. Set to 1 by program to enable timer to count; cleared to 0 by
program to h¡lt timer. Does not reset timer.
IE1 External interrupt 1edge flag. Set to 1when a high to low edge signal is received on port 3
pin 3.3 (INT1). Cleared when processor vectors to interrupt service routine
located at program address 0013h. Not related to timer operations.
2 IT1 External interrupt 1signal type control bit. Set to 1by program to enabie external interrupt 1
to be triggered by a falling edge signal. Set to 0 by program to enable a low level
signalon external interrupt 1 to generate an interrupt.
IE0 External interrupt Oedge flag. Set to I when a high to low edge signal is received on port 3
pin 3.2 (INTO). Cleared when processor vectors to interrupt service routine located at
program address 0003h. Not related to timer operations.
Continued
first 32K RAM (000Oh-7FFFh) can then be enabled when Al5 of port 2 is low, and the
second 32K RAM (8000h - FFFFh) when A15 is high, by using an inverter.
Note that the WR and RD Signals are alternate uses for port 3 pins 16 and 17. AlIso,
port Ois used for the lower address byte and data: port 2 is used for upper address bits. The
use of external memory consumes many of the port pins, leaving only port I and parts of
port 3 for general /0.
MO Gate cà M1 MO
Gate M1
Timer 1 Timer O
5/1 M1 Timer/counter operating mode select bit 1 Set/cleared by program to select mode
4/0 MO Timer/cOUnter operating mode select brt O. Set/cleared by program to select mode
M1 MO Mode
1
2
1 1 3
The counters are divided into two 8-bit registers called the tiner low (TL0, TL) and
high (TH0, THI)bytes. Allcounter action is controlied by bit states in the timer mode
control register (TMOD), the timer/counter control register (TCON), and certain program
instructions.
TMOD is dedicated solely to the two timers and can bc considered to be two duplicate
4-bit registers, cach of which controls the action of one of the timers. TCON has control
bits and flags for the timers in the upper nibble, and control bits and flags for the external
interrupts in the lower nibble. Figure 2. 10 shows the bit assignments for TMOD and TCON.
Timer Counter Interrupts
The counters have been included on the chip to relieve the processor of timing and count
ing chores. When the program wishes to count a certain number of internal pulses or
external events, a number is placed in one of the counters. The number represents the
maximum count less the desired count, plus one. The counter increments from the initial
number to the maximum and then rolls over to zero on the final pulse and also sets a timer
fag. The flag condition may be tested by an instruction to tell the program that the count
has been accomplished, or the flag may be used to interrupt the program.
CHAPTER TWO
30
Oscitlator + 12d
Frequency
INTI/0tnput Pin
Timing
internal clock frequency of the
Ifa counter is programmed to be a timer, it will count the
8051 oscillator divided by 12d. As an example, if the crystal frequency is 6.0 megahertz.
then the timer clock will have a frequency of 500 kilohertz.
Figure
The resultant timer clock is gated to the timer by means of the circuit shown in TMOD
oscillator clock pulses to reach the timer, the C/T bit in the
2.I1. In order for
be set to l
register must be set to 0 (timer operation). Bit TRX in the TCÔN register must must
(timer run), and the gate bit in the TMOD register must be 0, or external pin INTX
1. In other words, the counter is configured as a timer, then the timer pulses are gated
be a
bits INTX.
to the counter by the run bit and the gate bit or the external input
Timer Modes of Operation
the mode bits.
The timers may operate in any one of four modes that are determined by
MI and M0, in the TMOD register. Figure 2.12 shows the four timer modes.
Timer Mode 0
Setting timer X mode bits to 00b in the TMOD register results in using the THX register
so
as an 8-bit counter and TLX as a 5-bit counter; the pulse input is divided by 32d in TL
that TH counts the original oscillator frequency reduced by a total 384d. As an example,
the 6 megahertz oscillator frequency would result in a final frequency to TH of 15625
hertz. The timer flag is set whenever THX goes from FFh to OOh, or in .0164 seconds for
a 6 megahertz crystal if THX starts at 00h.
Timer Mode 1
Mode I is similar to mode 0 except TLX is configured as a full 8-bit counter when the
mode bits are set to 0lb in TMOD. The timer flag would be set in .131lseconds using
a6 megahertz crystal.
THE 8051ARCHITECTURE 31
Modes
FIGURE 2.12 Timer 1and Timer 0 Operation
interrupt
TEX
Pulse
TLX 5 Bits THX 8Bits
Input
(Figure 2.11)
Timer/Counter
Timer Mode O 13- Bit
Reload TY
THX 8 Bits
Pulse TFO
TLO 8 Bits Interrupt
Input
(Figure 2.11)
TRI Bit
In TCON
Timer Mode 3 Two8-Bit Timers Using Timer O
Timer Mode 2
Setting the mode bits to 10b in TMOD configures the timer to Use only the TLX counter as
an 8-bit counter. THX is used to hold a value that is loaded into TLX every time TLX
overfiows from FFh to 00h. The timer flag is also set when TLX overflows.
This mode exhibits an auto-reload feature: TLX will count up from the number in
THX,overflow, and be initialized again with the contents of THX. For example, placing
32 CHAPTERWO