SetTimer()设置的定时器的定时时间>50ms,这在实时数据采集中是不够的。用多媒体定时器能精确到1ms,效果很好。
//定时器------------------------
#define
TIMER_ACCURACY 1
int
TimerID;
UINT
wAccuracy;
int
InitTimer();
int
ReleaseTimer();
void PASCAL TimerProc(UINT wTimerID,UINT msg,DWORD dwUser,DWORD dwParam,DWORD dlParam);
int
InitTimer()
{
TIMECAPS tc;
if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR) //获取系统的取值范围
{
wAccuracy=min(max(tc.wPeriodMin,TIMER_ACCURACY),tc.wPeriodMax);
timeBeginPeriod(wAccuracy);
return 1;
}
else
{
MessageBox(NULL,"定时器无法使用!",NULL,NULL);
return 0;
}
}
int ReleaseTimer()
{
timeKillEvent(TimerID);
timeEndPeriod(wAccuracy);
return 1;
}
/*名称:TimerProc
功能:定时处理
参数:wTimerID定时器ID,msg消息参数,dwUser使用者,dwParam参数1,dlParam参数2
*/
void PASCAL TimerProc(UINT wTimerID,UINT msg,DWORD dwUser,DWORD dwParam,DWORD dlParam)
{
}
//打开定时器
TimerID=timeSetEvent(1000,wAccuracy,(LPTIMECALLBACK)TimerProc,(DWORD)0,TIME_PERIODIC);
//1000为定时时间,表示1000ms