MCU delay method (Keil software delay)

Publisher:FreeSpirit123Latest update time:2021-06-24 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

There are four commonly used delay methods in C language, as shown in Figure 4-2.

Figure 2-4 C Speech Delay Method
Figure 2-4 C Speech Delay Method


Figure 4-2 shows four common delay methods in C language programming, two of which are inaccurate delays and two more accurate delays. Both for statements and while statements can change the delay time by changing the range value of i, but the execution time of a C language loop cannot be seen through the program. There are two methods for accurate delay. One method is to use a timer to delay, which we will introduce in detail in the following course. The timer is a key point of the microcontroller. Another method is to use the library function _nop_();. The time of a NOP is the time of a machine cycle, which will also be introduced later.


Inaccurate delay is only used in some complex demonstration experiments such as flashing small lights and flowing lights. In the development process of actual projects, this kind of inaccurate delay is rarely used.


Okay, now that the introduction is over, we are going to practice. In the last lesson, the delay method used in the LED light flashing program was for(i=0;i<30000;i++); if you change i here to 100 and download it into the microcontroller, you will find that the light is constantly on instead of flashing. Now please change this program to 100, then download it to observe the phenomenon and continue...


After the observation, there is no doubt that the actual phenomenon is consistent with the theory I mentioned. Why is this? Here is a piece of knowledge. Our naked eyes have a minimum resolution for the flickering light. Under normal circumstances, when the flickering frequency is higher than 50Hz, the signal we see is always on. That is, when the delay time is less than 20ms, our naked eyes cannot distinguish that the small light is flickering. The most we can see is that the light of the small light has changed slightly. In order to clearly see the flickering of the small light, the delay value must be larger. How large should it be? Lights of different brightness are not exactly the same. Everyone can do experiments by themselves.


So how do we check how long the delay we wrote is? Select the Keil menu item Project-->Options for Target Target1..., or click the icon mentioned in Figure 2-17 to enter the project options, as shown in Figure 4-3.

Figure 4-3 Project Options - Clock Frequency Settings
Figure 4-3 Project Options - Clock Frequency Settings


First, open the Target tab and find the Xtal (MHz) position. This is the crystal oscillator option for the simulation time. From our schematic and the board, we can see that the crystal oscillator used by the microcontroller is 11.0592MHz, so we need to fill in 11.0592 here. Then find the Debug tab, select Use Simulator on the left, and then click OK at the bottom, as shown in Figure 4-4.

Figure 4-4 Project Options - Simulation Settings
Figure 4-4 Project Options - Simulation Settings


Select the menu item Debug-->Start/Stop Debug Session, or click the button in the red box in Figure 4-5, and you will enter a new page as shown in Figure 4-6.

Figure 4-5 Start/End Debugging Button
Figure 4-5 Start/End Debugging Button
Figure 4-6 Project debugging interface
Figure 4-6 Project Debugging Interface


The leftmost column shows the current values ​​and fragmentary information of some registers of the microcontroller. The topmost column is the code that Keil converts from C language to assembly language. Below is the program we wrote in C language. The debugging interface includes many sub-windows, which can be opened and closed through the options in the View menu. You may feel that this default distribution is not in line with your habits or inconvenient to view specific information. It is easy to deal with. The positions of almost all sub-windows on the interface can be adjusted. For example, if I want to place the Disassembly window and the source code window side by side horizontally, I only need to drag the title bar of the Disassembly window with the mouse. At this time, multiple icons indicating the destination position will appear on the screen. Drag the window and move the mouse to the corresponding icon. The software will also use a blue background to indicate the specific position, as shown in Figure 4-7. Release the mouse and the window will be placed in the new position. The result after adjustment is shown in Figure 4-8.

Figure 4-7 Adjustment window position
Figure 4-7 Adjusting the window position
Figure 4-8 Window position adjustment results
Figure 4-8 Result of adjusting the window position


You may have noticed that there is an arrow with a ××× in the source code file and the disassembly window of the C language. This arrow represents the current running position of the program. Since the code in the disassembly is generated by compiling the source file, they point to the opposite actual position. In this project debugging interface, we can see the process of program running. There are three buttons in the toolbar in the upper left corner: the first one marked with the word RST is reset. After clicking it, the program will run to the very beginning; the button next to the right is full speed run. Click it and the program will run at full speed; the cross on the left is the stop button. When the program runs at full speed, we can stop the program by clicking the third icon to see where the program is running. After clicking reset, you will find that there is gray or green on the left side of the C language program, and some places still keep the original white. We can double-click the mouse to set a breakpoint in our gray position. For example, if the program has a total of 20 lines, after setting a breakpoint on the tenth line, click full speed run, and the program will run to the tenth line and stop, which is convenient for us to observe the status of running to this point.


Students will find that breakpoints can be set at some locations, but not at others. Why is this? Because Keil software itself has the function of program optimization. If you want to set breakpoints at all code locations, you can set the optimization level to 0 in the project options, which means telling Keil not to stop optimization. As shown in Figure 4-9.

Figure 4-9 Engineering Optimization Grade
Figure 4-9 Engineering Optimization Grade


In this lesson, we will focus on the running time of C language code. In the register box on the far left, there is a sec option, which shows how long the microcontroller has been running. Click the reset button and you will find that sec becomes 0. Then we add a breakpoint at LED = 0; and another breakpoint at LED = 1;. We click the full speed run button and it will stay directly at LED = 0;. We will see that our time changes to 0.00042752 seconds, as shown in Figure 4-10. Please note that the optimization level we set here is the default 8. If you use other levels, the running time will be different, because the optimization level will directly affect the execution efficiency of the program.

Figure 4-10 Check program running time
Figure 4-10 Check program running time


Click on "Run at full speed" again, and you will find that sec becomes 0.16342556. Then subtract the previous value, and you will get the time the program takes to execute between the two breakpoints, which is the execution time of this for loop, which is about 163ms. We can also change the delay time by changing the number 30000. Of course, everyone should pay attention to the value range of i. If you write a value greater than 65535, the program will not be able to run continuously, because no matter how i changes, it will not be greater than this value. If you want to exceed this value and run normally, you must change the type of i definition. Later, if we want to check how long a program has been running, we can check it in this way.


In fact, when you enter debug mode, in addition to seeing how long the program has been running, you can also view the value changes of each register and each variable. Click Watch Windows-->Watch 1 in the View menu to open the variable watch window, as shown in Figure 4-11.

Figure 4-11 Variable viewing window
Figure 4-11 Variable viewing window


In this window, you can double-click or press the F2 key, and then enter the name of the variable or register we want to view, and then its value will be displayed. This function is more useful in our later debugging program, so let's understand it first.


Keywords:MCU Reference address:MCU delay method (Keil software delay)

Previous article:The use of decoupling capacitors in microcontrollers
Next article:Function and specification of MCU programming files

Latest Microcontroller Articles
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号