定時器的使用例項(ExTimerSet/ExTimerCheck)
阿新 • • 發佈:2018-12-13
關於【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; } }