1. GPIO configuration
(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
(5) GPIO_Mode_Out_OD open-drain output
(6) GPIO_Mode_Out_PP push-pull output
(7) GPIO_Mode_AF_OD multiplexed open-drain output
(8) GPIO_Mode_AF_PP multiplexed push-pull output
GPIO_Speed_10MHz Maximum output rate 10MHz
GPIO_Speed_2MHz Maximum output rate 2MHz
GPIO_Speed_50MHz Maximum output rate 50MHz
1.1 In the output mode of the I/O port, there are 3 output speeds to choose from (2MHz, 10MHz and 50MHz). This speed refers to the response speed of the I/O port drive circuit rather than the speed of the output signal. The speed of the output signal is related to the program (the chip has arranged multiple output drive circuits with different response speeds in the output part of the I/O port. Users can choose the appropriate drive circuit according to their needs). By selecting different output drive modules by selecting the speed, the best noise control and power consumption reduction can be achieved. High-frequency drive circuits also have high noise. When a high output frequency is not required, please use a low-frequency drive circuit, which is very helpful to improve the EMI performance of the system. Of course, if you want to output a higher frequency signal, but choose a lower frequency drive module, you may get a distorted output signal.
The key is to match the GPIO pin speed with the application (recommended 10 times or more?). For example:
1.1.1 For the serial port, if the maximum baud rate is only 115.2k, then a 2M GPIO pin speed is enough, which is both power-saving and noise-reducing.
1.1.2 For the I2C interface, if a 400k baud rate is used, if you want to leave a larger margin, then a 2M GPIO pin speed may not be enough, and a 10M GPIO pin speed can be used.
1.1.3 For the SPI interface, if an 18M or 9M baud rate is used, a 10M GPIO pin speed is obviously not enough, and a 50M GPIO pin speed is required.
1.2 When the GPIO port is set as input, the output drive circuit is disconnected from the port, so the output speed configuration is meaningless.
1.3 During reset and just after reset, the multiplexing function is not enabled, and the I/O port is configured as floating input mode.
1.4 All ports have external interrupt capabilities. In order to use the external interrupt line, the port must be configured in input mode.
1.5 The configuration of the GPIO port has a locking function. After the GPIO port is configured, the configuration combination can be locked by the program until the next chip reset.
2. The difference between push-pull output and open-drain output
Push-pull output: can output high or low level, connect digital devices; open-drain output: the output end is equivalent to the collector of the transistor. A pull-up resistor is required to obtain a high level state. It is suitable for current-type driving, and its ability to absorb current is relatively strong (generally within 20ma).
The push-pull structure generally refers to two transistors that are controlled by two complementary signals respectively, and one transistor is always turned on when the other is turned off.
To achieve line-and-connection, an OC (open collector) gate circuit is required. It is two transistors or MOSFETs with the same parameters, which exist in the circuit in a push-pull manner, each 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 switch tubes is turned on at a time, so the conduction loss is small and the efficiency is high. The output can both inject current into the load and extract current from the load.
When the port is configured as output:
Open-drain mode: When outputting 0, N-MOS is turned on, P-MOS is not activated, and output is 0.
When outputting 1, N-MOS is high impedance, P-MOS is not activated, and output is 1 (external pull-up circuit is required); this mode can use the port as a bidirectional IO.
Push-pull mode: When outputting 0, N-MOS is turned on, P-MOS is high impedance, and output is 0.
When outputting 1, N-MOS is high impedance, P-MOS is turned on, and output is 1 (external pull-up circuit is not required).
Simply put, when the open drain is 0, it is connected to GND, and when it is 1, it is floating. When the push-pull is 0, it is connected to GND, and when it is 1, it is connected to VCC.
3. Select IO mode in STM32
(1) Floating input_IN_FLOATING - floating input, can be used for KEY identification, RX1
(2) Pull-up input_IPU - IO internal pull-up resistor input
(3) Pull-down input_IPD - IO internal pull-down resistor input
(4) Analog input_AIN - ADC analog input, or power saving under low power consumption
(5) Open drain output_OUT_OD - IO output 0 connected to GND, IO output 1, floating, need external pull-up resistor to achieve high output 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 also be changed to low level or unchanged by the external circuit. Can read IO input level changes to realize C51 IO bidirectional function
(6) Push-pull output _OUT_PP - IO output 0 - connected to GND, IO output 1 - connected to VCC, read input value is unknown
(7) Multiplexed function push-pull output _AF_PP - on-chip external function (I2C SCL, SDA)
(8) Multiplexed function open-drain output _AF_OD - on-chip external function (TX1, MOSI, MISO.SCK.SS)
Example summary:
(1) Use open-drain output _OUT_OD to simulate I2C, connect a pull-up resistor, and it can 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 IO value; use
GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0);
(2) If there is no pull-up resistor, the IO is high level by default; if you need to read the value of IO, you can use
pull-up input_IPU, floating input_IN_FLOATING and open-drain output_OUT_OD;
4. IO low power consumption:
Regarding analog input & low power consumption, according to the STM32 low power consumption AN (AN2629) and its source file, in STOP mode, in order to get the lowest power consumption possible, all IOs (including non-A/D input GPIOs) are set to analog inputs.
5. Procedure
(1) Clock:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC, ENABLE); //Clock enable
(2) IO configuration:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // Pin mode
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, & GPIO_InitStructure);
(3) Output and input:
Output 0: GPIO_ResetBits(GPIOB,GPIO_Pin_0)
Output 1: GPIO_SetBits(GPIOB,GPIO_Pin_0)
Input: GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)
Previous article:STM32 study notes on battery detection filtering algorithm
Next article:Research on STM32 independent watchdog IWDG and window watchdog WWDG
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- How to use WatchDog in RTOS?
- Last day! Double 11 discount of 1,000 yuan for Tektronix oscilloscopes!
- [FM33LG0 series development board evaluation] 02. Basic engineering (LED, KEY, LPUART, LPTIM, SHELL)
- TMS320F28027F for power stage control
- [TI recommended course] #DC/DC switching regulator packaging innovation#
- How to set BGA area ROOM rules in Altium Designer
- How to choose E907 boot mode for Allwinner V853 under Tina
- How to isolate power modules and non-isolated power supplies respectively
- High Voltage Impedance Tuning Quick Guide
- Will 5G really bring about big changes?