Practical VC Time Control Function

zhaozj2021-02-11  151

With the rapid development of hardware and software, computer technology has been widely applied to the field of automation control. In order to implement real-time control, the control program must be able to accurately complete timing and timing functions. VC provides a lot of functions about time operation, which will be explained separately according to their precision.

General time control function

VC programmers use Windows's WM-TIMER message mapping to make simple time control: 1. Call function setTimer () sets fixed time separation, such as SetTimer (0,200, null) sets the time interval for setting 200 milliseconds; 2. Application Increase timing response functions onTimer () and add a response to the processing statement in this function to complete the operation time. This timing method is very simple, but the timing function is like the delay function of the SLEEP () function, the accuracy is low, and can only be used to realize the case of the constant accuracy requirements such as dynamic display such as bitmaps, and This method should be avoided under conditions with high precision requirements.

Accuracy time control function

In the case where the error is not more than 1 millisecond, a GetTickCount () function can be used. The return value of the function is a DWORD type, indicating the time interval experienced after the computer starts in milliseconds. Using the following programming statement, you can implement a 50 millisecond precision, and the error is less than 1 millisecond.

DWORD DWSTART, DWSTOP;

// Start value and termination value

DWSTOP = GetTickCount ();

While (True)

{

DWSTART = DWSTOP;

// The last termination value becomes a new start value

/ / Add a corresponding control statement here

DO

{

DWSTOP = GetTickCount ();

WHILE (DWSTOP - 50

}

High precision time control function

For general real-time control, use the getTickCount () function to meet the accuracy requirements, but to further improve the timing accuracy, you must use the QueryPerformanceFrequency () function and the queryperformanceCounter () function. These two functions are VCs provided for the high-precision time function for Windows 9x only for Windows 9X, and require a computer to support high-precision timers from the hardware. QueryPerformanceFrequency () Function and QueryPerFormanceCounter () function prototype:

Bool QueryperFormanceFrequency (large-integer * lpfrequency);

Bool QueryperFormanceCounter (Large-Integer * lpcount);

The data type LARGE-INTEGER can be an integer as an 8-byte length, or as a joint structure of two 4-byte lengths, and the specific usage is determined according to whether the compiler supports 64 bits. This type is defined as follows:

TypedEf Union-Large-Integer

{

Struct

{

DWORD lowpart; // 4 byte integer

Long highpart; // 4 byte integer

}

Longlong quadpart;

// 8 byte integer

Large-integer;

Before the timing, you should call the queryperformancefrequency () function to get the clock frequency of the internal timer of the machine. The author uses this function on three PentiumII machines with the frequency of 266, 300, 333, and the clock frequency obtained is 1193180 Hz. Next, the author calls the QueryPerFormanceCounter () function before and after the event that needs strict timing, and uses the difference and clock frequency of the two acquisitions, and the exact time of the event experience can be calculated. The following procedure is used to test the exact duration of the function SLEEP (100). Large-integer bitmp;

Longlong QPart1, QPart2;

Double DFMINUS, DFFREQ, DFTIM;

QueryperFormanceFrequency (& Litmp);

/ / Get the clock frequency of the counter

DFFREQ = (double) Litmp.quadpart;

QueryperFormanceCounter (& litmp);

// Get the initial value

QPART1 = Litmp.quadpart;

Sleep (100);

QueryperFormanceCounter (& litmp);

// Get the termination value

QPART2 = Litmp.quadpart;

DFMINUS = (Double) (qPart2 - qPart1);

DFTIM = DFMINUS / DFFREQ;

// Get the corresponding time value

Execute the above program, the result is DFTIM = 0.097143767076216 (seconds). The careful readers will find that each execution is different, there is a certain difference, which is due to the error of Sleep () itself.

This article introduces three timing or timing implementation methods, readers can choose according to their own actual situation to achieve timed and timing functions of the program. The above programs were debugged in the VC 6.0, Windows 98 environment.

转载请注明原文地址:https://www.9cbs.com/read-6069.html

New Post(0)