Blink LED with STM32 Timer
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:
- Open STM32CubeIDE and create a new 'Hello World' project for your specific STM32 MCU (e.g., STM32F103x8).
- Generated project files will include a 'main.c' file where we'll implement our LED blinking function.
- 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.
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.
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:
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:
```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:
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.
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!