home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 08 / vansick / listing2.asc next >
Text File  |  1990-12-19  |  2KB  |  67 lines

  1. #pragma option v
  2. #include "hc05j1.h"
  3. #include "general.h"
  4.  
  5. /* define the global variables  */
  6.  
  7. int hrs,mts,sec;    
  8. int count;
  9. int corr1,corr2,corr3;       /* used to correct the time errors  */
  10.  
  11. main(void)
  12. {
  13.     corr1=corr2=corr3=0;     /* time corrections */
  14.     TCST.RT0=0;   /* 57.3 ms cop timer */
  15.     TCST.RT1=0;   /* 8.192 ms RTI */
  16.     TCST.RTIE=1;  /* Turn on the RTI */
  17.     TCST.RTIF=0;  /* Reset interruput */
  18.     TCST.TOF=0;   /* flags */
  19.     CLI();                   /* turn on interrupt */
  20.  
  21.     FOREVER
  22.     {
  23.         if(sec==60)  /* do clock things each minute  */
  24.         {
  25.             sec=0;
  26.             if(++mts==60)
  27.             {
  28.                 mts=0;
  29.                 if(++hrs==13)
  30.                     hrs=1;
  31.             }
  32.         }
  33.         WAIT();   /* wait here to save the energy  */
  34.     }
  35. }
  36.  
  37. void __TIMER(void)   /* routine executed every RTI (8.192 ms) */
  38. {
  39.     TCST.TOF=0;  /* reset the interrupt */
  40.     TCST.RTIF=0; /* flags  */
  41.  
  42.     if (++count==122)
  43.     {
  44.        sec++;               /* increment seconds */
  45.        if(++corr1==14)      /* To correct for 8.192 ms per tick */
  46.        {    
  47.               corr1=0;         /* run 122 ticks per second for 13  */
  48.            if(++corr2==80)  /* seconds, and 123 for the 14th second */
  49.            {                /* With this algorithm there are 14.000128 */
  50.                    corr2=0;    /* actual seconds per 14 indicated.  Then run */
  51.                    if(++corr3==4)
  52.                    {       
  53.                       count=1;
  54.                       corr3==0;
  55.                    } 
  56.                    else
  57.                        count=0;/* 79 of these cycles followed by one cycle of */        
  58.                }               /* 14 seconds with 122 ticks per second.  The */
  59.                else            /* elapsed time for this cycle is 1120.002048 */
  60.                   count=(-1);  /* seconds for and indicated time of 1120 */
  61.        }                    /* seconds. Repeat this cycle 4 times and on*/
  62.        else                 /* the last cycle drop one tick makes the */
  63.            count=0;         /* indicate and elapsed time exactly 4480 seconds.*/
  64.    }
  65. }
  66.  
  67.