There are four commonly used delay methods in C language, as shown in Figure 4-2.
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
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
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-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 Adjusting the window position
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
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
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
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.
Previous article:The use of decoupling capacitors in microcontrollers
Next article:Function and specification of MCU programming files
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Design scheme for realizing communication between upper and lower computers via USB
- Which parameters in the datasheet of the five devices, diode, triode, MOS tube, op amp, and optocoupler, can reflect...
- I want a Zircon A4 FPGA board (without downloader). If you have one, please contact me.
- Is inductance or magnetic beads used to isolate the digital power supply from the analog power supply?
- Introduction to RISC-V Toolchain
- Crazy Shell AI open source drone GPIO (LED flight status light, signal light control)
- Analog Dialogue Volume 55 Issue 3 is now online! Come and get your source of inspiration~
- 51 delay problem
- Get a gift for your evaluation! Hands-on experience: Recruiting TI millimeter-wave radar testers from all over the Internet
- How does Allegro's built-in package library identify which type of device it belongs to?