Analysis of GPIO...

  • 2022-09-23 10:13:53

Analysis of GPIO mode of STM32 microcontroller

1. Push-pull output: It can output high and low levels and connect to digital devices; the push-pull structure generally means that two triodes are controlled by two complementary signals respectively, and the other is always turned off when one triode is turned on. The high and low levels are determined by the power supply of the IC.

The push-pull circuit is two transistors or MOSFETs with the same parameters, which exist in the circuit in a push-pull manner, and each is responsible for the waveform amplification task of the positive and negative half cycles. 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 into the load or draw current from the load. The push-pull output stage not only increases the load capacity of the circuit, but also increases the switching speed.


2. Open-drain output: The output terminal is equivalent to the collector of the triode, and a pull-up resistor is required to obtain a high-level state. It is suitable for current-type drive, and its ability to absorb current is relatively strong (generally within 20mA). Open-drain circuits have the following characteristics:

1. Utilize the driving ability of the external circuit to reduce the internal driving of the IC. When the IC's internal MOSFET is turned on, the drive current flows from the external VCC through the pull-up resistor, MOSFET to GND. Only a small gate drive current is required inside the IC.

2. Generally speaking, open-drain is used to connect devices of different levels and to match the level, because when the open-drain pin is not connected to an external pull-up resistor, it can only output a low level. If it needs to have an output at the same time A 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 value of the pull-up resistor determines the speed of logic level conversion. The larger the resistance value, the lower the speed and the lower the power consumption, so the selection of the load resistor should take into account both power consumption and speed.)

3. The open-drain output provides a flexible output method, 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 resistor is selected 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 falling edge output.

4. Multiple open-drain outputs can be connected to one line. Through a pull-up resistor, an "AND logic" relationship is formed without adding any devices, that is, "wired AND". It can be simply understood as: when all pins are connected together, an external pull-up resistor is connected. If there is a pin output as logic 0, it is equivalent to grounding, and the circuit in parallel with it is "equivalent to being short-circuited by a wire". Therefore, the logic level of the external circuit is 0, and the result of AND is logic 1 only when both are high.

Regarding the push-pull output and the open-drain output, we can finally summarize it with the simplest picture: the left side of the picture is the push-pull output mode, in which the lower PNP transistor is turned off when the comparator outputs a high level, and the upper NPN transistor conducts On, the output level is VS+; when the comparator outputs a low level, it is just the opposite, the PNP transistor is turned on, the output is connected to the ground, and it is a low level. The one on the right can be understood as an open-drain output form and needs to be pulled up.

Analysis of GPIO mode of STM32 microcontroller

3. Floating input: For the floating input, I have not found a very authoritative explanation, so I have to understand it from the following figure

Analysis of GPIO mode of STM32 microcontroller

Since the floating input is generally used for external key input, combined with the input circuit in the figure, I understand that in the floating input state, the level state of the IO is uncertain, which is completely determined by the external input. If the pin is left floating In the case of reading the level of this port is indeterminate.

4. Pull-up input/pull-down input/analog input: These concepts are easy to understand and can be easily read literally.

5. Multiplexing open-drain output and multiplexing 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)

6. Summary Selecting IO mode in STM32

1. Floating input GPIO_IN_FLOATING - floating input, can be used for KEY identification, RX1

2. With pull-up input GPIO_IPU - IO internal pull-up resistor input

3. With pull-down input GPIO_IPD—— IO internal pull-down resistor input

4. Analog input GPIO_AIN - apply ADC analog input, or save power under low power consumption

5. Open-drain output GPIO_OUT_OD - IO output 0 is connected to GND, IO output 1, suspended, and an external pull-up resistor is required to output a 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 IO input level changes, realize the IO bidirectional function of C51

6. Push-pull output GPIO_OUT_PP - IO output 0-connected to GND, IO output 1-connected to VCC, the read input value is unknown

7. Push-pull output of multiplexing function GPIO_AF_PP - on-chip peripheral functions (SCL, SDA of I2C)

8. Open-drain output of multiplexed function GPIO_AF_OD——on-chip peripheral functions (TX1, MOSI, MISO.SCK.SS)

Seven, STM32 setting example:

1. Analog I2C uses open-drain output _OUT_OD, connect pull-up resistor, can output 0 and 1 correctly; when reading the value, first GPIO_SetBits (GPIOB, GPIO_Pin_0); pull high, and then read the value of IO; use GPIO_ReadInputDataBit (GPIOB, GPIO_Pin_0 );

2. If there is no pull-up resistor, IO defaults to high level; 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;

8. There are usually 5 ways to use a certain pin function, and their configuration is as follows:

1. As a common GPIO input: configure the pin as a floating input, a weak pull-up input or a weak pull-down input as required, and do not enable all the multiplexed function modules corresponding to this pin.

2. As a common GPIO output: configure this pin as push-pull output or open-drain output as required, and do not enable all multiplexed function modules corresponding to this pin.

3. As a common analog input: configure this pin as analog input mode, and do not enable all the multiplexed function modules corresponding to this pin.

4. As the input of the built-in peripheral: configure the pin as a floating input, a weak pull-up input or a weak pull-down input as required, and enable a multiplexed function module corresponding to the pin at the same time.

5. As the output of the built-in peripheral: configure the pin as multiplexed push-pull output or multiplexed open-drain output as required, and enable all multiplexed function modules corresponding to this pin at the same time.

Note that if there are multiple multiplexed function modules corresponding to the same pin, only one of them can be enabled, and the other modules remain disabled. For example, to use the USART3 function of pins 47 and 48 of STM32F103VBT6, you need to configure pin 47 as multiplexed push-pull output or multiplexed open-drain output, configure pin 48 as an input mode, and enable USART3 and keep I2C2 inactive. able state. If you want to use pin 47 of STM32F103VBT6 as TIM2_CH3, you need to remap TIM2, and then configure the corresponding pins according to the multiplexing function.