您的位置:首页 > 其它

VxWorks_Timer

2016-05-19 19:01 281 查看
#include "VxWorks.h"

#include "time.h"

#include "timers.h"

#include "syslib.h"

#include "logLib.h"

#include "stdio.h"

 

#define COUNT 5

 

/*处理函数*/

void myHandler(timer_t tmId,int arg)

{
static int iCount=0;
int iRet;

iCount++;
printf("myHandler is called. the arg is      %d,count is %d\n",arg,iCount);

/* When this funciton is called COUNT times, cancle the timer and
delete it.
*/
if(iCount>=COUNT)
{
iRet=timer_cancel(tmId);
if(iRet!=0)
{
printf("time_cancel error.\n");
return;
}
printf("Timer cancled\n");
timer_delete(tmId);
if(iRet!=0)
{
printf("time_delete error.\n");
return;
}
}

}

 

/*初始化、挂载Timer*/

void main()

{
int iRet;
struct timespec     stTp0,stTp1;
struct itimerspec stValue;
timer_t tmId;

#if 0
iRet=clock_gettime(CLOCK_REALTIME, &stTp0);
printf("clock_gettime:%d, %d.\n", stTp0.tv_sec, stTp0.tv_nsec);

/* set current time to 0 second and o nsecond*/
stTp0.tv_sec=0;
stTp0.tv_nsec=0;
iRet=clock_settime(CLOCK_REALTIME, &stTp0);
if(iRet!=0)
{
printf("clock_settime error.\n");
return;
}

#endif

      /*timer_t*/

iRet = timer_create(CLOCK_REALTIME, NULL, &tmId);
if(iRet!=0)
{
printf("timer_create error.\n");
}
else
{
/* connect tmId with myHandler*/ 
iRet=timer_connect(tmId,myHandler,10);
if(iRet!=0)
{
printf("timer_connect error.\n");
}
else
{
/* set interrupt time*/
stValue.it_interval.tv_sec=0;
stValue.it_interval.tv_nsec=500000000;
stValue.it_value.tv_sec=0;

stValue.it_value.tv_nsec=500000000;

iRet=timer_settime(tmId,0,&stValue,0);
if(iRet!=0)
{
printf("timer_settime error.\n");
}
else
{
/* Test here*/
stTp0.tv_sec=10;
stTp0.tv_nsec=0;
while(1)
{
/**/
//taskDelay(20);
#if 0
nanosleep(&stTp0,&stTp1);
#endif
//printf("**************tv_sec %ld tv_nsec %ld\n",stTp1.tv_sec,stTp1.tv_nsec);

}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: