Eight working modes of GPIO in STM32

Every time I do a project, I need to choose a different GPIO mode for the circuit diagram given by the hardware engineer. The following information is recorded for future reference.

There are 8 types of 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 multiplexing push-pull output

A total of 4 inputs, 2 outputs, and 2 multiplexed outputs. The three most commonly used are push-pull output, open-drain output, and pull-up input.

The internal basic structure of I/O is as follows


1. Floating input mode:


The red line in the figure represents the data transmission channel. The output circuit in the lower half is in an inactive state. The external level signal enters the STM32 through the I/O port number 1 on the right, and passes through the Schmitt trigger of number 2. After the device enters the input data register, at number 4, the CPU can be alone at any time with the level change state of the I/O port through the internal data bus.

2. Pull-up input mode, pull-down input mode


Compared with the previous floating input, the pull-up input is only connected to a pull-up resistor on the data channel. According to the data sheet, the value of this pull-up resistor is between 30K and 50K. Likewise, the CPU can read out the port level changes through the internal data bus at the other end of the input data register at any time. The pull-down input is connected to the pull-down resistor, and the structure diagram is not listed. The function is the same. The pull-up resistor is to ensure that the port input signal is high when there is no signal input. The pull-down resistor is to ensure that the port input signal is low level when there is no signal input.

3. Analog input


When configured as analog input mode, the signal enters from the right port, and directly enters the AD module of STM32 from No. 2. At this time, the Schmitt trigger is in the off state, and the input data register cannot reflect the level state on the I/O port. It cannot directly read the port level status.

4. Open-drain output, push-pull output


Open-drain output: When the CPU writes data through the "bit set/clear register" or "output data register" at the No. 1 end, the data bit will be transmitted to the No. 4 I/O port through the No. 2 output control circuit. If what the CPU writes is logic "1", the N-MOS tube number 3 will be in a closed state, and the level of the I/O port will be determined by an external pull-up resistor. If the CPU writes a logic "0", the N-MOS tube number 3 will be in an on state, and the level of the I/O port is pulled to the zero potential of "ground" by the N-MOS tube number 3.

In the upper part of the figure, the Schmitt trigger is turned on, which means that the CPU can monitor the status of the I/O port at any time at the other end of the "input data register" . Virtual I/O port bidirectional communication: if the CPU outputs a logic "1". Since the N-MOS tube number 3 is in the off state, the level of the I/O port will be completely determined by the external circuit. Therefore, the CPU can read the signal of the external circuit in the "input data register" instead of the logic output by itself. "1 ".

GPIO口的输出模式下,有3 种输出速度可选(2MHz 、10MHz和50MHz) ,这个速度是指GPIO口驱动电路的响应速度,而不是输出信号的速度,输出信号的速度与程序有关(芯片内部在I/O口的输出部分安排了多个响应速度不同的输出驱动电路,用户可以根据自己的需要选择合适的驱动电路)。通过选择速度来选择不同的输出驱动模块,达到最佳的噪声控制和降低功耗的目的。高频的驱动电路,噪声很高  。当我们的项目不需要比较高的输出频率时,请选用低频驱动电路,这样非常有利于提高系统的EMI 性能。当然如果我们的项目要求输出较高频率的信号,但却选用了较低频率的驱动模块,很可能会得到比较失真的输出信号。

推挽输出:GPIO的推挽输出模式是在开漏输出模式的基础上,在“输出控制电路”之后,增加了一个P-MOS管。当CPU输出逻辑“1 ”时,编号3 处的P-MOS管导通,而下方的N-MOS管截止,达到输出高电平的目的;当CPU输出逻辑“0 ”时,编号3 处的P-MOS管截止,而下方的N-MOS管导通,达到输出低电平的目的。在这个模式下,CPU 仍然可以从“输入数据寄存器”读到该IO端口电压变化的信号。

所以两者之间的区别就在于图中黑色框的区域部分。

5、推挽复用输出、开漏复用输出


GPIO的开漏复用输出模式与开漏输出模式的工作原理基本相同,不同的是编号为2 的输入的源不同,它是和复用功能的输出端相连,此时的“输出数据寄存器”被输出通道给断开了。从上面的这个图,我们还可以看到CPU同样可以从“输入数据寄存器”读取到外部IO端口变化的电平信号。

编号2“输出控制电路” 输入是与复用功能的输出端相连,片上外设的输出信号直接与“输出控制电路”的输入端连接。我们将GPIO配置成复用输出功能后,假如相应的外设模块没有被激活,那么此时IO端口的输出将不确定。其它部分原理与前面叙述的模式一样,包括两者之间的区别也是P-MOS。

在STM32中常用IO模式
(1) 浮空输入_IN_FLOATING ——浮空输入,可以做KEY识别,RX1
(2)带上拉输入_IPU——IO内部上拉电阻输入
(3)带下拉输入_IPD—— IO内部下拉电阻输入
(4) 模拟输入_AIN ——应用ADC模拟输入,或者低功耗下省电
(5)开漏输出_OUT_OD ——IO输出0接GND,IO输出1,悬空,需要外接上拉电阻,才能实现输出高电平。当输出为1时,IO口的状态由上拉电阻拉高电平,但由于是开漏输出模式,这样IO口也就可以由外部电路改变为低电平或不变。可以读IO输入电平变化,实现C51的IO双向功能
(6)推挽输出_OUT_PP ——IO输出0-接GND, IO输出1 -接VCC,读输入值是未知的
(7)复用功能的推挽输出_AF_PP ——片内外设功能(TX1,I2C的SCL,SDA)
(8)复用功能的开漏输出_AF_OD——片内外设功能(MOSI,MISO.SCK.SS)

通常有5种方式使用某个引脚功能,它们的配置方式如下:
1)作为普通GPIO输入:根据需要配置该引脚为浮空输入带弱上拉输入带弱下拉输入,同时不要使能该引脚对应的所有复用功能模块。
2)作为普通GPIO输出:根据需要配置该引脚为推挽输出开漏输出,同时不要使能该引脚对应的所有复用功能模块。
3)作为普通模拟输入:配置该引脚为模拟输入模式,同时不要使能该引脚对应的所有复用功能模块。
4)作为内置外设的输入:根据需要配置该引脚为浮空输入带弱上拉输入带弱下拉输入,同时使能该引脚对应的某个复用功能模块。
5)作为内置外设的输出:根据需要配置该引脚为复用推挽输出复用开漏输出,同时使能该引脚对应的所有复用功能模块。
如果有多个复用功能模块对应同一个引脚,只能使能其中之一,其它模块保持非使能状态。








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325601021&siteId=291194637