Page 416 - Programming Microcontrollers in C
P. 416

Delays Revisited    401

            Delays Revisited

                              With semaphores available, we can improve on the delay function
                          by removing the wait loop from within the function to the calling
                          function, where its use can be dictated by the program. In this case,
                          the calling function will create a semaphore and send the semaphore
                          to the delay function. The proposed delay function is

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


                   /***********************************************************
                       Delay by t milliseconds. This delay controls a semaphore
                       that will be examined by the calling program. When the
                       semaphore is released, the delay is completed. It is
                       assumed that fast interrupts are enabled when this func­
                       tion is called and that the interrupt handler has been
                       appropriately set up.
                   *****************************************************************/

                   static int sem; /* this variable needs to be file global */

                   void delay(WORD t, int semaphore)
                   {
                       long count;

                       sem=semaphore;  /* save semaphore so isr can see it */
                       ITCSR.EN = ON;  /* enable the pit */
                       ITCSR.OVW=ON;      /* enable write through */
                       /* an interrupt will occur in t milliseconds */
                       count =(long)t*1000; /* make the delay in us */
                       ITDR =count/122;         /* 122 us per tick */
                       ITCSR.ITIE = ON;         /* enable pit interrupt */
                       FIER.EF8=ON;             /* enable the fast interrupts */
                   } /* return to the calling program, we are done here */

                   void pit_isr(void) /* interrupt occurs when delay time expires */
                   {
                       ITCSR.ITIF = ON;  /* turn interrupt flag off */
                       ITCSR.ITIE = OFF;  /* disable the PIT interrupt */
                       ITCSR.EN = OFF;      /*  disable the PIT */
                       FIER.EF8 = OFF;      /* disable the pit fast interrupt */
                       release_semaphore(sem); /* release semaphore to calling program */
                   }
                              Listing 8-3: Alternate Delay Routine
   411   412   413   414   415   416   417   418   419   420   421