Page 436 - Programming Microcontrollers in C
P. 436

A Clock Program     421

                          of a loop. There are four external variables, hours, seconds, minutes,
                          and count accessed by keep_time(). The variable count is
                          incremented in an interrupt service routine that is executed each
                          1.953125 milliseconds. The constant TIME_COUNT will have a value
                          of 511. When count becomes greater than TIME_COUNT, count
                          is reset to zero and the parameter seconds are incremented. When
                          seconds exceeds the value 59, seconds is reset to zero and
                          minutes is incremented. When minutes exceeds 59, it is reset to
                          zero and hours is incremented. Finally, when hours becomes
                          greater than 12, it is reset to 1 which corresponds to one o’clock.
                          Once each second, the function output_time() is executed.

                   #include “mmc2001.h”
                   #include “timer.h”

                   #define TIME_COUNT       511
                   #define MAX_SECONDS  59
                   #define MAX_MINUTES  MAX_SECONDS
                   #define MAX_HOURS        12
                   #define MIN_HOURS        1

                   /* function prototypes */
                   void output_time(void);
                   void keep_time(void);

                   /* external—global variables */
                   WORD seconds,minutes,hours,count;


                   /* the main applications program. count is incremented in
                       the isr 512 times each second. Therefore, this routine
                       must be executed at least once every two milliseconds. */


                   void keep_time(void)
                   {
                       if(count>TIME_COUNT)
                       {
                          count=0;
                          if(++seconds>MAX_SECONDS)
                          {
                              seconds=0;
                              if(++minutes>MAX_MINUTES)
                              {
                                 minutes=0;
                                 if(++hours>MAX_HOURS)
                                     hours=MIN_HOURS;
   431   432   433   434   435   436   437   438   439   440   441