Texas Instruments MSC1210 User Manual

Page 78

Advertising
background image

Using Timers to Measure Time

8-4

Unlike instructions—some of which require one instruction cycle, others 2, and
others 4—the timers are consistent. They will always be incremented once ev-
ery 12 (or four) clocks. Therefore, if a timer has counted from 0 to 55 000 you
may calculate:

55 000 / 2 750 000 = 0.020 seconds (f

osc

/12) or

55 000 / 8 250 000 = 0.007 seconds (f

osc

/4)

The trade off in using f

osc

/12 or f

osc

/4 as the clock source is (1) code compatibility

and (2) resolution. With a 33MHz external clock, the resolution of f

osc

/12 = 364ns

per increment, and the resolution of f

osc

/4 is 121ns per increment.

Thus, we now have a system that measures time. All we need to review is how
to control the timers and initialize them to provide us with the information needed.

8.3.2

Timer SFRs

As mentioned before, the MSC1210 has three standard timers. Two of these
timers work in essentially the same way. One timer is Timer 0 and the other
is Timer 1. The two timers share two SFRs (TMOD and TCON) which control
the timers, and each timer also has two SFRs dedicated solely to maintaining
the value of the timer itself (TH0/TL0 and TH1/TL1). The third timer (Timer 2)
functions somewhat differently and will be explained separately.

The SFRs used to control and manipulate the first two timers are presented
in the Table 8−1.

Table 8−1. Timer Conrol SFRs.

SFR Name

Description

SFR Address

Bit Addressable?

TH0

Timer 0 high byte

8C

H

No

TL0

Timer 0 low byte

8A

H

No

TH1

Timer 1 high byte

8D

H

No

TL1

Timer 1 low byte

8B

H

No

TCON

Timer control

88

H

Yes

TMOD

Timer mode

89

H

No

Timer 0 has two SFRs dedicated exclusively to itself: TH0 and TL0. TL0 is the
low byte of the value of the timer, while TH0 is the high byte of the value of the
timer. That is to say, when Timer 0 has a value of 0, both TH0 and TL0 will con-
tain 0. When Timer 0 has the value 1000, TH0 will hold the high byte of the val-
ue (3 decimal) and TL0 will contain the low byte of the value (232 decimal).
Reviewing low/high byte notation, recall that you must multiply the high byte
by 256 and add the low byte to calculate the final value. In this case:

(TH0

S

256) + TL0 = 1000

(3

S

256) + 232 = 1000

Timer 1 works the exact same way, but its SFRs are TH1 and TL1.

Advertising