定时器的使用实例(ExTimerSet/ExTimerCheck)

关于【int ExTimerSet(ST_TIMER *pstTimer,unsigned long ulMs)】和
【unsigned long ExTimerCheck(ST_TIMER *pstTimer)】结合作为定时器使用实例。

实例1:正确定时器的用法

void ExTimerSet_FUN1()   //设置一个10s定时器
{
	int ret;
	ST_TIMER SetTimer = {0};  //定义入参为结构体变量,并赋初值给变量
	unsigned long Ms = 10*1000;
	ulong TimeLeft;

	ret = ExTimerSet(&SetTimer,Ms);  //&SetTimer为实参,取这个结构体变量的首地址
	ASSERT(ret == RET_OK,"Timer Set Fail, ret = %d\n", ret);
    while(1)
    {
		TimeLeft = ExTimerCheck(&SetTimer);
		printline_p(5, 0, "Remaining Time:%ds",TimeLeft /1000);
		if(TimeLeft == 0)   break;
		
		 //如果有按键按下,且按键为CNACEL键则跳出死循环且直接返回以结束函数执行
		if(!kbhit() && getkey()==KEYCANCEL)  
		{
		   FAIL("FAIL! CANCEL AND RETURN");
    			return;
		}
              
   }
}

实例2:不正确用法
注意:若声明ST_TIMER *SetTimer = {0};,则设置10s定时器失败,不起作用,读取的TimeLeft 一直显示10s,无法起到定时器的作用。

void ExTimerSet_FUN2()   //设置一个10s定时器
   {
   int ret;
   ST_TIMER *SetTimer = {0};
   
ret = ExTimerSet(SetTimer,Ms); 
ASSERT(ret == RET_OK,"Timer Set Fail, ret = %d\n", ret); 

while(1){ 
TimeLeft = ExTimerCheck(SetTimer); 
printline_p(5, 0, "Remaining Time:%ds",TimeLeft /1000); 
if(TimeLeft == 0) break;
}

}

猜你喜欢

转载自blog.csdn.net/ZoeyZY/article/details/84784230