home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_04 / 3n04043a < prev    next >
Text File  |  1992-04-01  |  5KB  |  174 lines

  1. /*
  2.       High Resolution Time Stamping Routines
  3.                     tstamp.c
  4.        Author: Charles B. Allison,
  5.                 Allison Technical Services
  6.                 Houston, Texas USA
  7.        Change History:
  8.                       Created 07:30 4-11-91
  9.        Purpose: To provide high resolution time of
  10.                 day time stamp acquisition and
  11.                 printing.
  12.        Hardware: PC and compatibles
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <conio.h>
  17. #include <dos.h>
  18. #include "timer.h"
  19.  
  20. /* generate inline code for interrupt and i/o */
  21. #pragma intrinsic (inp,outp,enable,disable)
  22.  
  23. /* # of seconds in a day at 12:00 noon
  24.   (i.e., 3600 * 12)                   */
  25. #define NOON_SECS 43200L
  26.  
  27. /*  Number of ticks til noon when including timer byte
  28.     as least significant byte of tick count.
  29.     (i.e., 256 * 12 * 3600 * 18.2)                  */
  30. #define NEW_TICKS 201277440L
  31.  
  32. /*  FRAC_SEC = 10 * the number of new ticks in a second
  33.     since one second has 18.2 ticks (i.e., 256 * 182)*/
  34. #define FRAC_SEC 46592L
  35.  
  36. long far *systime = (long far *) 0x0046b;  
  37.              /*actually 46c but want times 256*/
  38.  
  39. long get_time(void);     /* get 32 bit time value */
  40. int  get_tmr(void);      /* get timer contents    */
  41. void init_timer(void);   /* set 8253 timer mode=2 */
  42. long get_sec_time(long val);
  43. int get_msec_time(long val);
  44. long atime_diff(unsigned long time1,
  45.                 unsigned long time2);
  46. void print_time(unsigned long intim);
  47.  
  48. /* ********* time access ************ */
  49. /* ------ get 32 bit time stamp -------- */
  50. /*
  51. NOTES - there are 86,400 seconds per day
  52. 0x40:6c (long) has number of ticks (18.2x per sec)
  53.               since midnight
  54. then get number of ticks from 40:6c * 10/182 
  55.           for # of seconds today
  56. then add together for number of seconds
  57. each timer tick is 54.945054 ms
  58. take remainder of the /182 division for
  59.            the fraction of seconds
  60.  (fraction*10000)/182 = milliseconds*10
  61.                         (use long and multiply first)
  62. take timer msb to .2 ms - each tick is .2146ms 
  63. of ms byte of timer
  64. (long) (timer byte * 2146)/1000 = 10*milliseconds
  65. number should be good to  .1 ms - divide
  66.                by 10 to get msecs for struct timeb
  67. timeb is loaded by ftime()
  68. note timeb is struct with:
  69. time_t (long) time seconds since 1-1-1970 (GMT)
  70. unsigned short milliseconds
  71. short timezone (diff. in minutes between gmt
  72.                  and local)
  73. short dstflag  ( =0 no daylight savings time)
  74.                 needs <sys\timeb.h>
  75. alternately just use 40:6c 4 bytes + timer 1 byte
  76. */
  77.  
  78. /* ---------- get_time ------------- */
  79. long get_time(void)
  80. {
  81.  long temptime;
  82.  
  83. _disable();
  84. temptime = (*systime) & 0xFFFFFF00L; /** 256;*/
  85. temptime += get_tmr();
  86. _enable();
  87. return temptime;
  88. }     /* end of get 32 bit time value */
  89.  
  90. /* ------- get 8 bit timer contents -------- */
  91. int get_tmr(void)
  92. {
  93. int tmp1;
  94. /* WARNING should be called with interrupts disabled
  95.      or risk glitching the who is who sequence 
  96.      of bytes for upper lower byte              */
  97.   outp(TIMERC,0);  /* latch data */
  98.   inp(TIMER0);     /* throw away lsb */
  99.   tmp1 = 255 - (0xff & inp(TIMER0));
  100.   /* request pending intrpt status */
  101.   outp(PIC00,OCW3IR);
  102.   tmp1 |= ( inp(PIC00) & 0x01) ? 0x100 : 0x00; 
  103.       /* get the pending status for timer int irq01*/
  104.       /* set - timer int not done yet */
  105.  
  106.    /* use PIC for rollover not captured yet */
  107. return tmp1;    
  108. }      /* end of get timer contents */
  109.  
  110. /* ----------- init_timer ------------- */
  111. void init_timer(void)
  112. {
  113. _disable();
  114. outp(TIMERC,TMODE2);
  115. outp(TIMER0,0);         /* load 65536 */
  116. outp(TIMER0,0);
  117. _enable();
  118. }
  119.  
  120. /* ----------- get_sec_time -------------- */
  121. long get_sec_time(long val)
  122. {
  123. long subflg = 0;
  124. long tmp = NEW_TICKS;
  125.  
  126. if(val > tmp)
  127.  {
  128.  val -= tmp;/* sub out 12 hrs if need to - overflow */
  129.  subflg = NOON_SECS; /* subtract 12 hrs of seconds */
  130.  }
  131. /*  return integer result of number of seconds */
  132.  return (subflg + ((val*10L)/(FRAC_SEC)));
  133. }
  134.  
  135. /* ----------- get_msec_time ------------ */
  136. int get_msec_time(long val)
  137. {
  138. long tmp = NEW_TICKS;
  139.  
  140. if(val > tmp) val -= tmp;  /* sub out 12 hrs  */
  141.  
  142. /*  return integer 0-9,999 for tenths of a fractional
  143.   millisecond.  i.e., get the fraction of a second
  144.   remainder of val and scale it. */
  145. return (int)((10000L*((10L*val)%FRAC_SEC))/FRAC_SEC);
  146. }
  147.  
  148.  
  149. /* ----------- atime_diff -------------- */
  150. long atime_diff(unsigned long time1,
  151.                         unsigned long time2)
  152. {
  153. if (time1 > time2)
  154.   return (time1-time2);
  155. else
  156.   return (time2-time1);
  157. }
  158.  
  159. /* ---------- print_time -------------- */
  160. void print_time(unsigned long intim)
  161. {
  162. long tmp;
  163. int sec,min,hr;
  164. int tenms;
  165.  
  166. tenms = get_msec_time(intim);
  167. tmp = get_sec_time(intim);
  168. sec = (int)(tmp % 60);
  169. tmp = (int)(tmp / 60);
  170. min = (int)(tmp % 60);
  171. hr  = (int)(tmp / 60);
  172. printf("%2d:%2.2d:%2.2d.%4.4d",hr, min,sec,tenms);
  173. }
  174.