Timing Technology (timer) _c language in C language console program in vc6.0

Source: Internet
Author: User

Turn on the MAIN.C compile run, note that after opening the MAIN.C must be added to the project WIN32TIMER.C also compiled together, the following map.
In the development of SCM, ARM and Linux system programs, because of the existence of hardware timing interruption, we are very convenient to construct a timed ISR, however, in the VC6.0, how do we write a timer program?
In fact, it's the timeSetEvent () call to this function. The explanation for this function is shown in MSDN. Detailed principles, please see the comments in my Code, I wrote in detail.

Main.c

Copy Code code as follows:

//======================
Main.c
//======================
#include <stdio.h>
#include "win32timer.h"//Usertimerset (Udelay,userfun)

int cnt = 0;

void myisr_called_per_1000ms (void);

int main (void)
{
/* Every 1000ms call once 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

Copy Code code as follows:

/*
* Instructions for use:
*
* 1. User programs need to #include "win32timer.h";
* 2. The user needs to add the WIN32TIMER.C to the root directory, or add it to the project;
* 3. The user program calls Usertimerset (Udelay,userfun) directly; Can
* Wherein, the udelay is the timing period of the timed call, in milliseconds (ms),
* Userfun is a function name ISR for user-tuned function void ISR (void).
* 4. You can use multiple timesetevent at the same time, each timesetevent can
* Return to the timer number, see the MSDN about timesetevent instructions.
*/


//=======================
Win32timer.h
//=======================
#ifndef __win32timer_h__
#define __win32timer_h__

void Usertimerset (unsigned int udelay, void (*userfun) (void));

#endif//@ #ifndef __win32timer_h__

Win32timer.c

Copy Code code as follows:

//=======================
Win32timer.c
//=======================

#include <windows.h>
#include "Win32timer.h"

#pragma comment (lib, "Winmm.lib")//import Winmm.lib Multimedia Library

/* Global variable * *
HANDLE Mainhandle; Main thread handle
Context context; Main thread Toggle Context
static void (*timercallfun) (void); Declaring a user calling a function pointer

/* Function Declaration * *
static void __stdcall TIMERISR (unsigned int utimerid, unsigned int umsg, unsigned long dwuser, unsigned long dw1, unsigned Long dw2);

//======================================================================================
//function function: User needs to call the timer settings (initialization) Function
//Entry parameters: Udelay: Timer time is long, Unit is Ms
//     Void (*userfun) (void): function pointer to user function void fun (void)
//return value: None
//======================================================================================
void Usertimerset (unsigned int udelay, void (*userfun) (void)
{
 handle cp,ct;

 timercallfun = userfun;    //function pointer to the function that the user is called periodically
 context.contextflags = Context_control;
&NBSP;CP = GetCurrentProcess ();  //get current process handle
 ct = GetCurrentThread ();  //get current thread pseudo handle
  DuplicateHandle (CP, CT, CP, &mainhandle, 0, TRUE, 2);  //pseudo handle conversion, get line Chengjian handle

 /* Simulate set timer interrupt, open a timer thread * *
 timesetevent (udelay, 0, TIMERISR, 0, Time_periodic);
 /* If you need to cancel the timer, you can call the Timekillevent () function, as described in msdn*/

//======================================================================================
//function function: timeSetEvent requires a timed call function
//ENTRY parameters: Unsigned int utimerid, unsigned int umsg, unsigned long dwuser, unsigned long dw1, unsigned long dw2, see MSDN
//return value: None
//======================================================================================
static void __stdcall TIMERISR (unsigned int utimerid, unsigned int umsg, unsigned long dwuser, unsigned long dw1, unsigned Long dw2)
{
 suspendthread (mainhandle);  //abort the main thread running, simulating the interrupt generation. But did not save the Register
 getthreadcontext ( Mainhandle, &context);  //get the main thread context, prepare for switching tasks
 //================================================ ===========================================
  (*timercallfun) ();    //or TimerCallFun ();-- --Interrupt calls to user-defined implementations
 //==================================================================================== =======
 resumethread (mainhandle);  //simulation interrupt return, main thread can continue



Engineering drawing

Run results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.