Embedded C universal delay driver writing method

In embedded C programming, software delays are inevitably used. This is usually achieved through loop statements. By controlling the number of loops in the loop statement, many different delay times can be obtained. In order to facilitate the use and increase the reuse rate of the program code, the loop statement is generally encapsulated into a function with a parameter called a delay function.

Embedded C universal delay driver writing method


Such as:

Void wait(unsigned int n)

{

Unsigned int i;

For(i=0;i〈n;i++);

}

The parameters of the delay function (parameters, such as the variable n in the above example) are variables that control the number of loops in the loop statement. In this way, when the software delay is needed, only the delay function needs to be called, and the actual parameter (actual parameter, ie, the actual value of n) is substituted into the parameter, and the delay time corresponding to the actual parameter can be obtained.

This is the classic software delay implementation method, very simple.

But the astute reader will find that the parameters of the delay function (such as n above) represent the “cycle number” of the loop statement rather than the “actual delay time”. In general, if the loop statement is b per cycle (note that the unit is "step", that is, a clock cycle, the same below), the intrinsic time required for function call, value, and return is a, then, Given the parameter n, the actual delay time for calling a delay function should be t = a + b*n , - not n!

This means that when the required delay time is t, the actual parameter that should be passed in is n = (ta)/b, not t. In this way, in order to obtain a more accurate delay, it is inconvenient to manually calculate the actual parameters before each function call; secondly, when the frequency of the crystal oscillator needs to be changed, all the delay parameters in the program must be recalculated. Obviously not conducive to the transplantation of the program.

In order to solve these two problems and improve the portability of the program, macros can be used to pre-correct parameters of the delay function. For example, for the wait delay function given above, the following macro definition can be used:

#define delay(n) wait( ( (n) - a ) / b )

In this way, calling delay(t) means calling wait((ta)/b) to obtain a delay of time t, which achieves synchronization of parameters and delay time, and is more convenient to use.

In order to further improve the portability, the software delay can adapt to different crystal frequencies, should follow the above ideas to choose a better solution. So what should be done? In fact, the method is very simple. Suppose we can obtain a delay of n steps by calling a delay function wait_step(n), and set the working frequency to f1. That is, the running time of each step is T=1/f1, then the actual delay time obtained is t=n. *T=n/f1. When the operating frequency becomes f2=C*f1, the program runs faster by a factor of C. In order to still obtain the delay of time t, the number of steps of the program operation should be C times the original, ie to call wait_step(n*C). ). In this way, we can define the following macro to complete the (n*C) operation:

#define C 4

#define delay_t(n) wait_step( n*C )

The first line is generally written at the beginning of the file. When you modify the frequency of the crystal, you only need to modify this field. You do not need to modify the parameters of each wait_step(n) one by one in the program, which is very convenient.

According to the method described above, you can write an accurate, easy-to-use, universal delay driver.

A complete delay driver is given below. This is a version I wrote earlier and was recently reorganized. The compiler is ICC AVR V7.13A and the operating environment is all chips of the AVR series. There are three statements used:

Microsecond delay: delay_us(n); delay n microseconds

Millisecond delay: delay_ms(n); delay n milliseconds

Delay in seconds: delay_s(n); delay in n seconds (max. 65 seconds)

#ifndef _DELAY_H_

#define _DELAY_H_

#define uchar unsigned char

#define uint unsigned int

/***************** Configuration Information******************/

#define CRYSTAL 8.0 //Set the crystal frequency. The unit is MHZ

#define delay_us(T) \

Wait_us( (T) ” 14.0/CRYSTAL ( ((T)*CRYSTAL-8)/6.0 : 1 )

#define delay_ms(T) wait_ms( T )

#define delay_s(n) delay_ms(1000*(n))

/**

Function Name: wait_us

Function: Microsecond (us)-level delay (rough)

Note: The delay time is: T = 8 + 6 * n (steps)

**/

Void wait_us( unsigned int n )

{

Do{

N--;

}while(n);

}

/** Function name: wait_ms

Function: ms-level delay

Explanation: When do.. When the .while internal delay is 992us, the error is 17 steps.

**/

Void delay_ms( unsigned int n )

{

Do {

Delay_us(992);

}while(--n);

}

#endif

Mini Gaming PC


What suddenly appear into your mind when hear Mini Gaming PC? Is there quality heat-releasing fan and box design of Mini Gaming PC Build? Does it run stable and smoothly when handle heavier jobs, like Photoshop, Pr, engineering design and drawing, 3d Max, big games, etc. ? now we can be responsible to answer [ Yes". No matter cheap mini gaming pc or Mini Gaming PC Under $500, equips with quality fan to heat releasing. Therefore, no need to worry that again.

To processor, can do from intel celeron j4125, N5105 up to i3, i5 i7 10th 11th 12th with or without video graphics. To slots, same rich as traditional computer tower, like above 4 or 6 usb ports, 2 Rj45, VGA, PD, slot, etc.

You can also find Mini Gaming PC, j4125 Mini PC ,Custom All In One PC, windows 10 Education Laptop,Quad Core Processor Laptop, 15 Inch Gaming Laptop ,windows Yoga Laptop , 8 inch Android Tablet, etc.

If any other special requirements interest, you can also let us know, will try our best support you.

Meet your unique demand in this field is our mission, so just feel free contact us whenever you have different idea.


Mini Gaming PC Build,Cheap Mini Gaming PC,Mini Gaming PC Under $500,Mini Gaming Desktop

Henan Shuyi Electronics Co., Ltd. , https://www.shuyiminipc.com