Different types of variables can be used in C programs for delay design. Experimental tests show that using 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.
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--);
}
Computational analysis:
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--);
}[page]
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)
{
uchar 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:Detailed explanation of 51 MCU assembly delay program algorithm
Next article:51 MCU Programming Skills Summary
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- Design and Implementation of VGA Video Acquisition System Based on TMS320DM355
- New ranking of global NOR flash memory market in the first quarter of 2006, Spansion takes the top spot
- FPGA-to-ASIC integration brings flexibility to microcontrollers
- Award-winning live broadcast: Registration for the introduction of ON Semiconductor's photovoltaic and energy storage products is now open~
- CircuitPython 7.0.0 Alpha 4 released
- How to Erase the Contents of EPCS in Altera FPGA Configuration Device
- [Problem Feedback] Anlu TangDynasty ChipWatcher network sorting confusion issue
- Instrument and control system grounding and shielding training materials
- 64 yuan group purchase (5 left): Thumb board NUCLEO-L432KC, limited quantity, come quickly
- CCS uninstallation and installation process and problems encountered