Page 442 - Programming Microcontrollers in C
P. 442

A Clock Program     427

                    /*	    in this isr, all that must be done is to turn off the
                          pit interrupt request bit and increment the global
                          parameter count */

                   void pit_isr(void)
                   {
                       ITCSR.ITIF=ON; /* ON clears the interrupt bit in these
                                         registers */
                       count++;
                   }
                          The interrupt handler takes care of restoring the status prior to
                          returning to the interrupted program.
                              The next logical function that we will need is the function
                          main(). Main is broken into two parts, the initialization portion of
                          the program and the applications portion of the program. The first
                          three instructions are the initialization portion of the program. Here,
                          the serial I/O system is initialized and the baud rate is set to the value
                          BAUD_RATE. The pit_init() routine written above is executed
                          and then the system fast interrupts are enabled with the assembly
                          language macro function defined in the header file mmc2001.h.
                   #define BAUD_RATE        38400
                   #define FAST_AUTOVECTOR 0x3000002c

                   main()
                   {
                       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();
                       }
                   }
                              The application portion of the program is quite simple. It is simply
                          a FOREVER loop that repeatedly executes keep_time(). Also in
                          the loop, a test is made to determine if a key has been touched on the
                          terminal attached to the serial port. If it has, the function
                          reset_time() is executed where the clock time can be set.
   437   438   439   440   441   442   443   444   445   446   447