Principle of 20 IO ports of MCU Xiaobaixuebu series

Publisher:MysticalSoulLatest update time:2016-07-12 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
IO port operation is the most basic and important knowledge in MCU practice. This article spends a long time to introduce the principle of IO port. I also consulted a lot of information to ensure that the content is correct, and it took a long time to write. The principle of IO port originally requires a lot of in-depth knowledge, but here I try to simplify it as much as possible to facilitate understanding. This will be of great help in solving various IO port related problems in the future.

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".

 

Principle of 20 IO ports of MCU Xiaobaixuebu series

 

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.

 

Principle of 20 IO ports of MCU Xiaobaixuebu series

 

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.

 

Principle of 20 IO ports of MCU Xiaobaixuebu series

 

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.

 

Principle of 20 IO ports of MCU Xiaobaixuebu series

 

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.

 

Principle of 20 IO ports of MCU Xiaobaixuebu series

 

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.

Keywords:MCU Reference address:Principle of 20 IO ports of MCU Xiaobaixuebu series

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

Two buttons of the microcontroller control a light-emitting diode
//The program is debugged on the hardware // Request; Two buttons of a single-chip microcomputer control a light-emitting diode        // Two buttons of a single-chip microcomputer control a light-emitting diode, programming in C language; Press one button to turn on the light, release the light to turn off. Press th
[Microcontroller]
Design of Multi-channel Optical Power Meter Based on MSP430F2272 Single Chip Microcomputer
In optical communication, it is necessary to accurately measure the intensity of the carrier optical signal in the optical fiber, and the optical power meter is an instrument designed based on this design that can detect the power of the optical fiber signal source. It is an indispensable measuring instrument for the p
[Microcontroller]
Design of Multi-channel Optical Power Meter Based on MSP430F2272 Single Chip Microcomputer
MCU instruction system
The previous section has described several major components of a single-chip microcomputer, which constitute the hardware of the single-chip microcomputer. The so-called hardware is a visible and tangible entity. However, such hardware alone only provides the possibility of realizing calculation and control function
[Microcontroller]
MCU reads 4*4 matrix keyboard
1. What is a matrix keyboard? A matrix keyboard is a keyboard group with a matrix-like layout used in external devices of a single-chip microcomputer. Since more external inputs are required during circuit design, controlling a single button would waste a lot of IO resources, so a matrix keyboard was created. Commonly
[Microcontroller]
MCU reads 4*4 matrix keyboard
1602LCD liquid crystal reverse display program based on 51 single chip microcomputer
/*1602LCD demonstration program*/ /*MCU model: AT89S51*/ /*Crystal: 11.0592MHZ*/ /*Author: http://www.51hei.com */ /*Compilation environment: Keil 7.50A*/ /****Include header file*****/ #include reg51.h #include intrins.h /******************Port definition can be modified accor
[Microcontroller]
The basic structure and working principle of single chip microcomputer
  Working mode of 89C51 microcontroller   The 89C51 microcontroller has four working modes: reset, program execution, low power consumption, and programming and verification.   1. Reset method   (l) Reset Operation Reset is the initialization operation of the microcontroller. Its main function is to initialize PC to O
[Microcontroller]
FAQs about 51 series microcontrollers
1. Question:   Should I choose AT89C51 or 89S51  when learning 51 series microcontrollers ? Answer: 89C51 and 89S51 have the same core. 89S51 has several obvious upgrades over 89C51 as follows:  1. Program memory writing method: The two have different ways of writing programs. 89C51 only supports parallel writing
[Microcontroller]
Overview of the Status of Single Chip Microcomputer in Modern Electronic Systems
Abstract : Modern electronic technology is developing at an increasingly rapid pace, and its basic core technology is embedded computer systems. As one of the embedded systems, single-chip microcomputers, with their obvious typicality, extensiveness, and popularity, occupy an important position in the development of m
[Analog Electronics]
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号