64,685
社区成员
发帖
与我相关
我的任务
分享
/***
*void xdelay(unsigned int ms)
*
*Purpose:
* Make an exactly millisecond delay
*
*Entry:
* unsigned int ms - The delay time (millisecond)
*
*Exit:
* void
*
*Exceptions:
* Note:
* .This function was created by Denny at Jun/26/2008
* .Dongguan Siliten Electronics CO., LTD.
* .The timer frequency is 1193180Hz
* .The timer counter is 16bits, so only elapsed 55ms for\
* counter a circle.
*
*******************************************************************************/
void xdelay(unsigned int ms)
{
unsigned int base_counter;
unsigned int circle;
unsigned int counter;
unsigned int last_counter;
unsigned long end;
unsigned long cur;
disable();
//outportb(0x43, 0x30);
base_counter = inportb(0x40);
base_counter += inportb(0x40)<<8;
enable();
circle = 0;
last_counter = 0;
end = (unsigned long)ms*1193;
cur = 0;
while (end > cur)
{
disable();
counter = inportb(0x40);
counter += inportb(0x40)<<8;
enable();
counter = base_counter - counter;
if (counter-last_counter > 50) // Illegal data
{
last_counter = counter;
continue;
}
if ( (counter < last_counter) && (last_counter > 65000) && (counter < (65536-65000)) )
circle++;
cur = ((unsigned long)circle<<16) + counter;
//printf("base:%u|cir:%u|cnt:%u|last:%u|end:%lu|cur:%lu\n", base_counter, circle, counter, last_counter, end, cur);
last_counter = counter;
}
}
#include <windows.h>
#include <time.h>
#include <stdio.h>
ULONG GetTimeDis(struct tm first, struct tm second) //计算两次时间差
{
return abs((first.tm_hour * 3600 + first.tm_min * 60 + first.tm_sec)
- (second.tm_hour * 3600 + second.tm_min * 60 + second.tm_sec));
}
void Delay(ULONG ulMicroSeconds)
{
LARGE_INTEGER timeStop;
LARGE_INTEGER timeStart;
LARGE_INTEGER Freq;
ULONG ulTimeToWait;
if (!QueryPerformanceFrequency( &Freq ))
return;
ulTimeToWait = Freq.QuadPart * ulMicroSeconds / 1000 / 1000;
QueryPerformanceCounter ( ×tart );
timeStop = timeStart;
while( timeStop.QuadPart - timeStart.QuadPart < ulTimeToWait )
{
QueryPerformanceCounter( ×top );
}
}
int main(void)
{
struct tm timeStart, timeEnd;
_getsystime(×tart);
Delay(1000 * 1000 * 3); //3秒
_getsystime(&timeEnd);
printf("用时:%d秒\n", GetTimeDis(timeStart, timeEnd));
return 0;
}