Try Embedded

Nucleo-F401RE: PWM fade

Fade an LED on D9 with TIM3 channel 2 on PC7, set up in registers: prescaler, auto-reload and compare.

Output · Beginner

In a real product: Dimmable reading lamp (Smart home): PWM sets the LED's brightness without wasting power in a resistor.

An STM32 pin is either high or low, so to dim an LED it switches on and off very fast, and changes how long it stays on in each period. That is PWM, and a hardware timer makes the waveform while the CPU does other work.

Here TIM3 runs at 1 kHz and its channel 2 drives PC7, which is D9 on the Nucleo's header.

What you'll learn

  • Handing a pin to a timer: MODER alternate function and AFR AF2 for TIM3 on PC7
  • Setting the PWM's frequency with the prescaler (PSC) and the auto-reload value (ARR)
  • Setting the duty cycle with the compare register (CCR2), and the PWM mode bits in CCMR1

How it works

TIM3 is clocked at 16 MHz after reset. PSC = 15 divides that by 16, so the counter counts at 1 MHz, and ARR = 999 makes it wrap every 1000 counts: a 1 kHz period. In PWM mode 1 (OC2M = 110) the output is high while the count is below CCR2, so CCR2 = 250 is 25 % duty, and CCR2 = 1000 keeps it high.

OC2PE and ARPE make new values wait for the end of the period, so the waveform never glitches mid-cycle. UG loads them once before the timer starts, CC2E connects the channel to its pin, and CEN starts counting.

The loop moves CCR2 up and down by 5 every 5 ms, so a fade from off to full takes a second.

Try this

  • Change PSC to 1599 and watch the LED flicker at 10 Hz.
  • Put a logic analyzer on D9 and measure the duty cycle as it fades.
  • Use TIM3's update interrupt in place of delay_ms() to step the fade.

main.c

// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// Fade an LED on D9 (PC7) with TIM3 channel 2: 1 kHz PWM, duty from 0 to 100 % and back.
#include "stm32f4xx.h"

static volatile uint32_t ticks;   // milliseconds since SysTick started

void SysTick_Handler(void) {
  ticks++;
}

static void delay_ms(uint32_t ms) {
  uint32_t start = ticks;
  while ((uint32_t)(ticks - start) < ms) {}
}

int main(void) {
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
  RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
  GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODER7) | GPIO_MODER_MODER7_1;   // PC7: alternate function
  GPIOC->AFR[0] = (GPIOC->AFR[0] & ~(0xFu << 28)) | (2u << 28);               // AF2: TIM3 CH2

  TIM3->PSC = 16 - 1;     // 16 MHz / 16 = 1 MHz counter
  TIM3->ARR = 1000 - 1;   // 1000 counts: 1 kHz
  TIM3->CCR2 = 0;
  TIM3->CCMR1 = (TIM3->CCMR1 & ~TIM_CCMR1_OC2M) | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2PE;   // PWM mode 1
  TIM3->CCER |= TIM_CCER_CC2E;
  TIM3->EGR = TIM_EGR_UG;   // load PSC and ARR now
  TIM3->CR1 |= TIM_CR1_ARPE | TIM_CR1_CEN;

  SysTick->LOAD = SystemCoreClock / 1000 - 1;   // 1 ms
  SysTick->VAL = 0;
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

  int level = 0, step = 5;
  while (1) {
    TIM3->CCR2 = level;
    level += step;
    if (level == 0 || level == 1000) step = -step;
    delay_ms(5);
  }
}

Lessons behind it