<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>STM32 on KnightLi Blog</title>
        <link>https://knightli.com/en/tags/stm32/</link>
        <description>Recent content in STM32 on KnightLi Blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en</language>
        <lastBuildDate>Tue, 18 Mar 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://knightli.com/en/tags/stm32/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>An in-depth explanation of the 8 input and output modes of GPIO of STM32, PY32 and other microcontrollers</title>
        <link>https://knightli.com/en/2025/03/18/stm32-py32-gpio-modes/</link>
        <pubDate>Tue, 18 Mar 2025 00:00:00 +0000</pubDate>
        
        <guid>https://knightli.com/en/2025/03/18/stm32-py32-gpio-modes/</guid>
        <description>&lt;h2 id=&#34;8-input-and-output-modes-of-gpio&#34;&gt;8 input and output modes of GPIO
&lt;/h2&gt;&lt;p&gt;GPIO (General Purpose Input/Output) is one of the commonly used peripherals in STM32 microcontrollers and is used to connect and control external circuits. GPIO pins can be used as input (as a signal input for a sensor) or as an output (a controller driving an external device)
In STM32 microcontrollers, there are eight common input and output (GPIO) modes, namely floating input, pull-up input, pull-down input, analog input, push-pull output, open-drain output, multiplexed push-pull output, and multiplexed open-drain output.&lt;/p&gt;
&lt;h2 id=&#34;input-mode&#34;&gt;Input mode
&lt;/h2&gt;&lt;h3 id=&#34;input-floating-gpio_mode_in_floating&#34;&gt;Input floating (GPIO_Mode_IN_FLOATING)
&lt;/h3&gt;&lt;p&gt;Input floating: Floating means that the logic device and pin are not connected to high level or low level. Due to the internal structure of the logic device, when its input pin is left floating, it is equivalent to the pin being connected to a high level. In general actual use, it is not recommended to leave the pin floating as it is susceptible to interference. In layman&amp;rsquo;s terms, floating means floating in the air, which means that this port is connected to nothing by default and is in a high-impedance state. This setting is often used during data transmission. The biggest characteristic of floating is the uncertainty of voltage. It may be 0V, it may be VCC, or it may be a value between the two (most likely). Floating is generally used for ADC input, which can reduce the impact of pull-up and pull-down resistors on the results.&lt;/p&gt;
&lt;p&gt;Features: Pin is in high impedance state, not connected to external circuit, measures external signal level.&lt;br&gt;
Application scenario: the state of receiving external signals, such as key input, sensor input, etc.
&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/1-1.webp&#34;
	width=&#34;3668&#34;
	height=&#34;2004&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/1-1_hu_f18ddbd2828c91d6.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/1-1_hu_62dfbe61888e86c9.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;183&#34;
		data-flex-basis=&#34;439px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化浮空输入模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;          // 浮空输入模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pull = GPIO_NOPULL;              // 不设置上拉或下拉
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;input-pull-up-gpio_mode_ipu&#34;&gt;Input pull-up (GPIO_Mode_IPU)
&lt;/h3&gt;&lt;p&gt;Enter pull-up mode: Pull-up means pulling the point high, such as to Vcc. Pull-up is to clamp an uncertain signal at a high level through a resistor. The resistor also acts as a current limiter. The difference between weak and strong is just the resistance value of the pull-up resistor, there is no strict distinction.
Features: With internal pull-up resistor, the default level of the pin is high level.&lt;br&gt;
Application scenario: Detect when the external signal is low level, such as a button press.&lt;br&gt;
&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/2-1.webp&#34;
	width=&#34;3592&#34;
	height=&#34;1972&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/2-1_hu_f8a5265721434e3.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/2-1_hu_677a45cdeffd1251.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;182&#34;
		data-flex-basis=&#34;437px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化上拉输入模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;          // 上拉输入模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pull = GPIO_PULLUP;              // 上拉输入
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;input-drop-down-gpio_mode_ipd&#34;&gt;Input drop-down (GPIO_Mode_IPD)
&lt;/h3&gt;&lt;p&gt;Input pull-down: means to pull the voltage low to GND. Similar to the pull-up principle
Features: With internal pull-down resistor, the default level of the pin is low level.&lt;br&gt;
Application scenario: Detect when the external signal is high level, such as when the button is lifted.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/3-1.webp&#34;
	width=&#34;3596&#34;
	height=&#34;1976&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/3-1_hu_6d3dd93703b23bf8.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/3-1_hu_f18257eb84884c7d.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;181&#34;
		data-flex-basis=&#34;436px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化下拉输入模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;          // 下拉输入模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pull = GPIO_PULLDOWN;            // 下拉输入
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;analog-input-gpio_mode_ain&#34;&gt;Analog input (GPIO_Mode_AIN)
&lt;/h3&gt;&lt;p&gt;Analog input: Analog input refers to the traditional input. Digital input is the input of PCM digital signal, that is, a binary digital signal of 0,1. It is converted into an analog signal through digital-to-analog conversion, and then enters the power amplifier through pre-amplification. The power amplifier is still analog.&lt;br&gt;
Features: Used to receive continuously changing analog signals, usually used in conjunction with ADC (analog-to-digital converter).&lt;br&gt;
Application scenario: Measure changes in analog signals such as sensor signals and audio inputs.&lt;br&gt;
&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/4-1.webp&#34;
	width=&#34;3440&#34;
	height=&#34;2004&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/4-1_hu_f24693b68952479a.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/4-1_hu_ee5ee1d0d9b4eccd.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;171&#34;
		data-flex-basis=&#34;411px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化模拟输入模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;         // 模拟输入模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pull = GPIO_NOPULL;              // 不设置上拉或下拉
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id=&#34;output-mode&#34;&gt;Output mode
&lt;/h2&gt;&lt;h3 id=&#34;open-drain-output-gpio_mode_out_od&#34;&gt;Open drain output (GPIO_Mode_Out_OD)
&lt;/h3&gt;&lt;p&gt;Open-drain output: The output terminal is equivalent to the collector of the transistor. To obtain a high-level state, a pull-up resistor is required. It is suitable for current-type driving. Its ability to absorb current is relatively strong (generally within 20mA).&lt;/p&gt;
&lt;p&gt;Open-drain circuits have the following characteristics:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Utilize the driving capability of the external circuit to reduce the internal driving of the IC. When the IC&amp;rsquo;s internal MOSFET is turned on, the drive current flows from the external VCC through R pull-up, MOSFET to GND. Only a very high gate drive current is required inside the IC.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Generally speaking, open drain is used to connect devices of different levels and match levels. Because the open drain pin can only output low level when it is not connected to an external pull-up resistor. If it needs to have the function of outputting high level at the same time, it needs to connect 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, etc. (The resistance of the pull-up resistor determines the speed of the logic level conversion edge. The larger the resistance, the lower the speed and the smaller the power consumption. Therefore, the selection of the load resistor must take into account both power consumption and speed.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OPEN-DRAIN 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 to be small, the delay is small, but the power consumption is large; conversely, when the delay is large, the power consumption is small. Therefore, if there are requirements for delay, it is recommended to use falling edge output.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Multiple open-drain output Pins can be connected to one line. Through a pull-up resistor, an &amp;ldquo;AND logic&amp;rdquo; relationship is formed without adding any devices. This is also the principle for buses such as I2C and SMBus to determine the bus occupancy status. Supplement: What is &amp;ldquo;line AND&amp;rdquo;?:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;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. The emitter E or source S of these transistors are all connected to the ground line. As long as one transistor is saturated, this node (line) will be pulled to the ground level. Because the base of these transistors injects current (NPN) or the gate adds a high level (NMOS), the transistor will be saturated. Therefore, the relationship between these bases or gates and this node (line) is NOR logic. If an inverter is added behind this node, it is OR logic.&lt;/p&gt;
&lt;p&gt;In fact, it can be simply understood as: when all the pins are connected together, an external pull-up resistor is connected. If one pin outputs logic 0, it is equivalent to grounding, and the circuit connected in parallel with it is &amp;ldquo;equivalent to being short-circuited by a wire&amp;rdquo;, so the logic level of the external circuit is 0. Only when they are all high, the result of the AND is logic 1.&lt;/p&gt;
&lt;p&gt;Features: It can only output low level and requires an external pull-up resistor to pull the pin high; it has certain driving capabilities.&lt;br&gt;
Application scenario: When connected to external devices, such as I2C bus, used to communicate with other devices.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/5-1.webp&#34;
	width=&#34;3596&#34;
	height=&#34;1976&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/5-1_hu_76ecad8b89653dd0.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/5-1_hu_54ffd4e2322724ec.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;181&#34;
		data-flex-basis=&#34;436px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化开漏输出模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;     // 开漏输出模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;   // GPIO速度设置为高速
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;open-drain-multiplexing-function-gpio_mode_af_od&#34;&gt;Open drain multiplexing function (GPIO_Mode_AF_OD)
&lt;/h3&gt;&lt;p&gt;Open-drain multiplexing function: It can be understood as the configuration situation when the GPIO port is used as a second function (that is, it is not used as a general-purpose IO port). The port must be configured in alternate function output mode (push-pull or open drain)&lt;/p&gt;
&lt;p&gt;Features: It has open-drain output characteristics, which can be used to use GPIO pins as functions of specific peripherals.&lt;br&gt;
Application scenario: Special function pins connected to peripherals, such as I2C bus communication pins, fault signal output, etc.
&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/6-1.webp&#34;
	width=&#34;3424&#34;
	height=&#34;2004&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/6-1_hu_c49c003fa4bd2db2.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/6-1_hu_e73757e3115b8aa8.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;170&#34;
		data-flex-basis=&#34;410px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化复用开漏输出模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;          // 复用开漏输出模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pull = GPIO_NOPULL;              // 不设置上拉或下拉
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;    // GPIO速度设置为高速
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;push-pull-output-gpio_mode_out_pp&#34;&gt;Push-pull output (GPIO_Mode_Out_PP)
&lt;/h3&gt;&lt;p&gt;Push-pull output: It can output high and low levels and connect to digital devices; the push-pull structure generally means that two transistors are controlled by complementary signals, and the other transistor is always turned off when one transistor is turned on. The high and low levels are determined by the IC&amp;rsquo;s power supply.&lt;/p&gt;
&lt;p&gt;The push-pull circuit is two transistors or MOSFETs with the same parameters. They exist in the circuit in push-pull mode. Each is responsible for the waveform task of the positive and negative half cycles. When the circuit is working, only one of the two symmetrical power switch tubes is turned on at a time, so the conduction loss is small and the efficiency is high. The output can sink current into the load. The push-pull output stage not only increases the load capacity of the circuit, but also increases the switching speed.&lt;/p&gt;
&lt;p&gt;Features: It can output high and low levels and has certain driving capabilities.&lt;br&gt;
Application scenario: used to drive external circuits, such as controlling LED lights, driving other logic circuits, etc.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/7-1.webp&#34;
	width=&#34;3596&#34;
	height=&#34;1972&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/7-1_hu_3f13bcce80afb1a5.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/7-1_hu_878c98673d517904.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;182&#34;
		data-flex-basis=&#34;437px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化推挽输出模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitTypeDef GPIO_InitStruct;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;               // GPIO引脚号
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;     // 推挽输出模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;   // GPIO速度设置为高速
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);         // 初始化GPIOA
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;push-pull-multiplexing-function-gpio_mode_af_pp&#34;&gt;Push-pull multiplexing function (GPIO_Mode_AF_PP)
&lt;/h3&gt;&lt;p&gt;Push-pull multiplexing function: It can be understood as the configuration situation when the GPIO port is used as a second function (not used as a general IO port)&lt;/p&gt;
&lt;p&gt;Features: It has push-pull output characteristics and can be used to use GPIO pins as functions of specific peripherals.&lt;br&gt;
Application scenario: Connect to special function pins of peripherals, such as UART serial communication pins, PWM output, etc.
&lt;img src=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/8-1.webp&#34;
	width=&#34;3424&#34;
	height=&#34;2004&#34;
	srcset=&#34;https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/8-1_hu_44a75836fa7fcb2.webp 480w, https://knightli.com/2025/03/18/gpio%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F/8-1_hu_d305901acad19263.webp 1024w&#34;
	loading=&#34;lazy&#34;
	
	
		class=&#34;gallery-image&#34; 
		data-flex-grow=&#34;170&#34;
		data-flex-basis=&#34;410px&#34;
	
&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;// 初始化复用推挽输出模式的GPIO
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pin = GPIO_PIN_0;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;          // 复用推挽输出模式
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Pull = GPIO_NOPULL;              // 不设置上拉或下拉
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;    // GPIO速度设置为高速
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;HAL_GPIO_Init(GPIOA, &amp;amp;GPIO_InitStruct);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        </item>
        
    </channel>
</rss>
