Using Timers to Measure Time

As shown, the value of TH0 never changed. In fact, when mode 2 is used, THx is almost always set to a known value and TLx is the SFR that is constantly incremented. THx is initialized once, and then left unchanged.

The benefit of auto-reload mode is that, perhaps, the timer may need to always have a value from 200 to 255. When using mode 0 or 1, the code would have to be checked to see if the timer had overflowed and, if so, the timer reset to 200. This takes precious amounts of execution time to check the value and/or reload it.

When mode 2 is used, the microcontroller takes care of this. Once a timer has been configured in mode 2, it does not have to be checked to see if the timer has overflowed, nor does the value need to be reset—the microcontroller hardware will do it all.

The auto-reload mode is very commonly used for establishing a baud rate, which will be discussed further in Chapter 9, Serial Communications.

8.3.3.4Split-Timer Mode (mode 3)

Timer mode 3 is a split-timer mode. When Timer 0 is placed in mode 3, it essen- tially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Tim- er 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0, and all the bits related to Timer 0 will be tied to TL0.

While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1, or 2 normally. However, the real Timer 1 may not be started or stopped, because the bits that do that are now linked to TH0. The real Timer 1, in this case, will be incremented every machine cycle no matter what. The real Timer 1 may be stopped by setting it to mode 3.

The only real use of note in using split-timer mode is if two separate timers are needed along with a baud rate generator. In such a case, use the real Timer 1 as a baud rate generator, and use TH0/TL0 as two separate timers.

8.3.4TCON SFR

Finally, there is one more SFR that controls the two timers and provides valu- able information about them. The TCON SFR has the structure described in Table 8−4.

Table 8−4. TCON (88 H ) SFR

Bit

Name

Bit Address

Explanation of Function

Timer

 

 

 

 

 

 

 

 

Timer 1 overflow. This bit is set by the microcontroller

 

7

TF1

8FH

when Timer 1 overflows.

1

 

 

 

Timer 1 run. When this bit is set, Timer 1 is turned on.

 

6

TR1

8EH

When this bit is clear, Timer 1 is off.

1

 

 

 

Timer 0 overflow. This bit is set by the microcontroller

 

5

TF0

8DH

when Timer 0 overflows.

0

 

 

 

Timer 0 run. When this bit is set, Timer 0 is turned on.

 

4

TR0

8CH

When this bit is clear, Timer 0 is off.

0

8-8

Page 82
Image 82
Texas Instruments MSC1210 manual Tcon SFR, 4. Tcon 88 H SFR, Bit Name Bit Address Explanation of Function Timer