home *** CD-ROM | disk | FTP | other *** search
/ nisttime.carsoncity.k12.mi.us / nisttime.carsoncity.k12.mi.us.tar / nisttime.carsoncity.k12.mi.us / pub / acts / interp.c < prev    next >
C/C++ Source or Header  |  1996-11-07  |  2KB  |  102 lines

  1. float interp()
  2. {
  3. /*
  4.   this subroutine estimates the fraction of a tick between
  5.   when it is called and the next time the clock changes.
  6.   it does this by incrementing a variable until the tick
  7.   changes and then repeating the operation until it changes
  8.   again.  the first number is a partial tick and the second
  9.   one is a full tick and the ratio is thus the fraction
  10.   from when the routine is called until the next tick.
  11.  
  12.   For the Sun system, the interval betweek ticks in microseconds
  13.   must be specified in the file nistime.h as parameter tickus.
  14.   If this parameter is set to 0, then the interpolator is 
  15.   effectively disabled.
  16. */
  17. #include "nistime.h"
  18. #include <stdio.h>
  19. #ifdef IBMPC
  20. #include <dos.h>
  21. #endif
  22. #ifdef SUN
  23. #include <sys/time.h>
  24. struct timeval tvv,*tp;
  25. LONG old,dly;
  26. #endif
  27. int j;
  28. int k,kk;
  29. long int mm,mmm;
  30. float xx;
  31. /*
  32.     get lower portion of time for IBMPC or current microseconds for sun
  33. */
  34. #ifdef IBMPC
  35.     _AH=0;
  36.     geninterrupt(0x1a);
  37.     k=_DX;
  38. #endif
  39. #ifdef SUN
  40.     tp= &tvv;
  41.     gettimeofday(tp,0);
  42.     old=tp->tv_usec;
  43.     if(tickus == 0) return (100.);  /*interpolation disabled */
  44. #endif
  45. #ifdef IBMPC
  46. /*
  47.   now wait until tick changes
  48. */
  49.     mmm=0;
  50.     do
  51.       {
  52.       mmm++;
  53.       _AH=0;
  54.       geninterrupt(0x1a);
  55.       kk=_DX;
  56.     } while (kk==k);
  57.     mm=0;
  58.     k=kk;
  59.     do
  60.       {
  61.       mm++;
  62.       _AH=0;
  63.        geninterrupt(0x1a);
  64.        kk=_DX;
  65.     } while (kk == k);
  66.     xx=mmm;
  67.     xx=100*xx/(float) mm;
  68.     if(xx < 0) return (0.);
  69.     if(xx > 100) return(100.);
  70.     return(xx);
  71. #endif
  72. #ifdef SUN
  73. /*
  74.     first increment counter until tick changes  by the 
  75.     time interval in microseconds specified in the
  76.     header file as parameter tickus. -- see nistime.h
  77. */
  78.     mmm=0;
  79.     do
  80.       {
  81.       mmm++;
  82.       gettimeofday(tp,0);
  83.       dly=tp->tv_usec - old;
  84.       if(dly < 0) dly += 1000000;  /*if rolled over to next second*/
  85.     } while(dly < tickus);
  86.     old=tp->tv_usec;
  87.     mm=0;
  88.     do
  89.       {
  90.       mm++;
  91.       gettimeofday(tp,0);
  92.       dly=tp->tv_usec - old;
  93.       if(dly < 0) dly += 1000000;
  94.     } while (dly < tickus);
  95.     xx=mmm;
  96.     xx=100*xx/(float)mm;
  97.     if(xx < 0) return(0.);
  98.     if(xx > 100) return(100.);
  99.     return(xx);
  100. #endif
  101. }
  102.