您的位置:首页 > 编程语言 > C语言/C++

vc6.0中c语言控制台程序中的定时技术(定时器)

2018-10-12 13:58 447 查看

打开main.c编译运行,注意,打开main.c之后一定要将win32timer.c也加进工程中一起编译,下面有图。
在开发单片机、ARM以及Linux系统的程序时,因为硬件定时中断的存在我们很方便构造出定时ISR,然而在VC6.0中,我们如何写一个定时程序呢?
其实,就是timeSetEvent()这个函数的调用。这个函数的解释见MSDN。详细原理,请看我代码中的注释,我写得很详细了。

main.c


//======================
// main.c
//======================
#include <stdio.h>
#include "win32timer.h"  // UserTimerSet(uDelay,UserFun)

int cnt = 0;

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


/*
 * 使用说明:
 *
 *     1. 用户程序需要 #include "win32timer.h" ;
 *     2. 用户需要将 win32timer.c 添加至根目录,或添加至工程;
 *    3. 用户程序直接调用UserTimerSet(uDelay,UserFun); 即可,
 *        其中,uDelay为定时调用的定时周期,单位为毫秒(ms),
 *        UserFun为用户被调函数void ISR(void)的函数名ISR。
 *     4. 可以同时使用多个timeSetEvent,每个timeSetEvent都可以
 *        返回定时器编号,详细见MSDN关于timeSetEvent的说明。
 */


//=======================
// win32timer.h
//=======================
#ifndef __WIN32TIMER_H__
#define __WIN32TIMER_H__

void UserTimerSet ( unsigned int uDelay, void (*UserFun)(void) ) ;

#endif  // @ #ifndef __WIN32TIMER_H__

win32timer.c


//=======================
// 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); //模拟中断返回,主线程得以继续执行
}



工程图

运行结果

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: