⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.c

📁 计算时间间隔的程序
💻 C
📖 第 1 页 / 共 3 页
字号:
\***************************************************************************/

tTIME TM_ElapsedTime ( tid )
    unsigned int tid;
{
    char custom_message[256];          /* Buffer for custom error message  */
    char *pFunction_name =             /* This function's name, used for   */
	     "TM_ElapsedTime()";       /*     error reporting              */

    /*---------------------------------------------------------------------*\
    **  Handle uninitialized module error.                                 **
    \*---------------------------------------------------------------------*/

    if ( gTM_module_not_initialized )
    {
	_TM_Error( pFunction_name, TM_ERR_UNINITIALIZED, NULL );
	return( (tTIME) (-1) );
    } /* end if */
	
    if ( tid < gTM_max_timers ) /* then tid is valid */
    {
	/*-----------------------------------------------------------------*\
	**  The timer ID is valid, so compute the elapsed time.            **
	\*-----------------------------------------------------------------*/

	return( TM_ReadTime ( ) - gpTM_start_time[tid] );
    }
    else /* tid is out of range */
    {
	/*-----------------------------------------------------------------*\
	**  Handle the bad timer ID error.                                 **
	\*-----------------------------------------------------------------*/

	sprintf ( custom_message, "Request received for timer %u, but the "
				  "last valid timer is timer %u.\n",
		  tid, gTM_max_timers-1 );
	_TM_Error ( pFunction_name, TM_ERR_BAD_TIMER_ID, custom_message );
	return( (tTIME) (-1) );
    } /* end if */
} /* end TM_ElapsedTime() */

/***************************************************************************\
**  int TM_Init( )                                                         **
*****************************************************************************
**  ARGUMENTS                                                              **
**      unsigned int num_timers                                            **
**          (I) number of timers the application is requesting             **
**-------------------------------------------------------------------------**
**  RETURNS                                                                **
**      int - non-zero means an error occurred                             **
**            0 if the timer module was successfully initialized           **
**-------------------------------------------------------------------------**
**  EXAMPLE USAGE                                                          **
**      InitializeApplication();                                           **
**      TM_Init(1);                     <-----<<                           **
**        . . .                                                            **
**      ApplicationBody();                                                 **
**        . . .                                                            **
**      TM_Close();                                                        **
**      ShutDownApplication();                                             **
**-------------------------------------------------------------------------**
**  DETECTABLE ERROR CONDITIONS                                            **
**      * Invalid number of timers requested (0)                           **
**      * Failed to allocated memory for array of pseudo-timers            **
**-------------------------------------------------------------------------**
**  DESCRIPTION                                                            **
**      This procedure sets timer 0 to operation mode 2, and allocates     **
**      memory to store starting times for the specified number of         **
**      (pseudo)timers.                                                    **
**                                                                         **
**      The gTM_module_not_initialized is set to 0 to express that the     **
**      module has been initialized, so the code can be used correctly.    **
**-------------------------------------------------------------------------**
**  LIMITATIONS                                                            **
**      None                                                               **
\***************************************************************************/

int TM_Init ( num_timers )
    unsigned int num_timers;
{
    char custom_message[256];          /* Buffer for custom error message  */
    char *pFunction_name =             /* This function's name, used for   */
	     "TM_Init()";              /*     error reporting              */

    /*---------------------------------------------------------------------*\
    **  Make sure this code doesn't get executed more than once before     **
    **  TM_Close().  We would allocate a new array of timers, and lose     **
    **  the previously allocated array (memory leak).                      **
    \*---------------------------------------------------------------------*/

    if ( !gTM_module_not_initialized )
    {
	return ( 0 );                /* return success                     */
				     /* (this isn't an error, just stupid) */
    }

    /*---------------------------------------------------------------------*\
    **  Allocate an array of qseudo-timers                                 **
    \*---------------------------------------------------------------------*/

    if ( num_timers > 0 )
    {
	gpTM_start_time = (tTIME *) calloc ( num_timers, sizeof ( tTIME ) );

	if ( gpTM_start_time == NULL )
	{
	    sprintf ( custom_message,
		      "Failed to allocate %u timers of size %u.\n",
		      num_timers, (unsigned int) sizeof ( tTIME ) );
	    _TM_Error ( pFunction_name, TM_ERR_ALLOC, custom_message );
	    return ( -1 );
	} /* end if */

	gTM_max_timers = num_timers;
    }
    else /* num_timers == 0, since num_timers is an unsigned int */
    {
	/*-----------------------------------------------------------------*\
	**  Application requested 0 timers, which is pointless.            **
	\*-----------------------------------------------------------------*/

	_TM_Error ( pFunction_name, TM_ERR_ZERO_TIMERS, NULL );
	return ( -1 );
    } /* end if-else */

    /*---------------------------------------------------------------------*\
    **  Set timer to operation mode 2                                      **
    **                                                                     **
    **  By writing 0x34 to port 0x43, we are specifying:                   **
    **     (0x34 = 00110100)                                               **
    **     bits 76  = 00  --> timer 0                                      **
    **     bits 54  = 11  --> writing 16-bit value: lsb followed by msb    **
    **     bits 321 = 010 --> operation mode 2                             **
    **     bit  0   = 0   --> binary counter operation                     **
    \*---------------------------------------------------------------------*/

    outportb ( 0x43, 0x34 );

    /*---------------------------------------------------------------------*\
    **  The following two statements writing 0x00 to port 0x40 are writing **
    **  a 16-bit value of 0x0000 to the timer's counter, which specifies   **
    **  that the counter is to begin its cycle with a value of 65536       **
    **  (0x10000), which specifies a maximum length cycle (54.926ms).      **
    \*---------------------------------------------------------------------*/

    outportb ( 0x40, 0x00 );
    outportb ( 0x40, 0x00 );

    /*---------------------------------------------------------------------*\
    **  Unlock this module                                                 **
    \*---------------------------------------------------------------------*/

    gTM_module_not_initialized = 0;

    return ( 0 );                      /* initialization successful */

} /* TM_Init() */

/***************************************************************************\
**  void TM_Close( )                                                       **
*****************************************************************************
**  ARGUMENTS                                                              **
**      void                                                               **
**-------------------------------------------------------------------------**
**  RETURNS                                                                **
**      void                                                               **
**-------------------------------------------------------------------------**
**  EXAMPLE USAGE                                                          **
**      InitializeApplication();                                           **
**      TM_Init(1);                                                        **
**        . . .                                                            **
**      ApplicationBody();                                                 **
**       . . .                                                             **
**      TM_Close();                     <-----<<                           **
**      ShutDownApplication();                                             **
**-------------------------------------------------------------------------**
**  DETECTABLE ERROR CONDITIONS                                            **
**      *  Module has not been initialized (TM_Init() not called)          **
**-------------------------------------------------------------------------**
**  DESCRIPTION                                                            **
**     This procedure returns system timer 0 to operation mode 3, and      **
**     deallocates the memory used to store the starting times for each    **
**     of the (pseudo)timers.                                              **
**                                                                         **
**     The gTM_module_not_initialized is set to 1 to prevent further use   **
**     of this module.  This module will remain locked until TM_Init()     **
**     is called.                                                          **
**-------------------------------------------------------------------------**
**  LIMITATIONS                                                            **
**      None                                                               **
\***************************************************************************/

void TM_Close ( void )
{
    char *pFunction_name =             /* This function's name, used for   */
	     "TM_Close()";             /*     error reporting              */

    /*---------------------------------------------------------------------*\
    **  Handle uninitialized module error.                                 **
    \*---------------------------------------------------------------------*/

    if ( gTM_module_not_initialized )
    {
	_TM_Error ( pFunction_name, TM_ERR_UNINITIALIZED, NULL );
	return;
    } /* end if */

    /*---------------------------------------------------------------------*\
    **  Deallocate the array of pseudo-timers                              **
    \*---------------------------------------------------------------------*/

    if ( gpTM_start_time != NULL )
    {
	free ( gpTM_start_time );
	gpTM_start_time = NULL;
    } /* end if */

    /*---------------------------------------------------------------------*\
    **  Set timer to operation mode 3                                      **
    **                                                                     **
    **  By writing 0x36 to port 0x43, we are specifying:                   **
    **     (0x36 = 00110110)                                               **
    **     bits 76  = 00  --> timer 0                                      **
    **     bits 54  = 11  --> writing 16-bit value: lsb followed by msb    **
    **     bits 321 = 011 --> operation mode 3                             **
    **     bit  0   = 0   --> binary counter operation                     **
    \*---------------------------------------------------------------------*/

    outportb ( 0x43, 0x36 );

    /*---------------------------------------------------------------------*\
    **  The following two statements writing 0x00 to port 0x40 are writing **
    **  a 16-bit value of 0x0000 to the timer's counter, which specifies   **
    **  that the counter is to begin its cycle with a value of 65536       **
    **  (0x10000), which specifies a maximum length cycle (54.926ms).      **
    \*---------------------------------------------------------------------*/

    outportb ( 0x40, 0x00 );
    outportb ( 0x40, 0x00 );

    /*---------------------------------------------------------------------*\
    **  Lock this module                                                   **
    \*---------------------------------------------------------------------*/

    gTM_module_not_initialized = 1;

} /* end TM_Close ( ) */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -