The working principle of GPIO in STM32
The GPIO (General Purpose Input/Output) pins on an STM32 microcontroller are highly flexible and are used to interact with external devices—like LEDs, buttons, sensors, or other ICs. Here's a clear explanation of the working principle of GPIO in STM32:
Basic Working Principle of GPIO in STM32
Each GPIO pin can be configured as:
-
Input (read external signal)
-
Output (drive a signal high or low)
-
Alternate function (used by peripherals like USART, SPI, etc.)
-
Analog (e.g., for ADC inputs)
GPIO Workflow in STM32
1. Clock Enable (RCC)
Before using any GPIO port (A, B, C, ...), you must enable its clock in the RCC (Reset and Clock Control):
2. Pin Mode Configuration
Each pin has a mode set in MODER register:
Modes:
-
00: Input -
01: Output -
10: Alternate Function -
11: Analog
3. Output Type
Set output type in OTYPER:
Types:
-
0: Push-pull (normal output) -
1: Open-drain (needs pull-up resistor)
4. Speed Configuration
In OSPEEDR, select output speed (low, medium, high, very high):
5. Pull-up / Pull-down Configuration
Set pull-up/down resistors in PUPDR:
6. Write to Output Pin
Set/reset pin using ODR or BSRR:
7. Read from Input Pin
Read input using the IDR register:
Example: Toggle LED on PA5
Summary Table
| Feature | Register |
|---|---|
| Mode selection | MODER |
| Output type | OTYPER |
| Output speed | OSPEEDR |
| Pull-up/down | PUPDR |
| Read input | IDR |
| Write output | ODR, BSRR |

评论
发表评论