Page 438 - Programming Microcontrollers in C
P. 438

A Clock Program     423

                          sequence is the outside test only. Such an arrangement will require a
                          minimum amount of computer time each time the function is executed.
                          Many programmers will not nest the above tests so that each test is
                          executed each time the function is called. It works, but it requires
                          more computer resources than the nesting shown above.
                              In the function keep_time() above, note that the arguments
                          of the several if() statements all involve a “greater than” test. This
                          particular approach is more robust than any test that involves an “is
                          equal to” test. In all cases but the hours, the tested parameter is reset
                          to zero. Therefore, the zero is counted in the sequence and the
                          maximum value is one less than the number that might be expected.
                          For example, the range of seconds is 0..59 not 1..60, the range of
                          count is 0.. 511 not 1..512. When the respective count exceeds the
                          specified maximum value, the parameter is reset to its minimum value.
                          If lightning should strike the chip and the value of seconds be set at
                          100, the “greater than” test would fix the error the next time that
                          second were tested. In the event that an “is equal to” test were used,
                          it would be a long time before the seconds would count through a
                          wrap-around and be equal to the MAX_SECONDS value again.
                              Also note the if argument

                       if(++seconds>MAX_SECONDS)
                       {

                          The seconds are first incremented and then the test is completed.
                          This sequence could be completed in two statements,
                       seconds=seconds+1;
                       if(seconds>MAX_SECONDS)
                       {

                          Some people prefer the latter approach, but the two approaches
                          accomplish exactly the same thing. The important item is that seconds
                          must be incremented before the test is completed rather than after.
                              At the bottom of the seconds loop, a call to output_time()
                          is executed. This function will send the current time to the output
                          device. Here again, the call to output_time() could be placed
                          anywhere in the loop, but the bottom, as is shown above, is best
                          because the output takes place after all of the parameters are updated
                          and the time displayed will be now rather than before all of the updates
                          are completed.
   433   434   435   436   437   438   439   440   441   442   443