I think among the many concepts about MCU that make you dizzy, confused, or even want to hit the wall, "stack" may be the most hateful one, because even if you understand this word from the perspective of Chinese, it will make you dizzy. In fact, I couldn't figure out who came up with this idea at first, but don't worry, we won't discuss the problem of this word here (this word is actually very well used. "Heap" and "stack" have their own meanings, which accurately summarize the functions of this area. If you are interested, you can Baidu it). Here I will make a more interesting analogy to bypass those concepts that make you want to hit the wall, and give you a deep understanding of the concept of "stack" intuitively.
You should basically know that there are storage areas and CPUs in MCUs. If you don't know, then I just told you, please remember. Now, please think of the CPU in the single chip as a person (you can think of him as the classmate in your dormitory who quarrels with you every day, you will find it very interesting in a while), here we call him Brother C, but this person is different from ordinary people, and has some characteristics, which we will slowly explain in a while. Now I want to tell you about the first characteristic of this person: his memory is very poor. Next, please imagine the storage area as small boxes arranged one by one. The functions of these boxes can be roughly divided into two categories: 1. Save the notes with your commands, such as the note in a box that says: Go wash my socks! ; 2. Save some of your things, such as your pair of smelly socks that are polluting the air in the dormitory. Because Brother C is a person with a poor memory, these boxes have their own numbers to facilitate his search.
So, now, we can explain how the microcontroller works. First, you need to put all the commands and things that need to be processed into those small boxes, such as the pair of socks to be washed and the note mentioned earlier. At this time, you should find another characteristic of Brother C: stupid - he will only do what you clearly tell him to do. In other words, if you don't write "go wash my socks!" on the note, Brother C is very likely to look at your socks indifferently until he faints from the fumes. Of course, it is more likely that he can't find your socks at all... Well, after you write down what you want to do and how to do it in the box, the following task is handed over to Brother C. Brother C is really principled in doing things. He will open the boxes one by one in the order you give or - if you don't give it, according to the number on the box in order from small to large, read the commands inside, and process the corresponding events until everything is executed, and he will rest. Please remember this simple and interesting process, because in fact, the microcontroller works like this. Of course, many details are ignored here, but this is enough for you to intuitively understand the concept of microcontrollers.
Next, I will start to explain the concept of stack. After thinking about it, I still feel that if I use the word "stack" directly in the text, it is really not in line with the style of this article. Considering that the "stack" is actually a storage area (you have to remember that the stack is not a special area like a dedicated register, it is specified by you in the general RAM area.), according to this article, it is a box. So, now we change the name of "stack" to "memory box". You can feel that the role of "stack" is closely related to memory, but you don't have to worry about the origin of this name here. I will explain it below.
Now, please pay attention, I will start to explain the "memory box", that is, the "stack". Generally speaking, the role of the "memory box" is to save the current task (including the box number and the contents of the box) when Brother C is halfway through executing a task and suddenly has something more urgent to execute. You must be confused by this. Actually, to put it simply, when Brother C was halfway through washing socks, he suddenly received your order to open another box (the note in that box might say "change my diaper") and execute the order inside. Because Brother C's memory is very poor, he can't remember which box to return to after completing that urgent task to continue executing the order of "washing socks". At this time, he has to save the things on hand in the "memory box". The things to be saved are: 1. The number of the box with the note and socks (note that there are actually two items here); 2. The pair of socks. In this way, when he completes the urgent task, he will go to the memory box and find two notes and a pair of socks from it (at this time, Brother C still hasn't remembered that he wants to wash socks, he must go to the note with the order to wash socks). He knows which box he should go to to wash socks according to the information on the two notes, and continues to complete the task of washing socks there. You may find that there is an important loophole in this explanation. That is, how did Brother C know which "memory box" the box number storing the original mission information was stored in after he completed the emergency mission? Don't worry, I will explain it below.
In essence, the "memory box" is no different from an ordinary box. They are all storage units in the microcontroller. The best evidence to prove this is that the stack needs to be specified by you, that is, you have to specify some boxes as "memory boxes" in advance. Next, I will explain how to specify the "memory box". In fact, this process is very simple. There is an SP pointer (81H) in the dedicated register of the microcontroller. This pointer records the address of the beginning of the stack. To explain it in the words that fit this article, there is a pocket on Brother C's clothes (that is, the SP pointer), and the "magic note" in this pocket records the number of the first "memory box", and the process of specifying a "memory box" is that you write the number of a box on this "magic note" (as the number of the first "memory box"), and this note will automatically add 1 or subtract 1 to the number on the note, so a box in a currently uncertain area has the possibility of becoming a "memory box". Note that the size of the stack cannot be specified, which is why the word "growth" is used to describe the stack.
Now, the concept of the stack has been basically introduced, but I know that you may still be dizzy, even more dizzy than before, because the process just described is separate and logically not sequential. Below, I will explain it in sequence, and I believe you will understand it immediately.
The protagonist is still the silly but hardworking Brother C, who opens the boxes one by one and performs the tasks you specify according to the instructions on the note inside. And you, in order to prevent him from making a fool of himself when performing a complex task, designated a box as a "memory box" and wrote the location of this "memory box" on a "magic note" and put it in Brother C's pocket. Now, Brother C is washing your socks. At this time, he suddenly receives your order to change your diaper. Brother C knows that he is stupid, so he automatically takes out the note in his pocket, finds the first "memory box", and then takes out a blank note, writes the number of the box containing the note "wash my socks" on it and puts it in the "memory box". Then, he puts the "magic note" back into his pocket. When this task is completed, the "magic note" will automatically add 1 to the number written on it, that is, write the number of a new, empty "memory box" on it. After that, he will put the number of the box containing the socks and the socks themselves into different memory boxes according to the process just now (now there are three boxes that have become "memory boxes", the stack has grown, and the red underlined fonts are the contents of these three boxes. Note that there is a sequence). Then, he went to change your diaper...
Now, the diaper has been changed. As expected, Brother C has completely forgotten that he was going to wash your socks. However, he remembered one thing, which was to look at the note in his pocket. So he took out the note in his pocket. Of course, it was the number of a "memory box". He found the first "memory box" according to the number (according to the order of the previous paragraph, it should be the third "memory box"), which should contain a pair of your socks, so he took your socks. However, he still didn't know what to do, so he took out the "magic note" again. At this time, the number on the note had automatically decreased by 1, so he found a new "memory box". The note inside recorded the number of the box where the socks were originally placed, so he put the socks in that box. Well, you can imagine that Brother C still doesn't know what to do with the socks. He patiently took out the "magic note" again. This time, according to the number on it, he found a note with the number of a box written on it. Brother C found the box according to the number and found a note in the box saying "Wash my socks!"... At this point, Brother C returned to his original task - washing socks.
Now, I hope you have understood that the stack is actually a set of storage units you specify. These storage units are designated to store only some special information, such as addresses (to protect breakpoints) or some data (to protect the scene). If you must say that there is something special about this storage area, it is: 1. The contents of these storage units are some related parameters when the CPU is interrupted in the middle of executing a task; 2. The addresses of these storage units are recorded in a place called the stack pointer, which is the note in Brother C's pocket!
Previous article:About timer interrupts 1 and 3 in MCU
Next article:Control Method of 28BYJ48 Stepper Motor
Recommended ReadingLatest update time:2024-11-16 22:24
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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!
- 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
- UV Sterilization Box TI Solution
- 【TI recommended course】#TI millimeter wave radar technology introduction#
- The new version of the Download Center is officially launched!
- Please tell me the process technology and manufacturing nodes of the current mainstream TOF sensors
- Thanks EE, I am very happy to receive the New Year gift
- [ESP32-Audio-Kit Audio Development Board] - 3: Install "esp-a...
- Understand DNS basics in one article
- Application of Dacai UART screen in environmental testing equipment
- EEWORLD University ---- TINA-TI (TM) series courses
- Moto 360 Wireless Charging Base Disassembly