当前位置:网站首页>C language: timer principle

C language: timer principle

2022-06-13 09:12:00 Caixinzhi

First , Need to know ,C The timer of the language delays the running result , among , Timing unit: bits per millisecond .

Timer , seeing the name of a thing one thinks of its function , It will take time , therefore , To get time, we need to use time library , So you need to reference the header file <time.h>

To make a timer , First, you need to set the static variables t1 and t2, Because only when setting static variables ,t1 and t2 The value of will not become the original initialized value every time the loop is executed , We need to fix it , The initial value executed in the next loop is the result value after the previous execution , therefore , It is natural to consider the use of static variables .

Next , Is to set a condition in the loop , For example, the interval between changes you want to make is 500 In milliseconds , You can write... In a conditional statement t2 - t1 > 500, such , It can be well expressed . Then write the code or custom function you want the program to execute in the statement block of the conditional statement , And will t2 Assign a value to t1. If the conditional statement is not satisfied , Or after executing the conditional statement block , Then assign the current time to t2, therefore , And that's where it comes in clock Function to get the current time . such , You can simply make the most basic timer , The specific code is as follows :

while (1)
{
	static DWORD t1, t2;
	if (t2 - t1 > 500)
	{
		Timer();
		t1 = t2;
	}
	t2 = clock();
}

One thing to note here , Static variables t1 and t2 Can be assigned an initial value , But because you can't use it here , So there is no assignment to them , Equivalent to t1 and t2 be equal to 0, Because the initial values of static variables are 0.

above , It is the simplest timer , Next , We are going to upgrade this timer . We can encapsulate this timer into a function , The advantage of this is , When timers are used in other functions to delay , You can call this function directly to operate quickly and efficiently . The following is the code after encapsulating the timer :

void Add(int ms)
{
	static DWORD t1, t2;
	if (t2 - t1 > ms)
	{
		Timer();
		t1 = t2;
	}
	t2 = clock();
}

here , It is easy to find , We just need to enter the number of seconds we want to delay where we call this function , Call the function directly .

But here , There's a problem , When you want to call this timer function in two or more different places , Through debugging, it will be checked that the second timer called is useless , Unable to achieve the function you want . So how to solve this problem ?

Here is a method that I prefer , It is to pass in another parameter to determine which one wants to call the timer function . therefore , When we get here , We can simplify two static variables into one , To continue the implementation , See the following figure for the specific package timer function :

int Add(int ms, int id)
{
	static DWORD t[10];
	if (clock() - t[id] > ms)
	{
		t[id] = clock();
		return 1;
	}
	return 0;
}

If the conditions are met, execute and return 1, Go back if you don't agree 0. then , You can write a where you want to call if Sentence to judge 0 perhaps 1,1 You can continue to execute , So the code at the call can be written as :

if (Add(500, 0) == 1)   // Call the timer function 
{
	Timer();
}

For example, it is written as above , first id It can be transmitted 0 In the past , The second one can be passed 1……, And so on , Because what I write here is t[10], So at most, you can pass in 10 Number , It can also be set according to requirements t And so on . If it is equal to 1, You can execute Timer function , It has a good function of delay .

There are many extensions to the timer principle , It is also widely used in many situations , Here is just a simple help to understand , Further systematic study is needed to master deeper contents .

原网站

版权声明
本文为[Caixinzhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270533005401.html