Chinaunix首页 | 论坛 | 博客
  • 博客访问: 84860
  • 博文数量: 99
  • 博客积分: 55
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 21:29
文章分类

全部博文(99)

文章存档

2013年(5)

2012年(94)

我的朋友

分类:

2012-11-04 20:36:46

原文地址:Linux_C计时器 作者:luozhiyong131

/**

 * 计时器

 * 最多同时支持8个计时器

 * Author:Lzy

 * Greate Date:2012.10.15

 */

#include

#include

#include

#include

#include

#include

 

#define TMAX      0xff

static unsigned char T=0;   // 每创建一个计时器,则在T中把相应的位置位

static struct timeval starttime[TMAX],endtime[TMAX];

 

/**

 *  函数功能:创建一个计时器

 *  phTickHandle:计时器句柄

 *  返回值: 0x00-->创建成功 : 0x8B-->参数错误 : 0x8F-->创建失败

 */

int ST_CreateTick(int *phTickHandle)

{

    int i = 0;

 

    while(T & (1<<i++))  ;      // 每创建一个计时器,则在T中把相应的位置位

    if(i > 8)

       return 0x8f;

    i--;

    T |= (1<<i);

    *phTickHandle = 1<<i;

    gettimeofday(&(starttime[1<<i]),0); // 获得时间

 

    return 0;

}

 

/**

 * 函数功能:重置计时器,计时重新开始

 * hTickHandle:计时器句柄

 * 返回值: 0x00-->成功 : 0x8B-->参数错误

 */

int ST_ResetTick(int hTickHandle)

{

    gettimeofday(&starttime[hTickHandle],0);

    return 0;

}

 

/**

 * 函数功能:获取指定计时器启动或重置以来过了多少毫秒

 * hTickHandle( 入口):计时器句柄

 * pulPassedTime( 出口):无符号长整型,指定计时器启动以来过了多少毫秒

 * 返回值: 0x00-->成功 : 0x8B-->参数错误

 */

int ST_GetPassTick(int hTickHandle, unsigned long *pulPassedTime)

{

    gettimeofday(&endtime[hTickHandle],0);

 

    long timeuse = 1000000*(endtime[hTickHandle].tv_sec - starttime[hTickHandle].tv_sec)

           + endtime[hTickHandle].tv_usec - starttime[hTickHandle].tv_usec;

    timeuse /=1000;   //除以1000则进行毫秒计时,如果除以1000000则进行秒级别计时,如果除以1则进行微妙级别计时

 

    *pulPassedTime = timeuse;

 

    return 0;

}

 

/**

 * 函数功能:关闭指定的计时器

 * phTickHandle:计时器句柄

 * 返回值: 0x00-->成功 : 0x8B-->参数错误

 */

int ST_CloseTick(int *phTickHandle)

{

    T &= ~(*phTickHandle);

 

    return 0;

}

 

 

 

 

阅读(114) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~