stm32 blink led with timer

Blink LED with STM32 Timer

By Nicole Brown
AD

Embarking on the journey of programming STM32 microcontrollers often begins with the exhilarating task of blinking an LED. This ubiquitous 'Hello, World!' of the microcontroller realm not only verifies that your hardware is functioning correctly but also familiarizes you with the toolchain and basic programming concepts. One popular approach to achieve this is by using a timer to control the LED's on and off states. Let's delve into the world of STM32 and explore how to blink an LED using a timer.

Before we dive into the code, let's ensure we have the necessary hardware and software in place. You'll need an STM32 development board, such as the STM32F103C8T (blue pill), and an LED connected to one of the board's GPIO pins. For programming, you can use an open-source IDE like STM32CubeIDE or makefile-based tools like makeflat or openstm32.

Setting Up the Development Environment

Before we begin, let's set up our development environment. If you're using STM32CubeIDE, follow these steps:

AD
The Blinking LED: a Beginner's Guide With the NE555 Timer
The Blinking LED: a Beginner's Guide With the NE555 Timer
External Led Blink Using STM32F401CE
External Led Blink Using STM32F401CE
Stm32f103 microcontroller Led on/off control with Push Button as Input
Stm32f103 microcontroller Led on/off control with Push Button as Input
  1. Open STM32CubeIDE and create a new 'Hello World' project for your specific STM32 MCU (e.g., STM32F103x8).
  2. Generated project files will include a 'main.c' file where we'll implement our LED blinking function.
  3. Ensure you've connected your STM32 board to your computer and that proper drivers are installed.

Configuring the GPIO Pin

To blink an LED, we first need to configure the GPIO pin connected to the LED. In STM32, each GPIO pin can be configured as either an input or output. For our LED, we want an output. In your 'main.c' file, find the 'main()' function and add the following code before the 'while(1)' loop:

```c RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; // Enable GPIOA clocks GPIOA->MODER &= ~GPIO_MODER_MODER11; // Clear MODER11 bits GPIOA->MODER |= GPIO_MODER_MODER11_0; // Set MODER11 to output mode (01) ```

In this code, we enable the clocks for GPIOA (where our LED is connected) and set GPIOA's pin 11 (PA11) as an output.

Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
STM32: First Blinking LED Project
STM32: First Blinking LED Project
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx

Configuring the Timer

Next, let's configure a timer to control the LED. For this example, we'll use Timer 2, which is a 16-bit up-counter. Add the following code before the 'while(1)' loop:

```c RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // Enable TIM2 clocks TIM2->PSC = (SystemCoreClock / 10000) - 1; // Set prescaler for 1ms ticks TIM2->ARR = 10000; // Set auto-reload value for 1s period TIM2->CR1 |= TIM_CR1_CEN; // Enable counter ```

Here, we enable TIM2 clocks, set the prescaler for a 1ms tick, set the auto-reload value for a 1s period, and enable the counter.

AD

Implementing the LED Blinking Function

Now that our GPIO pin and timer are configured, let's implement the LED blinking function. Add the following code to your 'main()' function:

Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Efficient LED Blinking for Embedded Systems
Efficient LED Blinking for Embedded Systems
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 with Arduino IDE - iCircuit
STM32 with Arduino IDE - iCircuit
```c while(1) { if(TIM2->SR & TIM_SRUIE) // Check if Timer 2 Update Interrupt flag is set { GPIOA->ODR ^= GPIO_ODR_11; // Toggle PA11 (LED) TIM2->SR &= ~TIM_SRUIE; // Clear Timer 2 Update Interrupt flag } } ```

In this code, we check if the Timer 2 Update Interrupt flag is set. If it is, we toggle the LED (PA11) and clear the interrupt flag. This way, the LED will toggle its state every second (due to our 1s period).

Adding the Timer Interrupt

To make our LED blinking code non-blocking, we can add an interrupt request (IRQ) vector to handle the timer's update event. Add the following code to your 'main.c' file, inside the 'void SysInit(void)' function:

AD
```c NVIC_SetPriority(TIM2_IRQn, 0); // Set Timer 2 IRQ priority NVIC_EnableIRQ(TIM2_IRQn); // Enable Timer 2 IRQ ```

Then, add the following code at the end of your main.c file, just before the closing brace (}) of your 'int main(void)' function:

PWM with STM32 - The Engineering Projects
PWM with STM32 - The Engineering Projects
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
Blinkenlights To Bootloader: A Guide To STM32 Development
Blinkenlights To Bootloader: A Guide To STM32 Development
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
LED Flashing Project with 555 Timer in Proteus - The Engineering Projects
LED Flashing Project with 555 Timer in Proteus - The Engineering Projects
Blink Blonk
Blink Blonk
How to Blink An LED Bulb Using ESP32 Board
How to Blink An LED Bulb Using ESP32 Board
STM32F103C8T6 - Purple Pill
STM32F103C8T6 - Purple Pill
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
Начинаем изучать STM32 или Управляем светом по-умному
Начинаем изучать STM32 или Управляем светом по-умному
```c void TIM2_IRQHandler(void) { if(TIM2->SR & TIM_SR_UIE) // Check if Timer 2 Update Interrupt flag is set { GPIOA->ODR ^= GPIO_ODR_11; // Toggle PA11 (LED) TIM2->SR &= ~TIM_SR_UIE; // Clear Timer 2 Update Interrupt flag } } ```

This code handling the timer's update event in an interrupt service routine (ISR), keeping our main code non-blocking while blinking the LED.

Building and Running the Project

Now that we've implemented our LED blinking function using a timer, let's build and run the project. In STM32CubeIDE, click on 'Run As' -> 'STM32 App Project'. Select your project and click 'Finish'. If everything is configured correctly, your LED should be blinking on and off every second.

AD

In the realm of STM32 programming, blinking an LED with a timer is just the beginning. As you delve deeper into the world of STM32, you'll explore more complex use cases, such as using different timers, implementing delays, and creating pulse-width modulation (PWM) signals to control the brightness of your LED. Happy coding!

AD