
01
Steps to Learn from Novice to Expert

02
Set a Good Two-Week Introduction Plan

Programming GPIO itself is actually very simple
02
Detailed Explanation of the 8 Working Modes of GPIO



Pull-Up Input/Pull-Down Input/Analog Input
These concepts are easy to understand and can be easily read from the literal meaning.
Multiplexed Open-Drain Output/Multiplexed Push-Pull Output
It can be understood as the configuration situation when the GPIO is used as a second function (i.e., not used as a general IO port).
Summary of Choosing IO Modes in STM32
1. Floating Input GPIO_IN_FLOATING — Floating input, can be used for KEY recognition, RX1;
2. Pull-Up Input GPIO_IPU — IO internal pull-up resistor input;
3. Pull-Down Input GPIO_IPD — IO internal pull-down resistor input;
4. Analog Input GPIO_AIN — Used for ADC analog input, or low power consumption during sleep;
5. Open-Drain Output GPIO_OUT_OD — IO output 0 connects to GND, IO output 1 is floating, requiring an external pull-up resistor to achieve high-level output. When output is 1, the IO state is pulled high by the pull-up resistor, but due to the open-drain output mode, the IO can also be changed to low by external circuits. It can read IO input level changes, achieving dual-directional functionality of C51 IO;
6. Push-Pull Output GPIO_OUT_PP — IO output 0 connects to GND, IO output 1 connects to VCC, reading input value is unknown;
7. Multiplexed Push-Pull Output GPIO_AF_PP — Internal peripheral function (I2C SCL, SDA);
8. Multiplexed Open-Drain Output GPIO_AF_OD — Internal peripheral function (TX1, MOSI, MISO, SCK, SS);
02
STM32 Configuration Example

1. Use open-drain output _OUT_OD with a pull-up resistor for analog I2C, allowing correct output of 0 and 1; when reading values, first use GPIO_SetBits(GPIOB, GPIO_Pin_0); to pull high, then read the IO value using GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0);
2. If there is no pull-up resistor, the IO defaults to high; to read the IO value, you can use pull-up input _IPU, floating input _IN_FLOATING, and open-drain output _OUT_OD;
There are usually 5 ways to use a pin function
Their configuration methods are as follows:
1. As a regular GPIO input: Configure the pin as floating input, weak pull-up input, or weak pull-down input as needed, and do not enable all multiplexing function modules corresponding to this pin.
2. As a regular GPIO output: Configure the pin as push-pull output or open-drain output as needed, and do not enable all multiplexing function modules corresponding to this pin.
3. As a regular analog input: Configure the pin as analog input mode and do not enable all multiplexing function modules corresponding to this pin.
4. As an input for built-in peripherals: Configure the pin as floating input, weak pull-up input, or weak pull-down input as needed, and enable a corresponding multiplexing function module for this pin.
5. As an output for built-in peripherals: Configure the pin as multiplexed push-pull output or multiplexed open-drain output as needed, and enable all multiplexing function modules corresponding to this pin.
Note: If multiple multiplexing function modules correspond to the same pin, only one can be enabled, and the others must remain disabled. For example, to use the USART3 function of pins 47 and 48 of STM32F103VBT6, pin 47 needs to be configured as multiplexed push-pull output or multiplexed open-drain output, and pin 48 needs to be configured as a certain input mode while keeping I2C2 disabled. If pin 47 of STM32F103VBT6 is to be used as TIM2_CH3, TIM2 must be remapped, and then the corresponding pin must be configured according to the multiplexing function.
———— / END / ————