?? timer.c
字號:
/* timer.c */
/* 本程序使用Borland C或者Turbo C編譯*/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <bios.h>
#include <conio.h>
/* Escape key */
#define VK_ESC 0x11b
#define TIMER 0x1c /* 時鐘中斷的中斷號 */
/* 中斷處理函數在C和C++中的表示略有不同。
如果定義了_cplusplus則表示在C++環境下,否則是在C環境下。 */
int TimerCounter=0; /* 計時變量,每秒鐘增加18。 */
/* 指向原來時鐘中斷處理過程入口的中斷處理函數指針(句柄) */
void interrupt ( *oldhandler)();
/* 新的時鐘中斷處理函數 */
void interrupt newhandler()
{
/* increase the global counter */
TimerCounter++;
/* call the old routine */
oldhandler();
}
/* 設置新的時鐘中斷處理過程 */
void SetTimer(void interrupt (*IntProc)())
{
oldhandler=getvect(TIMER);
disable(); /* 設置新的時鐘中斷處理過程時,禁止所有中斷 */
setvect(TIMER,IntProc);
enable(); /* 開啟中斷 */
}
/* 恢復原有的時鐘中斷處理過程 */
void KillTimer()
{
disable();
setvect(TIMER,oldhandler);
enable();
}
void main(void)
{
int key,time=0;
SetTimer(newhandler); /* 修改時鐘中斷 */
for (;;)
{
if (bioskey(1))
{
key=bioskey(0);
if (key==VK_ESC) /* 按escape鍵提前退出程序 */
{
printf("User cancel!\n");
break;
}
}
if (TimerCounter>18) /* 1秒鐘處理一次 */
{
/* 恢復計時變量 */
TimerCounter=0;
time++;
printf("%d\n",time);
if (time==10) /* 10秒鐘后結束程序 */
{
printf("Program terminated normally!\n");
break;
}
}
}
KillTimer(); /* 恢復時鐘中斷 */
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -