Page 411 - Programming Microcontrollers in C
P. 411

396    Chapter 8  MCORE, A RISC Machine

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

                   /***************************************************
                       Delay by t milliseconds. The data here are passed
                       as an int because, this routing ties up the
                       computer the whole delay time. An alternate means
                       should be used if a long delay is needed. This
                       routine uses the PIT which counts at about 122 us
                       per ticks. It is not very accurate because, the
                       count is at 1/8192 seconds, more nearly 122.070312
                       us ticks. Not good enough to make a clock, but
                       good enough to control a few milliseconds.

                       This modification assumes that the pit is enabled
                       and shall remain enabled.  TVS 6/2000
                   ****************************************************/
                   void delay(WORD t)
                   {
                       UWORD now,next;
                       UWORD count;


                       count=(long)t*1000; /* make the delay in us */
                       count/=122;       /* 122 us per tick */
                       ITCSR.EN=ON;      /* enable the pit */
                       while(count>0) /* there are probably faster ways to do */
                       {                 /* this, but who cares, you are killing time */
                          now = ITADR; /* and don’t care for speed */
                          do
                          {
                                 next = ITADR;
                          } while(now==next); /* wait until next tick */
                          count—–;
                       }
                       ITCSR.EN=OFF;  /* delay is done don’t need the pit now */
                   }
                              Listing 8-1: Delay routine
                              The function parameter time is the delay time in milliseconds.
                          The clocking rate for the counter is each 122.0… microseconds.
                          Therefore the time is converted to counts when it is divided by 122.
                          The PIT is then enabled with the instruction

                       ITCSR.EN = ON;          /* enable the pit */
   406   407   408   409   410   411   412   413   414   415   416