Many friends say that the delay time cannot be accurately controlled in C and it 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:How to implement ICSP connection circuit of PIC18F4550
Next article:Introduction to the features and advantages of PIC microcontrollers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications