Page 446 - Programming Microcontrollers in C
P. 446

A Clock Program     431

                   {
                       inituart(BAUD_RATE); /* initialize the UART */
                   pit_init();                  /* initialize the PIT */
                   vector(handler,FAST_AUTOVECTOR);
                       /* put handler address in FAST_AUTOVECTOR. */
                       Enable_Fast_Interrupts();
                       FOREVER
                       {
                          keep_time();  /* keep track of the time */
                          if(kbhit())
                              reset_time();
                       }
                   }

                   void reset_time(void)
                   {
                       int c;

                       c=getch();
                       switch(c)
                       {
                          case ‘m’:
                          case ‘M’:      if(++minutes>MAX_MINUTES)
                                            minutes=0;
                                         break;
                          case ‘h’:
                          case ‘H’:      if(++hours>MAX_HOURS)
                                            hours=MIN_HOURS;
                                         break;
                       }
                       output_time(); /* send out a new time after each change */
                   }
                              Listing 8-10: Complete Clock Routine
                              Note that in this program the only functions that have direct access
                          to the memory of the computer are those that involve the PIT. The
                          pit_init() routine is truly chip specific as is the pit_isr().
                          All of the other routines could be used on just about any computer. It
                          is always good to place the chip-specific routines in functions by
                          themselves. Then when you need to change to another chip, all of the
                          chip-specific code is lumped together and the general-purpose code
                          is easy to lift and move to the new program.
                              All #define object-like and function-like macros are placed at
                          the head of the program. Also, a list of function prototypes is included.
   441   442   443   444   445   446   447   448   449   450   451