Page 205 - Programming Microcontrollers in C
P. 205

190    Chapter 4  Small 8-Bit Systems

                          creates a structure that contains two bytes. This structure is com­
                          bined with a type long in the union below. Remember that a union
                          provides space to hold its largest member and the different members
                          of the union will occupy the same memory space. Therefore, the
                          memory space to store the long l is the exact same memory to
                          store the structure b. b.hi will occupy the most significant byte of
                          l, and b.lo will occupy the least significant byte of l. It is now easy
                          to deal with the bytes of the 16-bit quantity when moving the data
                          around, and equally easy to invoke 16-bit arithmetic operations on
                          the long combination of the two bytes.

                   union both
                   {
                       long l;
                       struct bothbytes b;
                   };
                          The statement

                   union both time_count;
                          declares that time_count is a union of the type both. In the in­
                          terrupt service routine, the members of time_count are handled
                          with little difficulty, as shown below.
                              The type registera defined as ac above is unique to the
                          M68HC05 compiler. This type specifies that the variable ac will be
                          stored in the accumulator. There is also a registerx type for the
                          index register.
                              In the main program, the output compare interrupt is enabled
                          and the system interrupt is enabled with the CLI() instruction in
                          the initialization section. The program then enters an endless loop in
                          which the clock is serviced every time sec becomes 60. The param­
                          eter sec will be changed by the interrupt service routine every second
                          so that the system should be a satisfactory clock. This loop is the
                          initialization section.
                              The last instruction in the FOREVER loop is a WAIT() instruc­
                          tion. This instruction places the processor into the wait mode. In this
                          mode, processor operations are halted and the microcontroller op­
                          eration is configured to reduce energy consumption. The operation
                          of the internal timers proceed as usual. The part is removed from the
                          wait mode by either a reset or the occurrence of an interrupt. The
   200   201   202   203   204   205   206   207   208   209   210