home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP2 / TIMEINTC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-02  |  3.6 KB  |  125 lines

  1. /***********************************************************************
  2.  
  3. FILE
  4.     timeintc.c  -  timer demo program using time1.c
  5.  
  6. REMARKS
  7.     Prints the seconds for 10 seconds.  DOS's time keeping function is
  8.     temporarily suspended, but will be restored at the end of the test.
  9.  
  10.     This program must be linked with the timeinta.asm module.  The
  11.     low level timer interrupt service routine in timeinta.asm, tmrisr(),
  12.     will save the registers and call tick(), which is defined in this
  13.     module.  Here is how to generate an executable program:
  14.  
  15.         masm timeinta /ml;
  16.         cl /Gs timeintc.c timeinta
  17.  
  18.     This module must be compiled using the /Gs flag because it contains
  19.     a routine that will be called inside an interrupt service routine.
  20.  
  21.     This program uses the PC's system clock frequency of approx. 18.2
  22.     timer interrupts per second.  If you want to use a different
  23.     tick frequency, refer to alarm.c for functions to change it.
  24.  
  25. LAST UPDATE
  26.     18 January 1988
  27.  
  28.     Copyright(c) 1987-1988  D.M. Auslander and C.H. Tham
  29.  
  30. ***********************************************************************/
  31.  
  32. /***********************************************************************
  33.                             I M P O R T S
  34. ***********************************************************************/
  35.  
  36. #include "envir.h"
  37.  
  38. #ifdef ANSI
  39.  
  40. extern void     tmrisr(void);
  41. extern void     setivec(int, unsigned, long);
  42. extern long     getivec(int);
  43.  
  44. #else
  45.  
  46. extern void     tmrisr();
  47. extern void     setivec();
  48. extern long     getivec();
  49.  
  50. #endif
  51.  
  52. /***********************************************************************
  53.                        P R I V A T E    D A T A
  54. ***********************************************************************/
  55.  
  56. #define VECTOR  8               /* timer interrupt vector number */
  57.  
  58. static int tock = 0;            /* count of interrupts */
  59. static int flag = 0;            /* flag each 1 second period */
  60.  
  61.  
  62. /***********************************************************************
  63.                            R O U T I N E S
  64. ***********************************************************************/
  65.  
  66. /*----------------------------------------------------------------------
  67. PROCEDURE
  68.     TICK  -  high level timer interrupt service routine
  69.  
  70. SYNOPSIS
  71.     void tick(void)  -  called by low level interrupt handler tmrisr()
  72.  
  73. REMARKS
  74.     Count 18 timer interrupts (equivalent to 1 second) and set flag
  75.  
  76. LAST UPDATE
  77.     26 January 1988
  78. ----------------------------------------------------------------------*/
  79.  
  80. void tick()
  81. {
  82.     if (++tock >= 18)
  83.     {
  84.         tock = 0;
  85.         flag = 1;
  86.     }
  87. }
  88.  
  89.  
  90.  
  91. /*----------------------------------------------------------------------
  92. PROCEDURE
  93.     MAIN  -  the main program
  94.  
  95. LAST UPDATE
  96.     26 January 1988
  97.         take advantage of new setivec() and getivec()
  98. ----------------------------------------------------------------------*/
  99.  
  100. main()
  101. {
  102.     long orig_isr;      /* place to save original timer vector */
  103.     int seconds = 0;    /* elasped time in seconds, initialized to 0 */
  104.  
  105.  
  106.     orig_isr = getivec(VECTOR);     /* save original vector contents */
  107.  
  108.     setivec(VECTOR, sizeof(tmrisr), (long)tmrisr);  /* install tmrisr() */
  109.  
  110.     while (seconds < 10)            /* not 10 seconds yet */
  111.     {
  112.         while (!flag)               /* wait for 1 second indication */
  113.             ;
  114.  
  115.         flag = 0;                   /* reset flag */
  116.  
  117.         printf("%d\n", ++seconds);  /* print elasped time in seconds */
  118.     }
  119.  
  120.     setivec(VECTOR, sizeof(orig_isr), orig_isr);
  121.  
  122. }
  123.  
  124.  
  125.