发表于:2004-11-26 16:48:00
3楼
下面的可以做到延时1 us 和cpu速度无关
#define OVERHEAD 0xFFFFFFFF
static int DelayMicorSecond(double us)
{
LARGE_INTEGER PerFreq;
LONGLONG StartQuad,StopQuad;
double dFreq,dTime,deltaQuad;
if(QueryPerformanceFrequency(&PerFreq))
dFreq = (double)PerFreq.QuadPart;
else
AfxMessageBox("Error");
QueryPerformanceCounter(&PerFreq);
StartQuad = PerFreq.LowPart;
int nCount = 0;
while(true)
{
// DunselFunction();//delay function
QueryPerformanceCounter(&PerFreq);
StopQuad = PerFreq.LowPart;
deltaQuad =(double)( (StopQuad>=StartQuad)?(StopQuad-StartQuad):(StopQuad-StartQuad+OVERHEAD));
dTime = (double)(deltaQuad/dFreq);
nCount++;
if(dTime*1000000>us)
break;
}
return nCount;
}