Page 188 - Programming Microcontrollers in C
P. 188

Timers    173

                              A few words about a good programming practice: numbers in a
                          program with no defined meaning are called “magic numbers.” You
                          should avoid magic numbers, because a number with no meaning
                          makes life difficult for the program maintenance people.  In the pro­
                          gram above, several numbers are needed.  These numbers are given a
                          name by either enum statements or #define statements.  Then, in
                          the program, you can see every instance of the use of the number
                          does have a meaning relative to the program. Another advantage to
                          avoiding magic numbers is not too evident in the above program, but
                          it is truly an important advantage.  If the program is long and com­
                          plicated, these numbers might be used many times.  Then if a
                          maintenance situation requires the change of the value of a number
                          in the program, it can be changed in one place and a recompilation
                          will correct every instance of the number in the program.
                              Several global variables are used in this program: hrs, mts,
                          sec, and count. These variables are all changed in the main pro­
                          gram, but they are available in any other part of the program if needed.
                          For example, the count variable is initialized to zero in the main
                          program and incremented and reset in the interrupt service routine.
                          One point should be noted in this program: the main program has all
                          of the time calculations based on the current contents of sec. The
                          variable sec is incremented each second in the interrupt service rou­
                          tine. Some programmers would put the complete time service within
                          the interrupt service routine. That is, they would reset sec when it
                          reaches 60, increment mts, and so forth within the interrupt service
                          routine. Either approach will provide the same result, and each takes
                          the same total computer time. It is, however, better to keep the time
                          that the program is controlled by the interrupt service routine at a
                          minimum. Interrupts are disabled when a program is in an interrupt
                          service routine. If there are several competing interrupts, execution
                          of an interrupt service routine prevents other interrupts from being
                          processed. Quickest response to all interrupts will be obtained if all
                          of the interrupt service routines are as short as possible.


            Program Organization
                              A compiled version of this program is listed below. Note that the
                          compiler listing routine prints out the contents of the include file.
                          The memory map #pragmas puts the RAM in page 0 beginning at
   183   184   185   186   187   188   189   190   191   192   193