Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7565336
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2012-11-01 17:33:45

/**

 * 计时器

 * 最多同时支持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;

}

 

 

 

 

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

M_O_Bz2012-11-02 13:31:39

没看懂。。。能在写得详细点吗?,能写个example 吗?