Page 201 - Programming Microcontrollers in C
P. 201

186    Chapter 4  Small 8-Bit Systems

                          ferred to the proper output pin, TCMP1 or TCMP2. If the OCIE bit is
                          set in the timer control register, an interrupt will accompany the out­
                          put compare.
                              There are times when it is desirable to force an output compare
                          from a program. The FOLV1 and FOLV2 bits can be used for this
                          purpose. These bits will always read 0, but writing a 1 to these bits in
                          the TCR will cause transfer of the corresponding OLVL1 or OLVL2 bit
                          to the specified output compare bit, either TCMP1 or TCMP2. This
                          output does not affect the compare flags, so no interrupt is generated.

            Programming the 16-bit Timer

                              We will examine several different uses of the 16-bit timer system
                          in this section. The first is merely a repeat of the simple timer pro­
                          grammed in the section on the 15-bit system. Here we merely want
                          to keep track of time, hours, minutes, and seconds in memory. No
                          provisions are made yet for reading the time values or to change the
                          values; these problems will be discussed later.
                              A listing of this program is shown below. In this case, the header
                          file for the M68HC05B6 is used. A listing of this file is found on the
                          CD-ROM. This program will make use of an output compare to gen­
                          erate periodic interrupts to the microcontroller. We will use output
                          compare register 1. It will be set up so that when the first output
                          compare interrupt occurs, the contents of the output compare regis­
                          ter will be incremented by 500. Since the clocking time of the counter
                          register is 2 microseconds, 500 2-microsecond periods will allow an
                          output compare every 1 millisecond. This occurrence will be treated
                          in the interrupt service routine.

                   #include “hc05b6.h”

                   int hrs, mts, sec; /* global variables */
                   long count=1000;


                   struct bothbytes /* 16 bit int structure */
                   {
                       int hi;
                       int lo;
                   };
   196   197   198   199   200   201   202   203   204   205   206