home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / ins_msc / imc9106 / hrtimer1.c < prev    next >
Text File  |  1991-05-01  |  4KB  |  87 lines

  1. /*********************************************************************
  2. * HRTIMER1.C - This program uses the function HRTimer1() as shown in *
  3. *   Figure D of the article "Designing a high-resolution timer" in   *
  4. *   the May 1991 issue of Inside Microsoft C. It demonstrates some   *
  5. *   of the problems associated with this function.                   *
  6. *********************************************************************/
  7.  
  8. #include <stdio.h>      /* puts()                                   */
  9. #include <conio.h>      /* outp(), inp(), kbhit()                   */
  10. #include <dos.h>        /* disable(), enable()                      */
  11.  
  12. #define COUNTS_PER_uSEC (double)1.19318
  13.  
  14. void HRMode2( void );
  15. void HRMode3( void );
  16. unsigned long HRTimer1( void );
  17.  
  18. /*********************************************************************
  19. * HRMode2 - Switch counter 0 to mode 2 and load initial value 65536  *
  20. *********************************************************************/
  21. void HRMode2( void )
  22.     {
  23.     _disable();                   /* Disable interrupts             */
  24.     outp(0x43, 0x34);             /* Counter 0, Mode 2, LSB/MSB     */
  25.     outp(0x40, 0x00);             /* Load low word of valu          */
  26.     outp(0x40, 0x00);             /* Load high word of value        */
  27.     _enable();                    /* Re-enable interrupts           */
  28.     }
  29.  
  30. /*********************************************************************
  31. * HRMode3 - Switch counter 0 to mode 3 and load initial value 65536  *
  32. *********************************************************************/
  33. void HRMode3( void )
  34.     {
  35.     _disable();                   /* Disable interrupts             */
  36.     outp(0x43, 0x36);             /* Counter 0, Mode 3, LSB/MSB     */
  37.     outp(0x40, 0x00);             /* Load low word of value         */
  38.     outp(0x40, 0x00);             /* Load high word of value        */
  39.     _enable();                    /* Re-enable interrupts           */
  40.     }
  41.  
  42. /*********************************************************************
  43. * HRTimer1 - as shown in Figure D.                                   *
  44. *********************************************************************/
  45. unsigned long HRTimer1( void )
  46.     {
  47.     unsigned int far * BiosTicker =
  48.         (unsigned int far *)0x0040006cL;
  49.     union {
  50.         unsigned long l;
  51.         struct { unsigned int l, h; } i;
  52.         struct { unsigned char l, h; } c;
  53.         } C0;
  54.  
  55.     _disable();                   /* Turn off interrupts            */
  56.     outp(0x43, 0x00);             /* Latch PIT counter 0            */
  57.     C0.c.l = inp(0x40);           /* Read low byte of counter 0     */
  58.     C0.c.h = inp(0x40);           /* Read high byte of counter 0    */
  59.     C0.i.h = *BiosTicker;         /* Get BIOS's master clock count  */
  60.     _enable();                    /* Turn interrupts back on        */
  61.     C0.i.l = 65535 - (--C0.i.l);  /* Invert counter 0 value         */
  62.  
  63.     return( C0.l );
  64.     }
  65.  
  66. /*********************************************************************
  67. * main - Test loop for HRTimer1().                                   *
  68. *********************************************************************/
  69. void main( void )
  70.     {
  71.     unsigned long time1, time2;
  72.  
  73.     HRMode2();
  74.  
  75.     puts( "Testing HRTimer1().  Press any key to quit ... " );
  76.  
  77.     while ( !kbhit() )
  78.         {
  79.         time1 = HRTimer1();
  80.         time2 = HRTimer1();
  81.         if ( time2 < time1 )
  82.             puts( "HRTimer1 error" );
  83.         }
  84.  
  85.     HRMode3();
  86.     }
  87.