Many friends say that the delay time cannot be accurately controlled in C and is not as intuitive as assembly.
In fact, this is not the case. A deep understanding of the delay function will help you design an ideal frame price.
Generally, we use
for(x=100;--x;){;}This sentence is equivalent to x=100;while(--x){;};
or for(x=0;x<100;x++){;}
Let's write a delay function.
It is important to note here that X=100 does not mean that the loop will exit after only 100 instructions have been executed.
You can look at the compiled assembly:
x=100;while(--x){;}
After compilation:
movlw 100
bcf 3,5
bcf 3,6
movwf _delay
l2 decfsz _delay
goto l2
return
From the code, we can see that the total number of instructions is 303, and the formula is 8+3*(X-1). Note that the number of cycles is X-1, which is 99.
The summary here is the loop body when x is of type char. When x is int, it is more affected by the value of X.
It is recommended to design a char type loop body, and then use another loop body to call it, so as to achieve accurate and long-term delay.
Here is a function that can accurately control the delay. The assembly code of this function is the most concise and can most accurately control the instruction time:
void delay(char x,char y){
char z;
do{
z=y;
do{;}while(--z);
}while(--x);
}
The instruction time is: 7+(3*(Y-1)+7)*(X-1)
If we add the call instruction for function call, page setting, and 7 instructions for passing parameters.
Then it is: 14+(3*(Y-1)+7)*(X-1).
If the delay requirement is not particularly strict, you can use this function:
void delay(){
unsigned int d=1000;
while(--d){;}
}
This function produces a delay of 10003us under a 4M crystal, which is 10MS.
If D is changed to 2000, it will be 20003us, and so on.
Some friends don't understand why we don't use while(x--) followed by decrement to control how many cycles the set X value is?
Now let's look at the assembly code that compiles it:
bcf 3,5
bcf 3,6
movlw 10
movwf _delay
l2
decf _delay
incfsz _delay,w
goto l2
return
It can be seen that there is one more instruction in the loop body, which is not concise. Therefore, it is best to use the front decrement to control the loop body in PICC.
Let’s talk about this sentence:
for(x=100;--x;){;} and for(x=0;x<100;x++){;}
The two have the same meaning literally, but you can view the code through assembly. The latter code is long, while the former compiles the concise code well.
Therefore, it is best to write the loop body in the former form in PICC. A good C compiler will automatically convert the increment loop into a decrement loop, because this is determined by the hardware characteristics of the processor.
PIC is not a very smart C compiler, so the human brain is still the first priority. Mastering some experience is helpful in writing efficient and concise code.
Previous article:Using PIC to write efficient bit shift operations
Next article:Using constant pointers in PIC
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- ESP32-S2-Kaluga-1 Espressif's new multimedia development board
- Get a more intuitive understanding of 5G. Watch the keynote speech by Rohde & Schwarz and win prizes!
- msp432 record 2-uart and display usage
- [AB32VG1 Development Board Review] RTC Electronic Clock
- Live broadcast at 10 am today [In-depth study of TI's industrial processor chip AM57X with machine learning accelerator]
- Protecting Your IP Cores - Part 1 Soft IP, Section 1: Encryption of HDL Code
- EEWorld 2020 annual summary: useful articles, popular reviews, download top list, and well-received courses, all here!
- Hey guys, this is my first time using Cadence's Pspice simulation and I encountered this problem
- Industrial Control Electronics Popular Data Download Collection
- How to use Bluetooth 4.2 to implement the Internet of Things