VxWorks_Timer

#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);

}
}
}
}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值