In the development of embedded systems based on STM32 microcontrollers, GPIO (General Purpose Input/Output) control is one of the most fundamental and common operations. By using the STM32 HAL library, configuring and controlling GPIO can be done easily. This article will demonstrate how to use the STM32 HAL library for GPIO control and provide a simple example of LED control.
2.1 Selecting the Appropriate Pin
Before controlling GPIO, it is necessary to select the appropriate pins to connect external devices, such as LEDs or buttons. Taking the STM32F4 series microcontroller as an example, here is how to select the pins:
“`c
// Select the GPIO pin to use
#define LED_PIN GPIO_PIN_13
#define LED_GPIO_PORT GPIOC
“`
2.2 GPIO Initialization Configuration
Before using the HAL library to control GPIO, it is necessary to initialize the GPIO. Below is a typical code snippet for GPIO initialization:
“`c
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable GPIO clock
__HAL_RCC_GPIOC_CLK_ENABLE();
// GPIO configuration
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
// Initialize GPIO pin
HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_RESET); // Initialize to low level
“`
In the above code snippet, the clock for GPIOC is enabled first, then pin 13 (corresponding to PC13) is configured as push-pull output mode and initialized to low level.
3.1 Controlling GPIO Output Level
Using the HAL library, controlling the output level of GPIO is very convenient. Below is an example of controlling the LED output high and low:
“`c
// Control LED output high level
HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_SET);
// Delay for a certain time
HAL_Delay(1000);
// Control LED output low level
HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_RESET);
“`
The above code uses the `HAL_GPIO_WritePin` function to control the output level of the LED pin, thus achieving the LED blinking effect.
3.2 Controlling GPIO Input Status
It is possible to obtain the input status of the GPIO pin through the HAL library. Below is an example of reading the button status:
“`c
// Read button status
if(HAL_GPIO_ReadPin(Button_GPIO_PORT, Button_PIN) == GPIO_PIN_SET) {
// Button pressed
} else {
// Button not pressed
}
“`
Through the HAL library, GPIO interrupts can be easily configured, and corresponding interrupt handling functions can be implemented. Below is a simple example of interrupt configuration:
4.1 Initializing Interrupts
“`c
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable GPIO clock
__HAL_RCC_GPIOA_CLK_ENABLE();
// GPIO configuration
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Enable interrupt
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
“`
4.2 Interrupt Handling Function
“`c
void EXTI0_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
// Handle GPIO interrupt
}
“`
Through this article’s introduction, you should have a preliminary understanding of how to use the STM32 HAL library for GPIO control. Properly configuring and controlling GPIO can effectively control and interact with external devices (such as LEDs, buttons, etc.), providing foundational support for embedded system development.
Follow Embedded Learning Station for more fresh topics.
If you have any questions, please leave a message for Teacher Xiaoxi.
Teacher Xiaoxi is waiting for you to chat~