Stm32 Led Blink Example Code
When it comes to low-power microcontroller boards, the STM32 is a popular choice among hobbyists and professionals alike. One of the most basic and essential functions of an STM32 development board is controlling LEDs. Blinking an LED is often one of the first tasks new users strive to perform, serving as a practical introduction to programming and hardware interaction. This article walks you through a comprehensive guide to blinking an LED using the STM32, providing examples and detailed explanations.
Before diving into the code, ensure you have the necessary hardware: an STM32 development board (like the STM32F103C8T or the Blue Pill), an LED, and some jumper wires. Connect your LED to a digital output pin on the STM32 board, for instance, GPIO_13 (Pin PA.15).
Setting Up the Development Environment
To start programming the STM32, you first need to set up a development environment.ST-LINK/V2 debugger/programmer, or use a third-party tool like OpenOCD, J-Link, or the native ST-LINK interface on your STM32F4x9/STM32F4x5 series.
Install the appropriate toolchain (GCC for ARM) and use an integrated development environment (IDE) like TrueSTUDIO, IAR Embedded Workbench, or Atollic TrueSTUDIO. For this guide, we'll assume you're using the free академической версии of TrueSTUDIO.
Creating a New Project
Launch TrueSTUDIO and create a new project. Select the appropriate target (e.g., STM32F103x8) and add main.c as the main source file. Ensure the project settings point to the correct toolchain and provide the necessary libraries for your STM32 board.
Double-check your toolchain settings, including the syscall delegate, memory layout, and optimization flags.
Including Necessary Headers
In your main.c file, include the necessary headers for handling GPIO functionality:
```c #include "stm32f10x.h" ```This header file ensures that you have access to the various STM32F103x8 registers and functions, enabling you to interact with the device hardware.
Initializing the LED Pin
The first task is to configure the GPIO pin connected to your LED. GPIO_13 is located on the PORT A, specifically on bit 15 (PA.15).
To initialize this pin as a general-purpose output (GPIO), follow these steps:
Configuring the RCC
Enable the clock for PORT A and the GPIO functionality:
This action informs the microcontroller that you intend to use PORT A for GPIO operations.
Configuring the GPIO Port
Set the PA.15 pin as a general-purpose output:
```c GPIOA->CRL &= ~(0xF << (15 * 4)); GPIOA->CRL |= (0x3 << (15 * 4)); // 0b0011 - Output mode, max speed 50 MHz ```This configuration Places PA.15 in output mode with a high-speed setting (50 MHz).
Blinking the LED
Now that the LED pin is configured, you can create a simple blinking pattern.
To generate a delay, you can use the SysTick timer, an independent 24-bit down counter controlled by the dedicated SysTick clock source. The SysTick clock is a high-frequency clock shared between SysTick and the core debugger. For the STM32F103x8, this clock runs at approximately 8 MHz.
Initializing the SysTick Timer
Initialize the SysTick timer with a specific delay in milliseconds:
```c void init_systick(uint16_t ms) { SysTick->CTRL = 0; SysTick->LOAD = (SystemCoreClock / 1000) * ms - 1ul; SysTick->VAL = 0ul; SysTick->CTRL = SysTick_CTRL_CLKSOURCE | SysTick_CTRL_ENABLE; } ```In this function, first disable the SysTick timer, then set the SysTick LOAD register to the desired delay in system clock cycles. Finally, reload the SysTick COUNT register and enable the SysTick timer.
Blinking the LED
Create a function to blink the LED using the SysTick delay:
```c void blink_led(uint16_t delay_ms) { while (1) { GPIOA->ODR ^= (1 << 15); // Toggle PA.15 (LED pin) init_systick(delay_ms); } } ```Here, the ~ while(1) loop continuously toggles the LED state (on and off) with a specified delay in milliseconds.
Implementing the Main Function
Finally, in the main function, call the blink_led function with an appropriate delay:
```c int main(void) { init_systick(100); // Delay for 100 ms blink_led(500); // Blink the LED every 500 ms while (1) { /* Avoid compiler warnings */ } } ```Ensure you include a simple infinite while loop at the end of the main function to avoid compiler warnings about predicted return values.
The STM32 LED blink example is a simple yet powerful project that introduces you to fundamental concepts in STM32 programming. Once you grasp the basics, you can build more complex projects, such as PWM-controlled LED dimming, LED animations, or interfacing with external sensors and displays. Happy coding!