Page 206 - Programming Microcontrollers in C
P. 206
Timers 191
interrupt can be either an internal or an external interrupt. Since the
output compare timer is set up and its interrupt is enabled, when the
internal counter matches the contents of the output compare register,
an interrupt will occur and remove the processor from the wait mode.
Sometimes, it is desirable to get a measure of the fraction of the
time that the microcontroller is used to execute its program. The use
of the wait mode provides an excellent mechanism for measurement
of this usage. If there is an extra output port pin available, this pin
can be set just prior to entering the wait mode. The pin can then be
reset as the first instruction in the interrupt service routine. To make
this measurement more accurate, you can set another output pin to
the on condition all of the time. Measure these two outputs with an
averaging DC voltmeter. One hundred times the ratio of the cycling
output to the fixed output is the percentage of time that the
microcontroller is available to execute other code.
Examine the following code sequence:
time_count.b.hi = OCHI1;
ac = TSR; /* Arm OCF1 bit clear*/
time_count.b.lo = OCLO1; /* Clear OCF1 bit */
time_count.l += 500; /* 500 counts per millisecond */
OCHI1 = time_count.b.hi;
OCLO1 = time_count.b.lo;
The first instruction copies the high byte of the output compare
register into the high byte of the structure bin the union time_count.
When the TSR is copied into the a register, the system is set up to clear
the OCF1 bit. Then, the low byte of the output compare register is
moved into the low byte of the structure b. These operations leave the
16-bit contents of the output compare register in the union
time_count.l. Note that the high byte is moved from OCHI1, the
TSR is accessed, and then the low byte is moved from OCLO1. This
sequence, accessing OCLO1 after the TSR has been accessed will clear
the output compare flag 1, OCF1, and remove the interrupt source.
500 is added to this 16-bit number, and the result is copied back into
the output compare register 1 a byte at a time.
While it is usually best to keep the interrupt service routines short,
sometimes other operations can be completed within these routines
that can be useful. Recall that in the EEPROM programming routine