Page 444 - Programming Microcontrollers in C
P. 444

A Clock Program     429

                          pulled together into a single listing. Several items have been moved
                          around in this listing to make the whole program easier to follow.

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

                   #define TIME_COUNT           511
                   #define MAX_SECONDS          59
                   #define MAX_MINUTES          MAX_SECONDS
                   #define MAX_HOURS            12
                   #define MIN_HOURS            1
                   #define FAST_AUTOVECTOR 0x3000002c
                   #define TWO_MS               15
                   #define BAUD_RATE            38400

                   #define hi(x) ((x)/10+’0')
                   #define lo(x) ((x)%10+’0')

                   /* function prototypes */
                   void output_time(void);
                   void keep_time(void);
                   void reset_time(void);
                   void pit_init(void);
                   void pit_isr(void);
                   void handler(void);

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

                   /* the main applications program. count is incremented in
                       the isr every two milliseconds. Therefore, this routine
                       must be executed at least once every two milliseconds to
                       keep the outputs to the serial port smooth. */
                       void keep_time(void)
                   {
                       if(count>TIME_COUNT)
                       {
                          count=0;
                          if(++seconds>MAX_SECONDS)
                          {
                              seconds=0;
                              if(++minutes>MAX_MINUTES)
                              {
   439   440   441   442   443   444   445   446   447   448   449