Getting Started with STM32 - Eight Working Modes of GPIO Ports

1. Introduction to GPIO

GPIO (general purpose input output) is the abbreviation of general purpose input and output port. 简单来说就是软件可控制的引脚The GPIO pins of the STM32 chip are connected to external devices to realize the functions of external communication, control and data acquisition.

1. Are all pins GPIO?
The answer is no, not all pins are GPIO

STM32F103VET6 pin diagram insert image description here
The GPIO of the STM32 chip is divided into many groups, each group has 16 pins, such as the model STM32F103VET6 chip has GPIOA, GPIOB, GPIOC to GPIOE, a total of 5 groups of GPIO, such as GPIOA (PA0~PA15 16 pins The chip has a total of 100 pins, of which GPIO accounts for a large part, and all GPIO pins have basic input and output functions.

Among these 100 pins, in addition to GPIO, there are pins with special functions, such as reset pins, power pins... 2. GPIO
insert image description here
input and output modes
GPIO can be configured with 8 modes

  • In the output mode, the output high and low levels of the port can be controlled, which can be used to drive LEDs, control the buzzer, and simulate the output timing of communication protocols (SPI I2C, etc.), etc.
  • In input mode, the high and low level or voltage of the port can be read, used to read key input, external module level signal input, ADC voltage acquisition, analog communication protocol receiving data (SPI I2C, etc.), etc.
    3. GPIO basic structure
    insert image description here
    Each GPIO port corresponds to 16 pins, such as GPIOA (PA0~PA15)
    The kernel cpu can read and write registers through the APB2 bus to complete the functions of output level and read level

2. GPIO functional block diagram

insert image description here

  • protection diode

The pin level of the chip is 0~3.3V, and some pins can be 5V. The two protection diodes of the pin can prevent the external high or low voltage input from
the pin. 1. When the pin voltage is higher than VDD, The diode above is turned on
insert image description here
to prevent excessive voltage from entering the chip and burning the chip

2. When the pin voltage is lower than VSS, the diode below is turned on
insert image description here
to prevent the voltage from being too low and draw current from the chip

  • The Schmitt trigger
    insert image description here
    has the function of filtering, which is to let the passed level output a stable high and low level.
    insert image description here

3. 8 working modes of GPIO

insert image description here

1. Floating, pull-up, pull-down input

insert image description here

In input mode, the Schmitt trigger is turned on, the output is disabled, and the I/O status can be read through the input data register GPIOx_IDR.

Due to the large resistance value of the resistor, the pull-up and pull-down inputs here are weak pull-up and weak pull-down, in order to have a great influence on the external input

  • Pull-up input: give a default high level that means the default input high level when there is no external input
    insert image description here

  • Pull-down input: give a default low level that means the default input low level when there is no external input
    insert image description here

  • Floating input: If the input pin is not connected to anything, the input level is very susceptible to external interference, resulting in an uncertain input level, which is completely determined by the external input.
    insert image description here

2. Analog input

This mode is mainly configured for the on-chip peripheral ADC, which reads the analog signal from the outside

  • Analog signal: Before the test signal is sampled, the signal whose time and amplitude are both continuous is called an analog signal, such as continuously changing voltage, current, temperature and so on.

  • Digital signal: After the analog signal is "sampled" at equal intervals and the amplitude is quantized, the time and amplitude are discontinuous (discrete) signals. For example

    , 0/1 does not need Schmitt filtering here. changing analog

3. Push-pull and open-drain output

Schmitt trigger input is activated
Weak pull-up and pull-down resistors are disabled
The data presented on the I/O pin is sampled into the input data register on every APB2 clock
In open-drain mode, the input data register Read access to get the I/O status
In push-pull mode, read access to the output data register to get the last written value

In addition to the analog input mode, the digital input function will be turned off in the other seven modes, and the I/O status can be read through the input register. For example, in the analog I2C experiment, when the GPIO working mode is configured as open-drain output, Read the pin level state, it doesn't matter now, I will explain it in detail later

In the output mode, in the push-pull mode, the dual MOS transistors work in turn, and the output data register GPIOx_ODR can control the high and low levels of the I/O output. In open-drain mode, only the N-MOS tube works, and the output data register can control the I/O to output a high-impedance state or a low level.

1. Push-pull outputinsert image description here

  • When the output register outputs a high level, the pin also outputs a high level

insert image description here

  • When the output register outputs a low level, the pin also outputs a low level 2. Open-drain output
    insert image description here

  • When the output register outputs a high level, the pin outputs a high-impedance state
    insert image description here

  • When the output register outputs a high level, the pin outputs a high-impedance state

insert image description here

4. Multiplexing function push-pull open-drain output

In the multiplexed function mode, the output is enabled, the output speed can be configured, and it can work in open-drain and push-pull modes, but the output signal originates from other peripheral output data registers GPIOx_ODR is invalid; input is available, and I/O can be obtained through the input data register O actual state, but generally use the peripheral register to get the data signal directly

Here is an example of the sending TX and receiving RX pins of the serial port

The sending pin of the serial port TX insert image description here
The receiving pin of the serial port RX
insert image description here
It doesn't matter what the serial port peripherals are. It will be discussed in detail in the future as long as it refers to the push-pull and open-drain outputs of the multiplexing function and the ordinary push-pull and open-drain outputs are just outputs The registers of the peripherals are replaced by the registers of the peripherals

4. GPIO registers

By writing different parameters to the GPIO register, the working mode of the GPIO can be changed. To understand the specific register, be sure to refer to the corresponding peripheral register description in the "STM32F10X-Chinese Reference Manual".

Port Configuration Low Register

insert image description here
In GPIO peripherals, the control port high and low control registers CRH and CRL can configure the working mode and speed of each GPIO, each 4 bits control an IO, CRH controls the upper eight bits of the port, and CRL controls the lower 8 bits of the port , see the register description of CRH and CRL for details

Port Configuration Low Register

insert image description here

port output register

insert image description here

Port Bit Set/Clear Register

insert image description here

Port Bit Clear Register

insert image description here

Port Bit Clear Register

insert image description here

Summarize

Everyone must understand the principles of the eight GPIO modes, and lay a solid foundation for later learning. This article is here. If there are still friends who don’t know about registers, please see
What are registers ? This article will help you. Just like and favorite! ! !

Guess you like

Origin blog.csdn.net/k666499436/article/details/123845466