Using a timer to delay is sometimes a bit troublesome. We might as well consider accurate software delay. Software delay is nothing more than using multiple loops of for or while. In the past, when using delay functions, I downloaded delay subroutines written by others from the Internet. Delay 5ms, 400ms, 1s,..., the function names of these delay functions clearly indicate the delay time, but I have never known how these functions are written. To be precise, how to determine the number of loops based on the delay time. If it is a nanosecond delay, you can observe the waveform through an oscilloscope, or disassemble it and calculate the instruction execution time, but if the delay time is relatively long, the oscilloscope will be powerless. I have taken a good look at Keil debugging these days and found that Keil's functions are really powerful. Using Keil uVersion debugging, you can write accurate software delay programs. The following is my brief summary. All programs in the article are tested under Xtal=11.0592MHZ. ~%k{ \F
M -5g Q*N^
$m. >< !
For example, I need a 400ms delay, so I write a double loop, with the outer loop 5 times and the inner loop set to 5000: 5X vp`Qq
E2|" juE
void Delay400Ms(void){ dy , Os v
P /70 xn_s
uchar i=5; P(CCz 8aJ
`2H(@yN pe
unint j; Xdj,/|(noI
}=v)Gd D ]
while(i--){ o AG ?{HV
k_-lj \
j=5000; //Determine the number of loops through keil debugging & kdiq= U
: kA R !
while(j--); 8,* 7p j
9i[ : N8~
} 9(c&bS Z<
/6fjujivd,
} x=V 4 wh5
Y zcA>
8Call Delay400Ms() in the main function: ^' E(g
FW f7G P#
void main() m lr:](Ge2
pXK,Sc+ sK
{ ^ ?Jz'650
e.*Pn 31x
while(1){ ]9 X/FM>c
G4x 8-L ie
P1=0; 8K*.0[4)Lb
- ( k'EPZ
Delay400ms(); * ;(e
7|A! P >-
P1=1; apT csVx
; |fsk[
} , -2p tT
R}^-/"5Y|
} WMca 'i,]\
KyrG. x gn
enters the debugging state of uVersion, press F10 to single-step, when the yellow arrow points to the statement Delay400ms(), write down the value of Sys->sec in the left window, as shown in the figure, it is 0.00042426. ; i Pv97>
+ &Y n DcW
F fh..d#wE
r ?6V H
1W P7"\|
Press F10 again. After executing Delay400ms(), the sec value becomes 0.38858181. At this time, the initial value 0.00042426 is recorded. The result 0.38815755 means that the execution of Delay400ms() takes 388.15755ms. It can be seen that the delay requirement of 400ms has not been met. At this time, increase the number of inner loops, increase the value of j to 6700, and repeat the above process. The result is 0.40009874, that is, the delay of Delay400ms() program is 400.09874ms, which meets the delay requirement of 400ms. tLC%5 b`*
Ha ; [~goIn
addition to observing the sec value to determine the delay time as mentioned above, the execution time of the Delay400ms() function can also be observed from the performance analysis window of Keil. After entering the debugging state, use the menu View->Performance Analyzer Window to open the performance analysis dialog box. After entering the dialog box, there is only one item unspecified. Right-click the mouse and select Setup PA in the shortcut menu to open the performance analysis setup dialog box. For C language programs, the list box under "Function Symbol" on the right side of the dialog box gives the function symbol. Double-click a symbol and the symbol will appear in the edit box under Define Performance Analyzer. Each time you enter a symbol name, click the Define button to add the function to the analysis list box above it. For assembly language source programs, the subroutine name will not appear in the list box under Function Symbol. You can directly enter the subroutine name in the edit box, click Close to close the window, and return to the performance analysis window. At this time, the window has a total of 4 options. Execute the program at full speed, and you can see a blue indicator bar after Delay400Ms. With the ruler above, you can intuitively see the proportion of the function in the entire execution time. Click the corresponding function name to see more detailed data in the status bar of the window, as shown below: /L-,)u(* O
jY r& ^ 0
FW(pBi1T=|
It is worth noting that using the performance analysis window to observe the execution time of the delay function requires that the observed delay function cannot call any other sub-functions, and the measured function can only consist of basic C statements. Otherwise, the observed time is not the running time of the entire function. OAb:Tgf y3
] @yA H/
Using the above method, the following delay programs are obtained: ;S- ,O!&:^
*M 2Gc6&
/* Ng E sN
l3 !5KG )
* Delay 400 milliseconds C.>O25L<3h
{ jgvM(?4
*/ ]C 4yX !q
~? '|S=A
void Delay400Ms(void){ >]% 5l M'
:WL,h !]g
uchar i=5; J8 N 2E]
~Tn U# U,
unint j; t ^jb X }
clN m*[}vi
while(i--){ #lD&((zp
+V$)j }mxc
j=" 6699";
Ds2?, {d_
while(j--); P BPL1V&:5
GN {ED't0W
} X= E3X8^=
\
} 6Yip `LU 0
$ 6\ t( =
-}e# s 4Yf
/* BL CZ p
0 tc4v{k=
* Delay 1 second hqbA+7k o|
(| y %0
*/ )neF 0A 5E
>< tx .m;
void delay_1_s() < ) \d! >
NF6`L(Ms
{ xpV = ] K
xv
uchar loop="10"; Ti M^-*b&5
ir, zOBJ,9
unint j; U5 }g ay
5Y |>LJ\
while(loop--){ zal0-#Ld;
`&H\ 6&< d
j="8375"; O~E0"p>Y v
oXb DiKtsV
while(j--); ^W :Iz[P`J
p;xE" nzdC
} I~@>& 5LNV
} Y 8{{U+FT
} uj^ZxNMQ
$' ;?T/, d
K{K2\cPt
/* 33 E l"*,
t O>0?~Xt
* Delay N seconds, parameter s is the number of seconds required to delay LOhd]E?LiV
yJ `NQ
*/ A5 Ov(ANT
PW3 sRcJq
void delay_N_s(uchar s) @ '9Q@CJDJ
e /duO e N
{ t}B ~
u8p
while(s--){ $*fz k$
SB7i| $b#
delay_1_s(); gL$b&U ]j2)
/w} Al2M
} flcDM_ R (
8Yz(av] <
}
Keywords:Keil
Reference address:Using Keil u3 debugging to accurately implement software delay
M -5g Q*N^
$m. >< !
For example, I need a 400ms delay, so I write a double loop, with the outer loop 5 times and the inner loop set to 5000: 5X vp`Qq
E2|" juE
void Delay400Ms(void){ dy , Os v
P /70 xn_s
uchar i=5; P(CCz 8aJ
`2H(@yN pe
unint j; Xdj,/|(noI
}=v)Gd D ]
while(i--){ o AG ?{HV
k_-lj \
j=5000; //Determine the number of loops through keil debugging & kdiq= U
: kA R !
while(j--); 8,* 7p j
9i[ : N8~
} 9(c&bS Z<
/6fjujivd,
} x=V 4 wh5
Y zcA>
8Call Delay400Ms() in the main function: ^' E(g
void main() m lr:](Ge2
pXK,Sc+ sK
{ ^ ?Jz'650
e.*Pn 31x
while(1){ ]9 X/FM>c
G4x 8-L ie
P1=0; 8K*.0[4)Lb
- ( k'EPZ
Delay400ms(); * ;(e
P1=1; apT csVx
; |fsk[
} , -2p tT
R}^-/"5Y|
} WMca 'i,]\
KyrG. x gn
enters the debugging state of uVersion, press F10 to single-step, when the yellow arrow points to the statement Delay400ms(), write down the value of Sys->sec in the left window, as shown in the figure, it is 0.00042426. ; i Pv97>
+ &Y n DcW
F fh..d#wE
r ?6V H
1W P7"\|
Press F10 again. After executing Delay400ms(), the sec value becomes 0.38858181. At this time, the initial value 0.00042426 is recorded. The result 0.38815755 means that the execution of Delay400ms() takes 388.15755ms. It can be seen that the delay requirement of 400ms has not been met. At this time, increase the number of inner loops, increase the value of j to 6700, and repeat the above process. The result is 0.40009874, that is, the delay of Delay400ms() program is 400.09874ms, which meets the delay requirement of 400ms. tLC%5 b`*
Ha ; [~goIn
addition to observing the sec value to determine the delay time as mentioned above, the execution time of the Delay400ms() function can also be observed from the performance analysis window of Keil. After entering the debugging state, use the menu View->Performance Analyzer Window to open the performance analysis dialog box. After entering the dialog box, there is only one item unspecified. Right-click the mouse and select Setup PA in the shortcut menu to open the performance analysis setup dialog box. For C language programs, the list box under "Function Symbol" on the right side of the dialog box gives the function symbol. Double-click a symbol and the symbol will appear in the edit box under Define Performance Analyzer. Each time you enter a symbol name, click the Define button to add the function to the analysis list box above it. For assembly language source programs, the subroutine name will not appear in the list box under Function Symbol. You can directly enter the subroutine name in the edit box, click Close to close the window, and return to the performance analysis window. At this time, the window has a total of 4 options. Execute the program at full speed, and you can see a blue indicator bar after Delay400Ms. With the ruler above, you can intuitively see the proportion of the function in the entire execution time. Click the corresponding function name to see more detailed data in the status bar of the window, as shown below: /L-,)u(* O
jY r& ^ 0
FW(pBi1T=|
It is worth noting that using the performance analysis window to observe the execution time of the delay function requires that the observed delay function cannot call any other sub-functions, and the measured function can only consist of basic C statements. Otherwise, the observed time is not the running time of the entire function. OAb:Tgf y3
] @yA H/
Using the above method, the following delay programs are obtained: ;S- ,O!&:^
*M 2Gc6&
/* Ng E sN
l3 !5KG )
* Delay 400 milliseconds C.>O25L<3h
{ jgvM(?4
*/ ]C 4yX !q
~? '|S=A
void Delay400Ms(void){ >]% 5l M'
:WL,h !]g
uchar i=5; J8 N 2E]
~Tn U# U,
unint j; t ^jb X }
clN m*[}vi
while(i--){ #lD&((zp
+V$)j }mxc
j=" 6699";
while(j--); P BPL1V&:5
GN {ED't0W
} X= E3X8^=
\
$ 6\ t( =
-}e# s 4Yf
/* BL CZ p
0 tc4v{k=
* Delay 1 second hqbA+7k o|
(| y %0
*/ )neF 0A 5E
>< tx .m;
void delay_1_s() < ) \d! >
NF6`L(Ms
{ xpV = ] K
xv
ir, zOBJ,9
unint j; U5 }g ay
5Y |>LJ\
while(loop--){ zal0-#Ld;
`&H\ 6&< d
j="8375"; O~E0"p>Y v
oXb DiKtsV
while(j--); ^W :Iz[P`J
p;xE" nzdC
} I~@>& 5LNV
} Y 8{{U+FT
} uj^ZxNMQ
$' ;?T/, d
K{K2\cPt
/* 33 E l"*,
t O>0?~Xt
* Delay N seconds, parameter s is the number of seconds required to delay LOhd]E?LiV
yJ `NQ
*/ A5 Ov(ANT
PW3 sRcJq
void delay_N_s(uchar s) @ '9Q@CJDJ
e /duO e N
{ t}B ~
u8p
SB7i| $b#
delay_1_s(); gL$b&U ]j2)
/w} Al2M
} flcDM_ R (
8Yz(av] <
}
Previous article:89C51 Pin Diagram and Function
Next article:c51 absolute memory access
Recommended ReadingLatest update time:2024-11-16 13:02
Predefined macros added in STM32F10xxx_Keil
Table of Contents update record Use Standard Peripheral Driver update record version status description date author V1.0 C Create Document 2018.10.15 John Wan status: C―― Create, A—— Add, M—— Modify, D—— Delete。 Use Standard Peripheral Driver When using the STM32 firmware library to build a project in the Kei
[Microcontroller]
About 51 precise delay and keil simulation delay time
Sometimes precise delay is required. For example, the 18B20 temperature sensor has very strict timing requirements and must be accurate to the microsecond level.
1. Use NOP function
In keil C51, call the library function directly:
#include // declares void _nop_(void);
_nop_(); // Generate a NOP instruction
[Microcontroller]
proteus7.7+Keil2 simulation 80C51 control water lamp
In many of the blogs about 8051 published this summer, I used the STC89C52RC experiment box to verify the correctness of the 8051 control program. The advantage of doing this is that you can directly interact with the real 8051 microcontroller and master the steps of burning .hex files into the 8051 program memory.
[Microcontroller]
STM32L0 low power design 2: Use Keil and ST-Link to download low power programs
Continuing the development of the project, I suddenly found that the program could not be downloaded to the microcontroller normally, and the error shown in the figure below was prompted. I was using Keil and ST-Link. This problem happened once when I debugged it for the first time. I thought I burned the ch
[Microcontroller]
Keil compilation error about the use of __use_no_semihosting_swi
__use_no_semihosting_swi, that is, do not use semihost mode to prevent the program from entering software interrupts. 1. If file operations such as printf, fopen, and fclose occur during the compilation of an embedded program, the device will enter the software interrupt BAEB when it is running because there is no u
[Microcontroller]
ARM development environment KEIL and IAR set the stack space size
During the ARM development process, everyone will encounter the situation where the stack is not enough or the stack is too large. Here I will introduce how to modify the stack size through the two development environments of IAR and KEIL. Tools/Raw Materials AND KEIL 1、 AGAIN Open the project file and select
[Microcontroller]
keil configuration wizard
1. Introduction When using Keil, many people feel that Keil's Configuration Wizard is magical and very convenient to use, but they don't know how to write their own Configuration Wizard. In fact, Keil's help document has it, but many people feel that English is inconvenient to use, or they don't understand i
[Microcontroller]
Keil C51's printf
To use printf in Keil C51, you first need to reimplement the putchar(char c) function. char putchar (char c)
{
EN=0;
SBUF = c;
while(TI==0);
IF = 0;
EN=1;
return 0;
} Let's analyze the above program first, turn off the serial port interrupt,
[Microcontroller]
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Single-chip microcomputer C language programming and simulation
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- C2000 Code Generation Tools - Compiler
- I want to use an I/O port of the STM32F1 chip to connect an infrared sensor at a distance of 3 meters. I am worried that the transmission distance is too long. How can I achieve this?
- Op amp transfer function solves the problem
- [Repost] This article tells you the difference between Ethernet and broadband
- Passive buzzer driver
- [Reference Solution] Nuvoton Bluetooth Low Energy Microcontroller M031BT for Personal Electronic Massager
- The CC3200 LaunchPad crashed before I even got it warm?
- TMS320F28377S Study Notes 3 Building a fully portable CCS9.3 project
- Recruiting part-time lecturers in motor control related majors
- It is recommended to merge the ATMEL and PIC microcontroller sections