Design and Application of software timer in W indows Operating System

Source: Internet
Author: User

Abstract: In order to realize accurate timing of computer software, a high-precision microsecond-level timer is designed. The timer uses the high-precision counter of the computer to complete the timer function. The timer precision on the pentiurn 1 V computer on the Great Wall can reach dozens of microseconds. To test the performance of this high-precision timer, a control program based on Windows multithreading mechanism is developed using Visual C ++, enables the computer to output analog signals of different frequencies and waveforms through an L6-bit D/A converter. Test results show that Windows-based high-precision timer has high timing stability and accuracy. Finally, the characteristics of piezoelectric ceramic actuator were investigated by using the timer.
Keywords: high-precision timer windows programming Multithreading

1) Windows operating system timer.
The timer uses WM-TIMER message ing for simple time control. WM-TIMER messages are generated by system interruptions of the operating system 18.2hz. Its timing precision can reach about 55 ms. This timer is easy to use. You only need to set the time interval in settimer () and add a message Response Processing statement to ontimer () to complete the scheduled time operation. Because Windows operating systems work in the way of multi-threaded preemptive multitasking, and the priority of WM-TIMER messages is low, this method is applicable to situations where the timer precision is not high, such as the screen regularly refresh and the dynamic display of Bitmap.

2) multimedia timer.
The timer has its own separate thread to call the callback function. It does not rely on the message mechanism, but is based on the interruption. In a scheduled task with a precision of more than 20 ms, the precision can reach 1 ms. The scheduled interval is 7 ~ The error is between 20 ms and 1 ~ 3 ms. Timing smaller than 7 ms is not easy to implement.

3) software and hardware can be combined to improve the timing accuracy.
This timer uses an external hardware clock circuit to obtain precise timing trigger signals, and uses the external timing trigger as a hardware interrupt. The timing precision can reach several microseconds.
Because the Windows operating system shields the hardware operation, the system application can only work on the Ring Level 3 of the CPU, and has no operation permission on the hardware port. Therefore, you need to write a Virtual Device Driver (VxD), which increases the workload and instability.
In newer computer hardware systems, counters with high precision performance are included, and Their counting frequency is related to the clock frequency of the CPU. It can be used to accurately estimate the running time of a piece of program code. The queryperformancefrequency () function provided by Win 32 api can obtain the counter frequency with high precision. The queryperformancecounter () function can be used to obtain the current counter value. The function prototype is as follows:
Bool queryperformancefrequency F LARGE-INTEGER1 pfrequency );
Bool queryperformancecounter F la rge-integer1pcount );
Among them, the Data Type LARGE-INTERGER can be an 8-byte long integer number, can also be a combination of two 4-byte long integer number structure, the specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:
Typedef Union-large-INTEGER {
Struct {
DWORD lowpart; // 4-byte integer
Long hightpart;/4-byte integer
}
Longlong quadpart;/8-byte integer
} La. RGE-INTEGER;
In time calculation, queryperformance is called first. frequency () function to obtain the clock frequency of the computer's Internal timer I factory C. Then, call the queryperformancecounter () function before and after the strict timing is required. The accurate time of the event experience can be calculated using the difference between the two counts and the clock frequency. With a slight modification to the above algorithm, you can get a precise timer in microseconds. For the sake of timing, we need to get a precise period of time. The specific method is as follows:
1) obtain the timer clock frequency. factory;
2) Calculate the Count required by T for a period of time = I factory × T;
3) read the initial value of the high-precision counter countl at the start of the timer;
4) read the value count 2 of the high-precision counter repeatedly until count 2 is a count 1/count. Send the message at the scheduled time.
5) Assign the Count value of Count 2 to count 1 and transfer it to 4 ).
In this way, we can obtain a timer with a timer length of approximately t in microsecond. However, in actual use, the timer may have some errors, and the error size is related to the frequency of the high-precision counter. The higher the counter frequency, the smaller the error. In addition, because the Windows operating system is based on preemptible multitasking, the timer precision is also affected.
With the multi-thread mechanism of the Windows operating system, you can implement timed and real-time display functions. The main thread is used for interaction with users, such as parameter setting and display of feedback information. The worker thread is used to start the timer, send the control signal, and transmit the control information to the main thread. The workflow 1 is shown in the following figure:

 

Related Article

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.