打开main.c编译运行,注意,打开main.c之后一定要将win32timer.c也加进工程中一起编译,下面有图。
在开发单片机、ARM以及Linux系统的程序时,因为硬件定时中断的存在我们很方便构造出定时ISR,然而在VC6.0中,我们如何写一个定时程序呢?
其实,就是timeSetEvent()这个函数的调用。这个函数的解释见MSDN。详细原理,请看我代码中的注释,我写得很详细了。
main.c
void myISR_Called_Per_1000ms(void);
int main(void)
{
 /* 每1000ms调用一次myISR_Called_Per_1000ms */
 UserTimerSet ( 1000, myISR_Called_Per_1000ms ) ; 
 while (cnt<10);
 return 0; 
}
void myISR_Called_Per_1000ms(void)
{ 
 printf("The Program has run %ds/n",cnt++);
}
win32timer.h
//=======================
// win32timer.h
//=======================
#ifndef __WIN32TIMER_H__
#define __WIN32TIMER_H__
void UserTimerSet ( unsigned int uDelay, void (*UserFun)(void) ) ;
#endif  // @ #ifndef __WIN32TIMER_H__
win32timer.c
#include <windows.h>
#include "win32timer.h"
#pragma comment(lib,"winmm.lib") //导入winmm.lib多媒体库
/* 全局变量 */
HANDLE mainhandle;     //主线程句柄
CONTEXT Context;     //主线程切换上下文
static void (*TimerCallFun)(void);  //声明用户调用函数指针
/* 函数声明 */
static void __stdcall TimerISR(unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2);
//======================================================================================
// 函数功能:用户需要调用的定时器设置(初始化)函数
// 入口参数:uDelay:定时器定时时长,单位为ms
//     void (*UserFun)(void):指向用户函数 void fun (void) 的函数指针
// 返 回 值:无
//======================================================================================
void UserTimerSet ( unsigned int uDelay, void (*UserFun)(void) ) 
{
 HANDLE cp,ct;
 TimerCallFun = UserFun;     //得到用户被定时调用的函数的函数指针
 Context.ContextFlags = CONTEXT_CONTROL;
 cp = GetCurrentProcess(); //得到当前进程句柄
 ct = GetCurrentThread(); //得到当前线程伪句柄
 DuplicateHandle( cp, ct, cp, &mainhandle, 0, TRUE, 2 ); //伪句柄转换,得到线程真句柄
 /*模拟设置定时器中断,开启一个定时器线程*/
 timeSetEvent( uDelay, 0, TimerISR, 0, TIME_PERIODIC );
 /*如果需要取消定时器,则可以调用timeKillEvent()函数,详见MSDN*/
}
//======================================================================================
// 函数功能:timeSetEvent需要定时调用的函数
// 入口参数:unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2,详见MSDN
// 返 回 值:无
//======================================================================================
static void __stdcall TimerISR(unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2)
{
 SuspendThread(mainhandle); //中止主线程的运行,模拟中断产生.但没有保存寄存器
 GetThreadContext(mainhandle, &Context); //得到主线程上下文,为切换任务做准备
 //===========================================================================================
 (*TimerCallFun)();    //或者TimerCallFun(); ―――― 用户自定义实现的中断调用
 //===========================================================================================
 ResumeThread(mainhandle); //模拟中断返回,主线程得以继续执行
}

运行结果
新闻热点
疑难解答
图片精选