STM32 HAL: Blink LED Example
Embarking on a journey to explore and program STM32 microcontrollers is an exciting venture for electronics enthusiasts. A fundamental operation, often the first step in this journey, is to make an LED blink. This hands-on tutorial will guide you through blinking an LED using STM32's HAL (Hardware Abstraction Layer) library, a crucial tool that simplifies the interaction with hardware components. By the end of this article, you'll have a fully functional LED blinking example, setting a solid foundation for more complex projects.
Before diving into the code, ensure you have the necessary tools and components. You'll need an STM32 development board, an LED (any color works), and a breadboard to connect them. Additionally, you must have installed the STM32CubeMX software and the STM32CubeF4 toolchain, which includes the HAL library for the STM32F4 series microcontrollers.
The Hardware Setup
Connecting your LED to the STM32 board is a straightforward process. For this example, let's use PA5, a digital output port on the STM32F4 Discovery board. This port is connected to a green LED on the board, simplifying breadboarding. If you're using a different board or LED, adjust the port accordingly.
1. Connect one end of your LED's cathode (negative leg) to a GND (ground) pin on your STM32 board.
2. Connect the other end, the anode (positive leg), to the PA5 pin on the board.
Creating a New Project in STM32CubeMX
Launch STM32CubeMX and choose your target MCU (STM32F4xx for our example). Configure the clock settings, avoiding changes unless necessary. In the Project Configurations pane, tick the 'Middlewares' option, then select 'HAL' to include it in your project.
In the Clock Configuration panel:
- Select the SYSCLK source oscillators and values.
- Configure the HSE (high-speed external) oscillator if needed.
- Options for the HSI (high-speed internal) oscillator are usually adequate for starting.
Generating the Code
Once configurations are set, click 'Projects' and select your desired project name and save location. Click 'Generate' to create the project files.
Open the generated project in your preferred IDE (e.g., SEGGER Embedded Studio or Makefile). Notice the main.c file containing a basic main function provided by the generated HAL library.
Blinking an LED with HAL
In this section, you'll utilize HAL to make your LED blink. By the end, you'll have a working LED blinking example using the HAL library.
Including the Necessary Headers and Defining the LED Pin
Add the following header at the beginning of your main.c file.
#include "stm32f4xx_hal.h"
Define the PA5 as an output pin in the following manner:
#define LED_PIN GPIO_PIN_5
#define LED_PORT GPIOA
Initializing GPIO in the 'main' Function
Inside the 'main' function, initialize the HAL by evolving the SystemClock_Config() function. Then, initialize the GPIO with the following code:
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
Blinking the LED
To blink the LED, use a simple while loop indefinitely setting the LED state high then low with a small delay in between:
while (1)
{
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
HAL_Delay(500);
}
Building and Running the Code
Compile the generated code and load the HEX file onto your STM32F4 discovery board. After a successful upload and reset, your LED should be blinking on and off every half a second.
Congratulations! You've successfully blinking an LED using the STM32 HAL library. This foundational exercise sets you up to explore more interactions with peripherals and complexities in STM32 programming. Happy tinkering!