STM32 study notes GPIO configuration issues

Publisher:二进制游侠Latest update time:2017-09-24 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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)


Keywords:STM32 Reference address:STM32 study notes GPIO configuration issues

Previous article:STM32 study notes on battery detection filtering algorithm
Next article:Research on STM32 independent watchdog IWDG and window watchdog WWDG

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号