Class 5 Timer Handout
Class 5 Timer Handout
Class 5 Timer Handout
22. TIMERS
The C8051F020/1/2/3 devices contain 5 counter/timers: three are 16-bit counter/timers compatible with those found
in the standard 8051, and two are 16-bit auto-reload timers for use with the ADCs, SMBus, UART1, or for general
purpose use. These can be used to measure time intervals, count external events and generate periodic interrupt
requests. Timer 0 and Timer 1 are nearly identical and have four primary modes of operation. Timer 2 offers addi-
tional capabilities not available in Timers 0 and 1. Timer 3 is similar to Timer 2, but without the capture or Baud Rate
Generator modes. Timer 4 is identical to Timer 2, and can supply baud-rate generation capabilities to UART1.
When functioning as a timer, the counter/timer registers are incremented on each clock tick. Clock ticks are derived
from the system clock divided by either one or twelve as specified by the Timer Clock Select bits (T4M-T0M) in
CKCON, shown in Figure 22.1. The twelve-clocks-per-tick option provides compatibility with the older generation
of the 8051 family. Applications that require a faster timer can use the one-clock-per-tick option.
When functioning as a counter, a counter/timer register is incremented on each high-to-low transition at the selected
input pin. Events with a frequency of up to one-fourth the system clock's frequency can be counted. The input signal
need not be periodic, but it should be held at a given level for at least two full system clock cycles to ensure the level
is sampled.
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
- T4M T2M T1M T0M Reserved Reserved Reserved 00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
0x8E
The TH0 register holds the eight MSBs of the 13-bit counter/timer. TL0 holds the five LSBs in bit positions TL0.4-
TL0.0. The three upper bits of TL0 (TL0.7-TL0.5) are indeterminate and should be masked out or ignored when read-
ing. As the 13-bit timer register increments and overflows from 0x1FFF (all ones) to 0x0000, the timer overflow flag
TF0 (TCON.5) is set and an interrupt will occur if enabled.
The C/T0 bit (TMOD.2) selects the counter/timer's clock source. Clearing C/T selects the system clock as the input
for the timer. When C/T0 is set to logic 1, high-to-low transitions at the selected input pin (T0) increment the timer
register. (Refer to Section “17.1. Ports 0 through 3 and the Priority Crossbar Decoder” on page 163 for informa-
tion on selecting and configuring external I/O pins for digital peripherals.)
Setting the TR0 bit (TCON.4) enables the timer when either GATE0 (TMOD.3) is 0 or the input signal /INT0 is
logic-level one. Setting GATE0 to logic 1 allows the timer to be controlled by the external input signal /INT0, facili-
tating pulse width measurements.
Setting TR0 does not reset the timer register. The timer register should be initialized to the desired value before
enabling the timer.
TL1 and TH1 form the 13-bit register for Timer 1 in the same manner as described above for TL0 and TH0. Timer 1
is configured and controlled using the relevant TCON and TMOD bits just as with Timer 0.
12 0
SYSCLK 0
1
TF1
T0 TR1
TCLK TL0 TH0 TF0 Interrupt
Crossbar TR0
(5 bits) (8 bits) IE1
TCON
IT1
/INT0 TR0 IE0
IT0
GATE0
CKCON TMOD
TTTT G C T T G C T T
4 2 1 0 A / 1 1 A / 0 0
MMMM T T MM T T MM
E 1 1 0 E 0 1 0
1 0
12 0
SYSCLK 0
T0 TF1
TCLK TL0 TR1
Crossbar TF0 Interrupt
(8 bits) TR0
IE1
TCON
/INT0 TR0 IT1
IE0
IT0
GATE0
TH0 Reload
(8 bits)
Timer 1 is inactive in Mode 3, so with Timer 0 in Mode 3, Timer 1 can be turned off and on by switching it into and
out of its Mode 3. When Timer 0 is in Mode 3, Timer 1 can be operated in Modes 0, 1 or 2, but cannot be clocked by
external signals nor set the TF1 flag and generate an interrupt. However, the Timer 1 overflow can be used to gener-
ate the baud clock for UART0 and/or UART1. Refer to Section “20. UART0” on page 205 and Section
“21. UART1” on page 215 for information on configuring Timer 1 for baud rate generation.
12 0
TR1 TH0
SYSCLK TF1 Interrupt
(8 bits) TR1
TF0 Interrupt
1 TR0
0 IE1
TCON
IT1
IE0
IT0
T0
TL0
Crossbar
(8 bits)
/INT0 TR0
GATE0
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
(bit addressable) 0x88
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0 00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
0x89
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
0x8A
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
0x8B
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
0x8C
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
0x8D
Cleared by HW?
Bit addressable?
Interrupt Priority Enable Priority
Interrupt Source Pending Flag
Vector Order Flag Control
Always Always
Reset 0x0000 Top None N/A N/A
Enabled Highest
External Interrupt 0 (/INT0) 0x0003 0 IE0 (TCON.1) Y Y EX0 (IE.0) PX0 (IP.0)
Timer 0 Overflow 0x000B 1 TF0 (TCON.5) Y Y ET0 (IE.1) PT0 (IP.1)
External Interrupt 1 (/INT1) 0x0013 2 IE1 (TCON.3) Y Y EX1 (IE.2) PX1 (IP.2)
Timer 1 Overflow 0x001B 3 TF1 (TCON.7) Y Y ET1 (IE.3) PT1 (IP.3)
RI0 (SCON0.0)
UART0 0x0023 4 Y ES0 (IE.4) PS0 (IP.4)
TI0 (SCON0.1)
Timer 2 Overflow (or EXF2) 0x002B 5 TF2 (T2CON.7) Y ET2 (IE.5) PT2 (IP.5)
ESPI0 PSPI0
Serial Peripheral Interface 0x0033 6 SPIF (SPI0CN.7) Y
(EIE1.0) (EIP1.0)
ESMB0 PSMB0
SMBus Interface 0x003B 7 SI (SMB0CN.3) Y
(EIE1.1) (EIP1.1)
AD0WINT EWADC0 PWADC0
ADC0 Window Comparator 0x0043 8 Y
(ADC0CN.2) (EIE1.2) (EIP1.2)
CF (PCA0CN.7)
EPCA0 PPCA0
Programmable Counter Array 0x004B 9 CCFn Y
(EIE1.3) (EIP1.3)
(PCA0CN.n)
CP0FIF ECP0F PCP0F
Comparator 0 Falling Edge 0x0053 10
(CPT0CN.4) (EIE1.4) (EIP1.4)
CP0RIF ECP0R PCP0R
Comparator 0 Rising Edge 0x005B 11
(CPT0CN.5) (EIE1.5) (EIP1.5)
CP1FIF ECP1F PCP1F
Comparator 1 Falling Edge 0x0063 12
(CPT1CN.4) (EIE1.6) (EIP1.6)
CP1RIF ECP1R PCP1F
Comparator 1 Rising Edge 0x006B 13
(CPT1CN.5) (EIE1.7) (EIP1.7)
ET3 PT3
Timer 3 Overflow 0x0073 14 TF3 (TMR3CN.7)
(EIE2.0) (EIP2.0)
AD0INT EADC0 PADC0
ADC0 End of Conversion 0x007B 15 Y
(ADC0CN.5) (EIE2.1) (EIP2.1)
ET4 PT4
Timer 4 Overflow 0x0083 16 TF4 (T4CON.7)
(EIE2.2) (EIP2.2)
AD1INT EADC1 PADC1
ADC1 End of Conversion 0x008B 17
(ADC1CN.5) (EIE2.3) (EIP2.3)
EX6 PX6
External Interrupt 6 0x0093 18 IE6 (P3IF.5)
(EIE2.4) (EIP2.4)
EX7 PX7
External Interrupt 7 0x009B 19 IE7 (P3IF.6)
(EIE2.5) (EIP2.5)
RI1 (SCON1.0)
UART1 0x00A3 20 ES1 PS1
TI1 (SCON1.1)
XTLVLD EXVLD PXVLD
External Crystal OSC Ready 0x00AB 21
(OSCXCN.7) (EIE2.7) (EIP2.7)
R/W R/W R/W R/W R/W R/W R/W R/W Reset Value
EA IEGF0 ET2 ES0 ET1 EX1 ET0 EX0 00000000
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 SFR Address:
(bit addressable) 0xA8