When the crystal oscillator is 11.0592, the following program can achieve a relatively accurate delay at the ms level:
1 void Delayms(unsigned int n)
2 {
3 unsigned int i,j;
4 for(j=n;j>0;j--)
5 for(i=112;i>0;i--);
6 }
Use keil to see the delay time. We first delay for 1ms, that is, Delayms(1).
Before entering Delayms, sec=0.00042209s
After delay, sec=0.00142253s
It can be known that the actual delay of Delayms (1) is 0.00142253s-0.00042209s=0.00100044s≈1ms
Similarly, if you want to delay for 15ms, use Delayms(15). The actual delay is 0.01480903s≈15ms, which is quite accurate.
Reference: www.jiangx.net/post/214.html
The above examples give us the inspiration that if we are not sure how much the delay program will delay, we can detect it through debugging.
--------------------------------------------------------------------------------
2. Assembly program (500ms):
C:0x0800 7F0F MOV R7,#0x0F
C:0x0802 7ECA MOV R6,#0xCA
C:0x0804 7D51 MOV R5,#0x51
C:0x0806 DDFE DJNZ R5,C:0806
C:0x0808 DEFA DJNZ R6,C:0804
C:0x080A DFF6 DJNZ R7,C:0802
C:0x080C 22 RET
Calculation:
The program has three layers of loops
One layer of loop n: R5*2 = 81*2 = 162us DJNZ 2us
Second layer loop m: R6*(n+3) = 202*165 = 33330us DJNZ 2us + R5 assignment 1us = 3us
Three-layer loop: R7*(m+3) = 15*33333 = 499995us DJNZ 2us + R6 assignment 1us = 3us
Outside the loop: 5us subroutine call 2us + subroutine return 2us + R7 assignment 1us = 5us
Total delay time = three-layer loop + outside loop = 499995+5 = 500000us = 500ms
Calculation formula: Delay time = [(2*R5+3)*R6+3]*R7+5
Assembly instruction quick reference table: http://www.cainiao8.com/embedded/51danpianji/danpianji03_zhiling.html
--------------------------------------------------------------------------------
3. Delay Program Analysis
When writing microcontroller programs in assembly language, this problem is relatively easy to solve. For example, if you use a 51 with a 12MHz crystal oscillator and want to delay for 20us, you can use the following code to meet general needs:
mov r0, #09h
loop: djnz r0, loop
The machine cycle of the 51 MCU is 1/12 of the crystal frequency, which is 1us per cycle. mov r0, #09h requires 2 machine cycles, and djnz also requires 2 machine cycles. Then the number stored in r0 is (20-2)/2. With this method, it is very convenient to achieve a delay of less than 256us. If a longer time is required, two layers of nesting can be used. And the accuracy can reach 2us, which is generally enough.
Nowadays, the most widely used one is undoubtedly Keil's C compiler. Compared with assembly, C has many advantages, such as easy program maintenance, easy to understand, and suitable for large projects. But the disadvantage is that the real-time performance is not guaranteed, and the instruction cycle of code execution cannot be predicted. Therefore, in situations where real-time performance is high, the joint application of assembly and C is also required. But does such a delay program also need to be implemented in assembly? In order to find the answer, I did an experiment.
When implementing a delay program in C language, the first thing that comes to mind is the commonly used loop statement in C. The following code is what I often see on the Internet:
void delay2(unsigned char i)
{
for(; i != 0; i--);
}
How high of an accuracy can this code achieve? In order to directly measure the effect of this code, I found the assembly code generated by Keil C based on this code:
; FUNCTION _delay2 (BEGIN)
; SOURCE LINE # 18
;---- Variable 'i' assigned to Register 'R7' ----
; SOURCE LINE # 19
; SOURCE LINE # 20
0000 ?C0007:
0000 EF MOV A,R7
0001 6003 JZ ?C0010
0003 1F DEC R7
0004 80FA SJMP ?C0007
; SOURCE LINE # 21
0006 ?C0010:
0006 22 COURT
; FUNCTION _delay2 (END)
You really don't know until you see it~~~ You will know how inaccurate this delay program is~~~ Just looking at the four main statements, it takes 6 machine cycles. In other words, its accuracy is at most 6us, and this does not include an lcall and a ret. If we list the value of i assigned when calling the function and the delay length in a table, it is:
i delay time/us
0 6
1 12
2 18
...
If you change (unsigned char i) in the program to (unsigned int i), the assembly program will become even more inconsistent with your intention.
Previous article:51 MCU and Mitsubishi PLC communicate successfully without protocol
Next article:Communication and control between PC and single chip microcomputer RS-232 serial port
- 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
- 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
- On May 5th, celebrate the Dragon Boat Festival, row dragon boats and play drums
- How 5G will impact the Internet of Things
- Powering Alternative Transportation Using Industrial Battery Packs
- Verilog implements instrument display regulator
- EEWORLD University ---- Power Stage: High Performance Parameters and Selection of MOSFET and Gate Driver
- Help: The solenoid valve coil controlled by DC 24V is open
- 【GD32E231 DIY】Temperature and humidity collection
- Urgent help, AD17 via setting problem?
- Capacitor charging time, MOS gate drive, TL431 reference, op amp followed by ADC sampling, composite tube and complementary push-pull advantages and disadvantages
- Serial communication can also communicate without sharing the same ground. What is the reason?