The IO port equivalent model is a method that I created myself. Through this model, the difficulty of understanding the internal structure of the IO port can be effectively reduced. And after consulting the data, it is confirmed that this model is basically consistent with the actual working principle.
==========================================
We have said a lot of things before, and many people may be eager to actually operate the microcontroller. As the main means for the microcontroller to communicate with the outside world, the IO port is the most basic and important knowledge to learn about microcontrollers. Previously, we programmed to realize the experiment of lighting up the LED with the IO port. This article will continue to introduce the knowledge related to the IO port.
In order to better learn IO port operation, it is necessary to understand the internal structure and related concepts of IO port. This knowledge is very helpful for subsequent learning. The key is to understand, and there is no need to remember deliberately. If you don’t remember, just come back and look at it again. You will remember it naturally after using it more.
Official information (most authoritative and accurate)
We have said that the most accurate and effective way to understand a chip is to check the official chip manual and other information. However, for beginners of microcontrollers, it may be difficult to understand directly by reading the chip manual, especially when seeing a bunch of English, unfamiliar circuits, and terminology. If it were me, I would definitely go crazy. But here I still give a picture taken from Atmel's official "Atmel 8051 Microcontrollers Hardware Manual".
The purpose of giving this picture is not to dampen your enthusiasm for learning, but to hope that everyone can understand where the various microcontroller information we have seen came from and whether it is accurate. All of this can be clarified through official information, which will be of some help to everyone in-depth learning in the future.
Second function introduction
The above figure is the official authoritative 51 MCU IO port structure diagram. It can be seen that the internal structures of the four groups of IO ports of the MCU are different. The reason is that some IO ports have a second function, which was mentioned in the Getting Started.
Do you remember this pin diagram? The name of the second function of the IO port is marked in brackets. Except for P1, each interface has a second function. When introducing the microcontroller system module, I mentioned that the 51 microcontroller has a reserved interface for expanding the memory, which is the second function of P0 and P1 in the figure (pins 29, 30, etc. are also used). Because it is not used much and the knowledge involved is relatively in-depth, I will not do a specific study. By the way, in fact, AD0~AD7 we see here is used for parallel ports. The second function of the P3 port includes serial ports, etc., which will be introduced in detail later.
IO port equivalent circuit
Since a deep understanding of the principles of IO ports involves a lot of knowledge related to circuits and even microcomputer principles, only a simplified introduction is given here to meet the needs of most cases. When used as ordinary IO ports, the working principles of the four IO ports are basically the same.
The following diagram is cut out from the P1 circuit in the previous diagram and is the key thing we need to understand.
The P1.X on the right indicates an IO port of P1, such as P1.0. The English on the right of the resistor means internal pull-up resistor, which is called pull-up resistor because one end of the resistor is connected to VCC. The triangle below indicates grounding, which is equivalent to GND. In addition, the most critical component is the one below.
The essence of this device is a transistor, which acts as an electronic switch (if you want to learn more, you can learn about analog circuits, or wait until the principle section for an introduction). The above circuit can be roughly equivalent to the following figure. Note that this structure is only one IO port, and there are 32 such structures in the entire microcontroller.
In the figure, R is a pull-up resistor with a resistance of 10k, and S is an electronic switch equivalent to the transistor in the front. The part in the blue box is inside the microcontroller.
The switch state of S is controlled by the CPU. When the P1.0 pin is set to a low level by the program, the electronic switch S is closed. In fact, when the electronic switch S is closed, there is still a small resistance at both ends. According to the voltage division principle, there will be a very low voltage on P1.0, which is close to 0V, which can be regarded as a low level. When the pin is set to a high level, S is disconnected, and P1.0 is connected to VCC through a 10k pull-up resistor. If measured with a voltmeter, because the internal resistance of the voltmeter is very large, it can be concluded that its voltage value is a high level.
IO port output: light up the LED
The procedure and circuit for lighting the first LED were introduced earlier. The procedure is as follows:
#include
sbit LED = P1^0;
void main()
{
LED = 0;
while(1);
}
The key parts of the circuit are as follows: VCC is connected to the positive pole of the LED through a 1k resistor, and the negative pole of the LED is connected to the P1.0 port:
When the MCU executes LED=0, the electronic switch S is controlled by the CPU to close, and P1.0 outputs a low level. The current flows into P1.0 through the 1k resistor and the LED, and then flows into GND through S. The LED has a suitable voltage at both ends and lights up. Combined with the equivalent circuit of the IO port inside the MCU, the entire circuit is as shown below
Current Sinking and Current Sourcing
In the above example, P1.0 outputs a low level to light up the LED. Can it be reversed, and P1.0 outputs a high level to light up the LED? We can consider connecting the circuit as follows, and write LED=1 in the program.
When LED=1 is executed, S is disconnected. Current flows out of P1.0 through the 10k pull-up resistor R and enters the LED. Since the resistance of the pull-up resistor is too large and the current is too small, the LED does not light up or the brightness is very weak. Therefore, this method is usually not used.
In these two methods, the former is the current flowing from the outside into the microcontroller, which we call the current sinking method; the latter is the current pulling method, which is the opposite direction. By comparison, it can be seen that for the 51 microcontroller, the current sinking method has a larger current, while the current pulling method has a smaller current due to the limitation of the pull-up resistor.
In practice, the maximum current of the current sink is also limited, because the current that can pass through the electronic switch S is limited. According to the official STC chip manual, for STC microcontrollers, it is recommended that the current of a single IO port should not exceed 20mA, and the sum of the currents of all IO ports should not exceed 55mA, otherwise the IO port will be easily burned. The pull current is only about 230uA.
Pull-up resistor/pull-down resistor/high impedance
The pull current flows out from the pull-up resistor. Can the pull current be increased? The answer is yes. We only need to add another pull-up resistor outside the microcontroller to increase the pull current and successfully light up the LED, as shown in the figure below.
The circuit in the figure is equivalent to R and R0 being connected in parallel, and the resistance of the entire pull-up resistor is reduced. However, there is a flaw in doing so. In this circuit, when the microcontroller outputs a low level, S is closed, and the current flows from VCC through the pull-up resistor and S to GND. Although the LED is off at this time, a large current is wasted through the pull-up resistor. Therefore, if the pull-up resistor is too large, it will lead to insufficient driving force, and if the pull-up resistor is too small, it will waste energy when the output is low.
What is the role of the pull-up resistor? People who know more about circuits will soon find that if there is no pull-up resistor, the IO port cannot output a high level, as shown in the figure below. When the switch is closed, it can output a low level, but when the switch is open, P1.0 is suspended and nothing is connected. At this time, the voltage of the IO port is uncertain. This state cannot determine whether it is a low level or a high level, which is called a high-impedance state. Coincidentally, the P0 port of the microcontroller does not have a pull-up resistor, while the other three groups of IO ports have pull-up resistors. So when P0 outputs a high level and there is no external pull-up resistor, it is a high-impedance state and cannot output a high level normally. Later, I will use specific examples to let everyone feel the high-impedance state.
Note: Although P1.0 seems to be connected to the CPU's IO input terminal at the same time, that is, the green arrow with "input" in the figure, this part of the circuit will only be turned on when reading the pin input, and it is unidirectional.
The existence of the pull-up resistor converts the original high impedance state into a high level, hence the name. Corresponding to the pull-up resistor, there is also a pull-down resistor, the difference is that the other end of the pull-down resistor is not connected to VCC but to GND.
IO port input
The reason why the IO port is called IO port (IO=Input/Output) means that it can output and input. The previous part is about the output of the IO port, and the following part is about the input of the IO port. The output of the IO port is introduced through LED, and the input of the IO port is explained through switches. In many single-chip microcomputers, the input and output of IO need to be switched through circuits, but for the 51 single-chip microcomputer, the input and output use the same set of circuits, which is the circuit we analyzed above.
S0 in the figure is a single-pole double-throw switch. Switching it up can connect P1.0 to VCC, and switching it down can connect it to GND. When reading, the CPU will obtain the level on the orange wire in the figure through a specific circuit. What we want to achieve is to let the CPU read the level of the P1.0 port to obtain the state of the switch S0.
When S is disconnected, the CPU can know the state of the external switch S0 by obtaining the level on P1.0 and perform corresponding operations.
When S is closed, S0 switches downward, and P1.0 is indeed low level. When S remains closed and S0 switches upward, VCC is directly connected to GND through S0 and S, which is short-circuited. At this time, a large amount of current passes through the electronic switch S, which may burn out the microcontroller. So we added resistor R0. S still remains closed, and S0 switches upward. At this time, P1.0 is still low level, so the CPU cannot determine the state of the external switch S0, as shown in the figure below.
To sum up, when reading the IO port level, you should first set the output high level (that is, disconnect S) and then read the data. This rule applies to all IO ports.
Similarly, you can also read the state of a single-pole single-throw switch (or push-button switch). Set the output high level before reading. The circuit diagram is as follows.
The above circuit needs to rely on a pull-up resistor to work. Since the P0 port does not have a pull-up resistor, a pull-up resistor needs to be added externally (because if there is no pull-up resistor and both S and S0 are disconnected, the IO port becomes a high-impedance state, and the read level result is uncertain, so the switch state of S0 cannot be correctly judged).
Bidirectional IO port/quasi-bidirectional IO port
The standard bidirectional IO port has two characteristics:
1. In output mode, high and low levels can be output;
2. In input mode, if there is no external circuit connected, it should be in high impedance state.
For the P1, P2, and P3 ports of the 51 single-chip microcomputer, since there are internal pull-up resistors, it is impossible for the high-impedance state to appear in the input mode, so they are called quasi-bidirectional IO ports. When the P0 port works as an IO port, it cannot output a high level without a pull-up resistor; and with a pull-up resistor, it will not appear in the high-impedance state during input, so it is also a quasi-bidirectional IO port.
Note 1: If the P0 port of the 51 single-chip microcomputer works in the second function state, it is a bidirectional IO port. The specific principle does not need to be studied thoroughly when you are a beginner. The following paragraph analyzes this for reference only by interested readers.
According to the official complete IO port structure diagram, there are two upper and lower transistors inside the P0 port. When the P0 port works in the IO port mode, the upper transistor is disconnected and can be directly ignored. The previous equivalent circuit does not consider the upper transistor. When the P0 port works in the second functional state, both transistors can work. If the upper transistor is disconnected and the lower one is turned on, a low level is output; conversely, if the upper one is turned on and the lower one is disconnected, a high level is output and no pull-up resistor is required; if both transistors are disconnected, they can be used as inputs and are in a high impedance state when there is no external circuit. So it is a bidirectional IO port.
Note 2: There is some controversy about the concepts of bidirectional IO ports and quasi-bidirectional IO ports. The introduction here combines various views on the Internet and is accepted by most people.
Line and logic
What will happen if we connect two MCU IO ports together? Below is our circuit diagram.
When both IO ports are set to output low level, that is, S and S1 are closed, the overall level is low; when one IO port is set to output low level and the other output high level, that is, S or S1 is closed, both IO ports will show low level. Only when P1.0 and P1.1 both output high level, that is, S and S1 are open, will they show high level.
It can be simply stated as follows: when two IO ports are connected together, only when P1.0 and P1.1 are both set to output high level, will the wiring of the two ports present a high level. This is the so-called wired AND logic. Not only two, but if many such IO ports are connected together, only when all IO ports are set to output high level, will the wiring present a high level. Wired AND logic will be used in the matrix keyboard later.
Summarize
Finally, the main points are as follows. There is a lot of content, but in fact, only a few of them are commonly used. Once again, you don't need to remember them deliberately. Once you understand the principles and use them more, you will remember them naturally.
1. Sinking current can pass a larger current than pulling current; sinking current is generally used to light up LEDs
2. The smaller the pull-up resistance, the greater the current output capability, but the more power is consumed when the output is low.
3. Before reading the IO port, set the output high level first
4. P0 is used as output and needs an external pull-up resistor
5. When the key switch is used as input, it is connected between the IO port and GND, and a pull-up resistor is required
6. The four IO ports of the 51 microcontroller are quasi-bidirectional ports in normal IO state.
7.51 MCU IO port complies with line and logic
================================================== =====
Introduction to the "Microcontroller Beginner's Guide" series of tutorials (formerly known as "Microcontroller Getting Started Guide")
This series of tutorials starts with the most basic introductory knowledge and gradually introduces the design of single-chip microcomputer systems. The contents include:
1. Introduction: basic concepts such as single-chip microcomputers, basic knowledge of various electronic designs
2. Ideas: engineering ideas for single-chip microcomputer/computer system design
3. Learning: single-chip microcomputer learning process, methods and techniques, taking 51 single-chip microcomputer as an example, and extending to other single-chip microcomputers
4. Application: follow standard engineering methods to design single-chip microcomputer system examples (the systems planned to be designed include: calculators, electronic watches, password locks, simple mobile phones, and the specific details will be determined based on whether there is time)
5. Principles: starting from analog circuits and digital circuits, gradually introduce the principles of single-chip microcomputer/computer systems, and design a simple CPU by yourself (due to limited personal level, I am not sure about writing this part well, the specific content depends on the situation)
Tutorial Features
It contains a variety of basic knowledge, especially the introduction to the basic concepts of single-chip microcomputers and why single-chip microcomputers are used, which are ignored in many similar books and tutorials. It also contains some in-depth knowledge, including the principle section that introduces the basic principles of single-chip microcomputers, which helps to deeply understand single-chip microcomputers.
This series of tutorials takes the 51 microcontroller as an example. After introducing the basic knowledge of the microcontroller through the 51, I will briefly introduce the 430, especially comparing the advantages and disadvantages between the two, so that everyone can quickly feel the huge advantages of the 430, and learning the 51 just lays a solid foundation for quickly understanding the 430.
In this series of tutorials, the main part that introduces the programming knowledge of various modules of the microcontroller is the learning part, and the learning part is only a part of the whole tutorial. In the learning part, I will go through various methods and techniques, such as how to understand some module functions, how to read timing diagrams, strictly follow engineering ideas for programming, how to debug when errors occur in the program, etc. In the idea part, many important ideas will be generally introduced to prepare for the subsequent learning.
The process of learning microcontrollers involves a large amount of knowledge, and a lot of knowledge is interdependent and highly related.
This series of tutorials has a relatively clear order of knowledge, trying to conform to the human cognitive process as much as possible. However, in actual planning, it is found that no matter how the order of knowledge is adjusted, there are always some knowledge that is interdependent and has a complex relationship. For example, when we start talking about IO ports, we will definitely mention registers, and understanding the word register requires deep background knowledge. However, this background knowledge is also difficult to understand without practice.
Beginners often feel confused and at a loss in such places. Whenever I encounter such knowledge, I will point out to beginners how to deal with it. Should they learn and supplement this knowledge by themselves, or wait until they have finished learning the principles to understand it, and how should they view this term now?
In addition, before studying the entire tutorial, you need to master certain basic knowledge such as C language. For details, please refer to the relevant instructions in the tutorial Part 0 "Preface"
http://www.hainter.com/mcu-primer-0
This series of tutorials strives to be easy to understand, and will not use a bunch of words that novices don't understand to explain another word that they don't understand. However, due to my limited language expression ability, some places may be more complicated or unclear, and I hope you can help point them out.
Previous article:MCU drives PS/2 keyboard
Next article:Microcontroller for Beginners (24) Revision Notes and Learning Ideas
Recommended ReadingLatest update time:2024-11-16 17:44
- 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
- Playing with Zynq Serial 1——Zynq Linux boot process
- 【AT-START-F425 Review】Overclocking Performance of AT32F425
- I can't access GitHub anymore, what should I do? I can't access it at all
- [Mill MYB-YT507 development board trial experience] opencv face detection
- TouchGFX application development based on STM32CubeMX on STM32H7A3 processor - HelloWorld!
- How large a fifo capacity can ep4ce6 achieve?
- Introduction to the causes of TPS79633KTTR voltage instability
- Bicycle modification series: colorful taillights
- Staying at home during the epidemic, reading books
- 【TI recommended course】#Motor control voltage and current sampling solution#