Page 193 - Programming Microcontrollers in C
P. 193
178 Chapter 4 Small 8-Bit Systems
count=0; /* the last cycle drop */
} /* one tick makes the */
} /* indicate and elapsed */
/* time exactly 4480 sec.*/
The three static variables corr1, corr2, and corr3 are used to
keep track of the number of times the several different loops in the
algorithm are executed. C will always initialize these variables to zero
and then their value will be retained from call to call of the function.
16-bit Timers
The multifunction timer discussed in the previous section pro
vides for implementation of relatively simple timing functions. Let’s
assume that the microcontroller clock frequency is 4.0 MHz. The
fastest interrupt time with this system is 0.512 milliseconds, and the
granularity of the interrupt times is in large, power-of-two blocks for
the RTI system. Also, the relation of the interrupt times to unity is
not “clean”; complicated algorithms or special frequency crystals
are needed to get the device to respond accurately in seconds.
Often microcontroller applications must provide more than one
time function. The 15-bit timer is set up to provide only one time
base. Of course, a programmer can program the timer to control many
different functions and at many different times. The limits on the
functions and times are difficult to determine. It is clear that the fast
est practical time base in the 15-bit timer is 0.512 milliseconds. To
obtain any finer time resolution, the programmer would have to com
pare the TCR bits to a specified value on a cycle-by-cycle basis. This
type of program completely consumes the microcontroller and leaves
no processing time for other functions during the execution of the
timing program. If the time base must be other than some multiple of
0.512 milliseconds and not one of the standard RTI times, the pro
cessor can probably service only one time function. If the required
times can fall on the above values, the processor can execute several
time-based functions limited by the total time required to execute the
functions and the microcomputer interrupt latency time.
The 16-bit timer addresses these problems. A block diagram of this
type of timer is shown in Figure 4-2. This style of timer contains an
internal 16-bit counter that is clocked at some fraction of the