Vc6.0 c language console program in the timing technology of timer

  • 2020-04-02 02:19:16
  • OfStack

Open main.c compile run, note, open main.c must be included in the win32timer.
In the development of SCM, ARM and Linux system procedures, due to the existence of hardware timing interrupt we are very convenient to construct timing ISR, but in VC6.0, how do we write a timing program?
It's just a call to the timeSetEvent() function. See MSDN for an explanation of this function. See the comments in my code for details.

The 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)
{
 
 UserTimerSet ( 1000, myISR_Called_Per_1000ms ) ; 

 while (cnt<10);

 return 0; 
}
void myISR_Called_Per_1000ms(void)
{ 
 printf("The Program has run %dsn",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


//=======================
// win32timer.c
//=======================
#include <windows.h>
#include "win32timer.h"
#pragma comment(lib,"winmm.lib") //Import the winmm.lib multimedia library

HANDLE mainhandle;     //The main thread handle
CONTEXT Context;     //The main thread switches context
static void (*TimerCallFun)(void);  //Declare the user to call the function pointer

static void __stdcall TimerISR(unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2);
//======================================================================================
//Function function: the timer setup (initialization) function that the user needs to call
//Entry parameter: uDelay: timer duration in ms
//& have spent & have spent & have spent Void (*UserFun)(void) : function pointer to the user function void fun (void)
//Return value: none
//======================================================================================
void UserTimerSet ( unsigned int uDelay, void (*UserFun)(void) ) 
{
 HANDLE cp,ct;

 TimerCallFun = UserFun;     //Gets the function pointer to the function that the user is calling periodically
 Context.ContextFlags = CONTEXT_CONTROL;
 cp = GetCurrentProcess(); //Gets the current process handle
 ct = GetCurrentThread(); //Gets the pseudo handle to the current thread
 DuplicateHandle( cp, ct, cp, &mainhandle, 0, TRUE, 2 ); //False handle conversion, get the thread real handle

 
 timeSetEvent( uDelay, 0, TimerISR, 0, TIME_PERIODIC );
 
}
//======================================================================================
//Function functionality: the function that the timeSetEvent needs to call on a regular basis
//Entry parameters: unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2, see MSDN for details
//Return value: none
//======================================================================================
static void __stdcall TimerISR(unsigned int uTimerID, unsigned int uMsg, unsigned long dwUser, unsigned long dw1, unsigned long dw2)
{
 SuspendThread(mainhandle); //Aborts the main thread, an analog interrupt occurs, but no register is saved
 GetThreadContext(mainhandle, &Context); //Get the main thread context and prepare for the task switch
 //===========================================================================================
 (*TimerCallFun)();    //Or TimerCallFun (); -- an interrupt call to a user-defined implementation
 //===========================================================================================
 ResumeThread(mainhandle); //The mock interrupt returns and the main thread continues
}


Engineering drawing

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201404/20140414074117.jpg? 201431474322 ">

The results

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201404/20140414074146.jpg? 201431474357 ">


Related articles: