top of page

5. GPIO Input and Output Control

GPIO (General Purpose Input/Output) is a fundamental feature in STM32 microcontrollers and is used to interact with external circuits. GPIO pins allow the microcontroller to read or output digital signals. In this section, we will focus on controlling GPIO inputs and outputs on the STM32.

GPIO Input Control: GPIO pins can be used to transfer digital signals from external devices (such as a sensor or a button) to the microcontroller. These pins generate a logic level based on the voltage levels read by the microcontroller. For example, a GPIO pin can generate a low logic level when a button is pressed, which the microcontroller detects and triggers a specific action.

GPIO Output Control: GPIO pins can also be used to send digital signals from the microcontroller to external devices. For instance, a GPIO pin can be configured as an output to control an LED. When the microcontroller sets a specific logic level on this pin, the connected LED will be turned on or off.

GPIO Control with STM32 Libraries: The HAL (Hardware Abstraction Layer) libraries for STM32 microcontrollers include functions for configuring, reading, and writing GPIO pins. These functions make it easier to monitor or change the state of GPIO pins. For example, the HAL_GPIO_ReadPin() function can be used to read the state of a specific GPIO pin, while the HAL_GPIO_WritePin() function can be used to change the state.

GPIO Interrupts: STM32 microcontrollers can react to changes in GPIO pins via interrupts. For example, an interrupt can be triggered when a button is pressed or released. This causes the microcontroller to switch to the interrupt service routine and perform a specific action.

In this way, you can interact with external devices using GPIO pins on STM32 microcontrollers, read inputs, control outputs, and respond to interrupts. This provides a flexible solution for various applications such as reading sensors, controlling buttons, toggling LEDs, and more.


GPIO Pin Configuration:

HAL_GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

This function initializes the pins of the specified GPIO port with a specific configuration. GPIOx specifies the GPIO port (e.g., GPIOA, GPIOB, etc.), while GPIO_InitStruct is a structure containing the configuration of GPIO pins.


GPIO Pin Writing:

HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

This function is used to write a specific state (high or low) to the specified GPIO pin. GPIOx specifies the GPIO port, GPIO_Pin defines the pin number, and PinState specifies the state of the pin (GPIO_PIN_RESET or GPIO_PIN_SET).


GPIO Pin Reading:

GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

This function is used to read the state of the specified GPIO pin. GPIOx specifies the GPIO port, and GPIO_Pin defines the pin number. The function returns the state of the pin (GPIO_PIN_RESET or GPIO_PIN_SET).


GPIO Pin Configuration Structure (GPIO_InitTypeDef):


typedef struct
{
    uint32_t Pin; 		// Pinlerin seçimi, bir veya birden fazla pin için OR işlemi ile belirtilebilir
    uint32_t Mode;          // Pin modu (Giriş, Çıkış, Alternatif, Analog)
    uint32_t Pull;           // Pin çekme direnci (Yukarı, Aşağı, Hiçbiri)
    uint32_t Speed;          // Pin hızı (Düşük, Orta, Yüksek, Çok Yüksek)
    uint32_t Alternate;     // Alternatif pin fonksiyonu (Alternatif modda kullanılacaksa)
} GPIO_InitTypeDef;

This structure defines the configuration of a GPIO pin. The Pin field specifies which pins to configure, Mode determines if the pin is input, output, alternate, or analog, Pull sets the pull-up/pull-down resistor of the pin, Speed defines the speed of the pin, and Alternate specifies which alternate function to select if used in alternate function mode.

These functions and definitions form the basic building blocks for using GPIO pins on STM32 microcontrollers. They can be used to establish data communication between the microcontroller and external circuits.


GPIO Pin State Toggle:

HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

This function toggles the current state of the specified GPIO pin. That is, it makes the pin low if it's high, and high if it's low.


GPIO Interrupt Configuration:

HAL_GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

This function configures the interrupt properties of the specified GPIO pin. The settings in the GPIO_InitStruct structure determine the interrupt properties of the pin.


GPIO Interrupt Service Routine:

HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

This function is a service routine called when an interrupt occurs. Depending on a specific GPIO pin, this function is automatically called when an interrupt occurs.


GPIO Pin Reading (Atomic Read):

GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

This function reads the current state of the specified GPIO pin. This read operation is atomic, meaning it's safe when used in multiple places to avoid interrupt errors.

These functions are common HAL (Hardware Abstraction Layer) functions used to control GPIO pins. They can be used to configure, read, write, and control GPIO pins on STM32 microcontrollers.

 
 
 

Recent Posts

See All

Comments


bottom of page