Different types of variables can be used in C programs for delay design. After experimental testing, the use of unsigned char type has more optimized code than unsigned int. Unsigned char should be used as a delay variable when used. Take a microcontroller with a crystal oscillator of 12MHz as an example. The crystal oscillator is 12MHz, which means that one machine cycle is 1us. Extended reading: Detailed explanation of the assembly delay program algorithm
1. 500ms delay subroutine
program:
void delay500ms(void)
{
unsigned char i,j,k;
for(i=15;i>0;i--)
for(j=202;j>0;j--)
for(k=81;k>0;k--);
}
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
2. 200ms delay subroutine
program:
void delay200ms(void)
{
unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=132;j>0;j--)
for(k=150;k>0;k--);
}
3. 10ms delay subroutine
program:
void delay10ms(void)
{
unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=4;j>0;j--)
for(k=248;k>0;k--);
}
4. 1s delay subroutine
program:
void delay1s(void)
{
unsigned char h,i,j,k;
for(h=5;h>0;h--)
for(i=4;i>0;i--)
for(j=116;j>0;j--)
for(k=214;k>0;k--);
}
Regarding the precise delay of the microcontroller C language, many functions on the Internet only give an approximate delay value without an exact value, and thus fail to meet the high precision requirements. This function overcomes the above shortcomings and can accurately count the delay value to within 1us. The CPU used in this example is the STC12C5412 series 12x speed microcontroller. It can be used in other series of microcontrollers as long as the parameter value is modified, and has a wide range of applications.
There are three delay functions as follows:
Function calls are divided into two levels: the first level is a delay of less than 10US, and the second level is a delay of more than 10US.
//====================Less than 10US【use 1US level delay】====================
//----------Microsecond delay---------
for(i=X;i>X;i--) delay time=(3+5*X)/12 Tips (unit: us, X cannot be greater than 255)
//================Greater than 10US and less than 21.9955Ms can use [10US level delay function]===========
void Delay10us(uchar Ms)
{
volatile data i;
for(;Ms>0;Ms--)
for(i=26;i>0;i--);
}
i=[(delay value-1.75)*12/Ms-15]/4
If you want to delay for 60US, then i=[(60-1.75)*12/6-15]/4=25.375≈26; modify the value of i = 26, and then call the above [10US level delay function] Delay10us(6); then the delay will be exactly 60US;
If you want to delay 64us, you can use these two functions in combination: Delay10us(6); for(i=9;i>X;i--) Total delay 64us
Previous article:51 MCU serial communication 1602 display
Next article:Is the 51 microcontroller a von Neumann structure or a Harvard structure?
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
- CY8CKIT-149 PSoC 4100S PLUS PROTOTYPING KIT Unboxing and Power-On Run Example
- B-U585I-IOT02A Bluetooth communication problem
- Super detailed teaching you how to use HFSS to design and simulate inverted F antenna 2
- How does an intelligent fully automatic multimeter measure capacitance?
- PCB current and signal integrity design
- micropython update: 2020.6
- 【DSP】TMS320F28035 SCI routine (self-transmission and self-reception + query)
- The reverse withstand voltage peak of the secondary winding rectifier tube D10 reaches -142V. How can I adjust the RCD parameter value to reduce it?
- C6678 multi-core application cache test
- Simulation AWR Learning Website