
Using Timers to Measure Time
It is apparent that the maximum value a timer may have is 65,535 because there are only two bytes devoted to the value of each timer. If a timer contains the value 65,535 and is subsequently incremented, it will
8.3.3TMOD SFR
The TMOD SFR is used to control the mode of operation of both timers. Each bit of the SFR gives the microcontroller specific information concerning how to run a timer. The high four bits (bits 4 through 7) relate to Timer 1, whereas the low four bits (bits 0 through 3) perform the exact same functions, but for Timer 0.
The individual bits of TMOD have the following functions:
| 7 | 6 |
| 5 | 4 | 3 | 2 |
| 1 | 0 |
| ||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| TIMER 1 |
|
|
| TIMER 0 |
| Reset Value | ||||
|
|
|
|
|
|
|
|
|
|
|
| ||
SFR 89H | GATE | C/T |
| M1 | M0 | GATE | C/T | M1 | M0 | 00H | |||
GATE (bit 7)— Timer 1 Gate Control. This bit enables/disables the ability of Timer 1 to increment.
0:Timer 1 will clock when TR1 = 1, regardless of the state of pin INT1.
1:Timer 1 will clock only when TR1 = 1 and pin INT1 = 1.
C/T (bit 6)— Timer 1 Counter/Timer Select.
0:Timer is incremented by internal clocks.
1:Timer is incremented by pulses on pin T1 when TR1 (TCON.6, SFR 88H) is 1.
M1, M0 (bits
M1 | M0 | Mode |
|
|
|
0 | 0 | Mode 0: |
|
|
|
0 | 1 | Mode 1: 16 bits. |
|
|
|
1 | 0 | Mode 2: |
|
|
|
1 | 1 | Mode 3: Timer 1 is halted, but holds its count. |
|
|
|
GATE (bit 3)— Timer 0 Gate Control. This bit enables/disables the ability of Timer 0 to increment.
0:Timer 0 will clock when TR0 = 1, regardless of the state of pin INT0 (software control).
1:Timer 0 will clock only when TR0 = 1 and pin INT0 = 1 (hardware control).
C/T (bit 2)— Timer 0 Counter/Timer Select.
0:Timer is incremented by internal clocks.
1:Timer is incremented by pulses on pin T0 when TR0 (TCON.4, SFR 88H) is 1.
M1, M0 (bits
M1 | M0 | Mode |
|
|
|
0 | 0 | Mode 0: |
|
|
|
0 | 1 | Mode 1: 16 bits. |
|
|
|
1 | 0 | Mode 2: |
|
|
|
1 | 1 | Mode 3: Timer 1 is halted, but holds its count. |
|
|
|
Timers8-5