Using Keil u3 debugging to accurately implement software delay

Publisher:BlissfulJourneyLatest update time:2016-10-21 Source: eefocusKeywords:Keil Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

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]
About 51 precise delay and keil simulation delay time
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]
proteus7.7+Keil2 simulation 80C51 control water lamp
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]
ARM development environment KEIL and IAR set the stack space size
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]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号