GPIO working mode

1. Classification

1. Floating: As the name suggests, it is floating in the air. The upper side is pulled up by a rope, and the lower side is pulled by a rope and sinks. 
2. Open drain: it is equivalent to an NPN transistor connected to the output port, and only e is connected , b. The c pole is open, you can connect a resistor to 3.3V, or you can connect a resistor to 5V, so that when outputting 1, it can be 5V or 3.3V. But not When the resistor is connected to the pull-up, the output is high. 
3. Push-pull: There is push and pull, the level of the IO port is determined at any time, and no external pull-up or pull-down resistor is required.

2. Input method

(1) GPIO_Mode_AIN analog input
(2) GPIO_Mode_IN_FLOATING floating input
(3) GPIO_Mode_IPD pull-down input
(4) GPIO_Mode_IPU pull-up input

Three, output method

(1) GPIO_Mode_Out_OD open-drain output
(2) GPIO_Mode_Out_PP push-pull output
(3) GPIO_Mode_AF_OD multiplexed open-drain output
(4) GPIO_Mode_AF_PP multiplexed push-pull output

Four, summary

The push-pull circuit is two transistors or MOSFETs with the same parameters, which exist in the circuit in a push-pull manner, each responsible for the positive and negative half-cycle waveform amplification tasks. When the circuit is working, only one of the two symmetrical power switches is turned on at a time. Therefore, the conduction loss is small and the efficiency is high. The output can either sink current to the load or draw current from the load. The push-pull output stage not only improves the load capacity of the circuit, but also increases the switching speed.

Open-drain output : The output terminal is equivalent to the collector of the triode. To get a high-level state, a pull-up resistor is needed. It is suitable for current-type driving, and its ability to absorb current is relatively strong (generally within 20ma).
Open-drain mode The circuit has the following characteristics:
1. Use the driving capability of the external circuit to reduce the internal driving of the IC. When the internal MOSFET of the IC is turned on, the drive current flows from the external VCC through R pull-up and the MOSFET to GND. Only a low gate drive current is required inside the IC.
2. Generally speaking, open-drain is used to connect devices of different levels to match the level, because when the open-drain pin is not connected to an external pull-up resistor, it can only output low level, if it needs to have an output at the same time The high-level function requires a pull-up resistor. A good advantage is that the transmission level can be changed by changing the voltage of the pull-up power supply. For example, adding a pull-up resistor can provide TTL/CMOS level output and so on. (The resistance of the pull-up resistor determines the speed of the logic level transition. The larger the resistance, the lower the speed, the lower the power consumption, so the choice of load resistance should take into account power consumption and speed.)
3. Provided by OPEN-DRAIN It has a flexible output mode, but it also has its weakness, which is the delay of the rising edge. Because the rising edge charges the load through an external pull-up passive resistor, when the resistance is small, the delay is small, but the power consumption is large; otherwise, the delay is large and the power consumption is small. Therefore, if there is a requirement for delay, it is recommended to use the falling edge output.
4. Multiple open-drain output pins can be connected to one line. With a pull-up resistor, an "AND logic" relationship is formed without adding any devices. This is also the principle by which I2C, SMBus and other buses determine the status of the bus occupation. Supplement: What is "line and"? :
On a node (line), connect a pull-up resistor to the power supply VCC or VDD and the collector C or drain D of n NPN or NMOS transistors, and the emitter E or source S of these transistors are connected to the ground As long as one transistor saturates, this node (line) is pulled to the ground level. Because the base of these transistors inject current (NPN) or the gate adds a high level (NMOS), the transistor will Saturation, so the relationship between these bases or gates to this node (line) is NOR logic. If an inverter is added after this node, it is OR logic.
In fact, it can be simply understood as: When the pins are connected together, an external pull-up resistor is connected. If a pin outputs a logic 0, it is equivalent to grounding, and the parallel circuit is "equivalent to being short-circuited by a wire", so the logic level of the external circuit is 0 , And the result of AND is logic 1 only when both are high level.

Since floating input is generally used for external key input, combined with the input part of the circuit in the figure, I understand that in the floating input state, the level of IO is uncertain, and it is completely determined by the external input. If the pin is left floating In the case of reading the level of the port is uncertain.
Pull-up input/pull-down input/analog input: These concepts are easy to understand and can be easily understood literally.
Multiplexed open-drain output, multiplexed push-pull output: It can be understood as the configuration when the GPIO port is used as the second function (that is, it is not used as a general-purpose IO port).
Finally, summarize the usage:

Choose IO mode in STM32

(1) Floating input _IN_FLOATING —— Floating input, can be used as KEY identification, RX1
(2) With pull-up input _IPU-IO internal pull-up resistor input
(3) With pull-down input _IPD-IO internal pull-down Resistance input
(4) Analog input _AIN —— ADC analog input, or power saving under low power consumption
(5) Open drain output _OUT_OD —— IO output 0 is connected to GND, IO output 1, floating, and an external pull-up resistor is required , Can realize the output high level. When the output is 1, the state of the IO port is pulled high by the pull-up resistor, but because it is an open-drain output mode, the IO port can be changed to a low level or unchanged by an external circuit. Can read the IO input level change to realize the IO bidirectional function of C51
(6) Push-pull output _OUT_PP ——IO output 0-connect to GND, IO output 1-connect to VCC, the read input value is unknown
(7) Multiplexing function Push-pull output _AF_PP-on-chip peripheral functions (SCL, SDA of I2C)
(8) Open-drain output of multiplex functions _AF_OD-on-chip peripheral functions (TX1, MOSI, MISO.SCK.SS)

STM32 setting example:

(1) Use open-drain output _OUT_OD for analog I2C and connect pull-up resistors to correctly output 0 and 1; when reading the value, first GPIO_SetBits(GPIOB, GPIO_Pin_0); pull it high, and then you can read the value of IO; use GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0);
(2) If there is no pull-up resistor, IO is high by default; if you need to read the value of IO, you can use pull-up input _IPU and floating input _IN_FLOATING and open drain output _OUT_OD;

 

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/113812270