Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
PWM Generation — Controlling the Analog World
Generate precise PWM signals using STM32F407 timers. CCMR, CCR, CCER registers explained with interactive duty-cycle demo, LED dimming, and servo motor control examples.
What is PWM and How Does STM32 Generate It?
PWM (Pulse Width Modulation) is a digital signal that alternates between HIGH and LOW at a fixed frequency, with a variable “duty cycle” — the percentage of time the signal is HIGH per period. It’s used to control motor speed, LED brightness, servo position, and to generate analog signals without a DAC.
In STM32 timers, PWM is generated purely in hardware:
- The counter counts from 0 to ARR (set by PSC and ARR for the desired frequency)
- The output pin is HIGH while CNT < CCR (PWM mode 1)
- The output pin goes LOW when CNT reaches CCR
- At ARR+1, the counter wraps to 0 and the pin goes HIGH again
- Duty cycle = CCR / (ARR + 1)
TIMx_CCMR1 — Capture/Compare Mode Register 1
This register configures channels 1 and 2 — whether they’re in input capture or output compare mode, and in OC mode, what the output behavior is.
| Bits | Field | For Channel 1 | For Channel 2 |
|---|---|---|---|
| [1:0] | CC1S | 00=Output, 01=IC(TI1), 10=IC(TI2) | — |
| [2] | OC1FE | Output Compare Fast Enable | — |
| [3] | OC1PE | Output Compare Preload Enable (1=recommended for PWM) | — |
| [6:4] | OC1M | Output Compare Mode: 110=PWM1, 111=PWM2 | — |
| [7] | OC1CE | Clear Enable (for OCRef clear on ETRF) | — |
| [9:8] | CC2S | — | Channel 2 selection |
| [14:12] | OC2M | — | Channel 2 output mode |
| [11] | OC2PE | — | Channel 2 preload enable |
PWM Mode 1 (OC1M=110): Output is HIGH while CNT < CCR, LOW while CNT ≥ CCR (normal PWM, duty cycle = CCR/ARR).
PWM Mode 2 (OC1M=111): Output is LOW while CNT < CCR, HIGH while CNT ≥ CCR (inverted PWM, duty cycle = 1 – CCR/ARR).
#define TIM2_CCMR1 (*((volatile uint32_t*)0x40000018))
#define TIM2_CCMR2 (*((volatile uint32_t*)0x4000001C))
/* Configure Channel 1 as PWM Mode 1 output with preload */
/* CC1S=00 (output), OC1PE=1 (preload), OC1M=110 (PWM1) */
TIM2_CCMR1 &= ~(0xFF << 0); /* Clear channel 1 bits [7:0] */
TIM2_CCMR1 |= (0 << 0); /* CC1S = 00 (output mode) */
TIM2_CCMR1 |= (1 << 3); /* OC1PE = 1 (preload enable) */
TIM2_CCMR1 |= (6 << 4); /* OC1M = 110 (PWM mode 1) */
/* Configure Channel 2 as PWM Mode 1 output with preload */
TIM2_CCMR1 &= ~(0xFF << 8);
TIM2_CCMR1 |= (1 << 11); /* OC2PE = 1 */
TIM2_CCMR1 |= (6 << 12); /* OC2M = 110 */
/* For channels 3 and 4, use CCMR2 register with same bit layout */
TIM2_CCMR2 &= ~0xFFFF;
TIM2_CCMR2 |= (1<<3)|(6<<4); /* Ch3: preload + PWM1 */
TIM2_CCMR2 |= (1<<11)|(6<<12); /* Ch4: preload + PWM1 */
TIMx_CCER — Capture/Compare Enable Register
This register enables each channel’s output and sets the output polarity.
| Bits | Field | Description |
|---|---|---|
| 0 | CC1E | Channel 1 output enable. 1=pin controlled by timer. |
| 1 | CC1P | Channel 1 polarity. 0=active HIGH (normal). 1=active LOW (inverted). |
| 3 | CC1NP | Channel 1 complementary polarity (advanced timers). |
| 4 | CC2E | Channel 2 output enable. |
| 5 | CC2P | Channel 2 polarity. |
| 8 | CC3E | Channel 3 output enable. |
| 12 | CC4E | Channel 4 output enable. |
#define TIM2_CCER (*((volatile uint32_t*)0x40000020))
/* Enable channels 1, 2, 3, 4 — all active HIGH */
TIM2_CCER = (1<<0) | (1<<4) | (1<<8) | (1<<12);
/* Channel 1 active HIGH, Channel 2 active LOW (inverted) */
TIM2_CCER = (1<<0) | (1<<4) | (1<<5);
/* Note: CC1P changes the meaning of PWM mode 1: */
/* CC1P=0 (normal): HIGH when CNT < CCR */
/* CC1P=1 (inverted): LOW when CNT < CCR (same as PWM mode 2 without inverting) */
CCR Register — Setting Duty Cycle
The CCR (Capture/Compare Register) value directly determines duty cycle when preload is enabled. Changing CCR at runtime smoothly changes the duty cycle without disrupting the timer or glitching the output.
Duty Cycle Formula:
Duty Cycle (%) = (CCR / (ARR + 1)) × 100
CCR = (Duty_Percent / 100) × (ARR + 1)
Examples with ARR = 999 (1000 steps):
10% → CCR = 100
25% → CCR = 250
50% → CCR = 500
75% → CCR = 750
100% → CCR = 1000 (or ARR+1 — but 100% means always HIGH)
0% → CCR = 0 (always LOW)
#define TIM2_CCR1 (*((volatile uint32_t*)0x40000034))
#define TIM2_CCR2 (*((volatile uint32_t*)0x40000038))
#define TIM2_CCR3 (*((volatile uint32_t*)0x4000003C))
#define TIM2_CCR4 (*((volatile uint32_t*)0x40000040))
/* Set duty cycle at runtime — just write to CCR */
/* With ARPE=1 and OC1PE=1, new value takes effect at next period */
#define PWM_SetDuty(ccr_reg, duty_pct, arr_val) \
(ccr_reg) = ((duty_pct) * ((arr_val) + 1)) / 100
/* Example: set TIM2 CH1 to 75% duty cycle (ARR=999) */
PWM_SetDuty(TIM2_CCR1, 75, 999); /* CCR1 = 750 */
/* LED fade: gradually increase brightness */
for (int duty = 0; duty <= 100; duty++) {
TIM2_CCR1 = (duty * 1000) / 100; /* 0–1000 range */
delay_ms(10);
}
LED Dimming — Complete Example
The Discovery board has 4 LEDs on PD12–PD15. TIM4 channels 1–4 connect to these pins via AF2. This is a complete 4-channel LED PWM example.
| LED | Pin | Timer Channel | AF | Color |
|---|---|---|---|---|
| LD4 | PD12 | TIM4_CH1 | AF2 | Green |
| LD3 | PD13 | TIM4_CH2 | AF2 | Orange |
| LD5 | PD14 | TIM4_CH3 | AF2 | Red |
| LD6 | PD15 | TIM4_CH4 | AF2 | Blue |
/*
* 4-channel LED PWM using TIM4 on STM32F407 Discovery
* TIM4 clock = 84 MHz (APB1 × 2)
* PWM frequency = 1 kHz (1ms period, good for LED dimming)
* Resolution: 1000 steps (0–999 CCR range)
*
* PSC = 83 → 84MHz / 84 = 1 MHz tick rate (1µs per tick)
* ARR = 999 → 1000 ticks × 1µs = 1ms period = 1kHz
*/
#include <stdint.h>
#define RCC_AHB1ENR (*((volatile uint32_t*)0x40023830))
#define RCC_APB1ENR (*((volatile uint32_t*)0x40023840))
#define GPIOD_MODER (*((volatile uint32_t*)0x40020C00))
#define GPIOD_AFRH (*((volatile uint32_t*)0x40020C24))
#define TIM4_BASE 0x40000800UL
#define TIM4_CR1 (*((volatile uint32_t*)(TIM4_BASE + 0x00)))
#define TIM4_CCMR1 (*((volatile uint32_t*)(TIM4_BASE + 0x18)))
#define TIM4_CCMR2 (*((volatile uint32_t*)(TIM4_BASE + 0x1C)))
#define TIM4_CCER (*((volatile uint32_t*)(TIM4_BASE + 0x20)))
#define TIM4_PSC (*((volatile uint32_t*)(TIM4_BASE + 0x28)))
#define TIM4_ARR (*((volatile uint32_t*)(TIM4_BASE + 0x2C)))
#define TIM4_CCR1 (*((volatile uint32_t*)(TIM4_BASE + 0x34)))
#define TIM4_CCR2 (*((volatile uint32_t*)(TIM4_BASE + 0x38)))
#define TIM4_CCR3 (*((volatile uint32_t*)(TIM4_BASE + 0x3C)))
#define TIM4_CCR4 (*((volatile uint32_t*)(TIM4_BASE + 0x40)))
#define TIM4_EGR (*((volatile uint32_t*)(TIM4_BASE + 0x14)))
void pwm_led_init(void) {
/* Enable clocks */
RCC_AHB1ENR |= (1 << 3); /* GPIOD */
RCC_APB1ENR |= (1 << 2); /* TIM4 */
/* PD12-PD15: Alternate Function mode (MODER = 10) */
GPIOD_MODER &= ~(0xFF << 24);
GPIOD_MODER |= (0xAA << 24); /* 10 10 10 10 for pins 15-12 */
/* Set AF2 (TIM4) for PD12-PD15 — these are in AFRH (pins 8-15) */
/* AFRH bits: [19:16]=PD12, [23:20]=PD13, [27:24]=PD14, [31:28]=PD15 */
GPIOD_AFRH &= ~(0xFFFF << 16);
GPIOD_AFRH |= (0x2222 << 16); /* AF2 for all four pins */
/* TIM4: PSC=83, ARR=999 → 1kHz PWM with 1000-step resolution */
TIM4_PSC = 83;
TIM4_ARR = 999;
/* All 4 channels: OC mode = PWM1 (110), preload enabled */
TIM4_CCMR1 = (6<<4) | (1<<3) | /* CH1: OC1M=PWM1, OC1PE=1 */
(6<<12) | (1<<11); /* CH2: OC2M=PWM1, OC2PE=1 */
TIM4_CCMR2 = (6<<4) | (1<<3) | /* CH3: OC3M=PWM1, OC3PE=1 */
(6<<12) | (1<<11); /* CH4: OC4M=PWM1, OC4PE=1 */
/* Enable all 4 channels, active HIGH */
TIM4_CCER = (1<<0) | (1<<4) | (1<<8) | (1<<12);
/* Initial duty cycles */
TIM4_CCR1 = 0; /* PD12 green LED: 0% */
TIM4_CCR2 = 250; /* PD13 orange: 25% */
TIM4_CCR3 = 500; /* PD14 red: 50% */
TIM4_CCR4 = 750; /* PD15 blue: 75% */
/* Load shadow registers and start */
TIM4_EGR = 1;
TIM4_CR1 = (1<<7) | (1<<0); /* ARPE=1 (buffered ARR), CEN=1 */
}
int main(void) {
SystemClock_Config(); /* 168 MHz */
SysTick_Init(168000000);
pwm_led_init();
uint32_t t = 0;
while (1) {
t = g_ms_ticks;
/* LED "breathing" effect — sine-like using triangle wave */
uint32_t phase = t % 2000;
uint32_t duty = (phase < 1000) ? phase : (2000 - phase);
TIM4_CCR1 = duty; /* Ramp 0→999→0 every 2 seconds */
}
}
Servo Motor Control
Standard RC servos expect a 50Hz PWM signal (20ms period). The pulse width determines position: 1ms = 0°, 1.5ms = 90° (center), 2ms = 180°. The total range is 1ms to 2ms pulse within a 20ms period.
/*
* Servo control using TIM2 CH1 on PA0 (AF1)
* 50 Hz PWM = 20ms period
* TIM2 clock = 84 MHz
* PSC = 839 → 84MHz/840 = 100,000 Hz (10µs per tick)
* ARR = 1999 → 2000 ticks × 10µs = 20ms = 50Hz
*
* Servo pulse width in ticks (10µs each):
* 0° → 1.0ms = 100 ticks → CCR = 100
* 90° → 1.5ms = 150 ticks → CCR = 150
* 180° → 2.0ms = 200 ticks → CCR = 200
*/
#define SERVO_MIN_TICKS 100 /* 1.0ms (0 degrees) */
#define SERVO_MID_TICKS 150 /* 1.5ms (90 degrees) */
#define SERVO_MAX_TICKS 200 /* 2.0ms (180 degrees) */
/* Convert angle (0-180) to CCR value */
uint32_t servo_angle_to_ccr(uint32_t angle_deg) {
if (angle_deg > 180) angle_deg = 180;
/* Linear map: 0° → 100 ticks, 180° → 200 ticks */
return SERVO_MIN_TICKS + (angle_deg * (SERVO_MAX_TICKS - SERVO_MIN_TICKS)) / 180;
}
void servo_init(void) {
/* PA0 → AF1 (TIM2_CH1) */
RCC_AHB1ENR |= 1;
RCC_APB1ENR |= 1;
GPIOA_MODER = (GPIOA_MODER & ~(3)) | 2; /* AF mode */
GPIOA_AFRL = (GPIOA_AFRL & ~0xF) | 1; /* AF1 */
TIM2_PSC = 839;
TIM2_ARR = 1999;
TIM2_CCR1 = SERVO_MID_TICKS; /* Start at center */
TIM2_CCMR1 = (6 << 4) | (1 << 3); /* PWM1, preload */
TIM2_CCER = (1 << 0); /* CH1 enable */
TIM2_EGR = 1; /* Load shadow regs */
TIM2_CR1 = (1<<7) | (1<<0); /* ARPE + CEN */
}
void servo_set_angle(uint32_t degrees) {
TIM2_CCR1 = servo_angle_to_ccr(degrees);
}
int main(void) {
SystemClock_Config();
SysTick_Init(168000000);
servo_init();
while (1) {
servo_set_angle(0); delay_ms(1000);
servo_set_angle(90); delay_ms(1000);
servo_set_angle(180); delay_ms(1000);
servo_set_angle(90); delay_ms(1000);
}
}
Multi-Channel and Multi-Timer PWM
Each timer has 4 channels, each can have independent duty cycles but they all share the same frequency (PSC and ARR). For different frequencies, use different timers.
| Timer | CH1 | CH2 | CH3 | CH4 |
|---|---|---|---|---|
| TIM2 | PA0(AF1) | PA1(AF1) | PA2(AF1) | PA3(AF1) |
| TIM3 | PA6(AF2)/PB4/PC6 | PA7(AF2)/PB5/PC7 | PB0(AF2)/PC8 | PB1(AF2)/PC9 |
| TIM4 | PB6(AF2)/PD12 | PB7(AF2)/PD13 | PB8(AF2)/PD14 | PB9(AF2)/PD15 |
| TIM5 | PA0(AF2) | PA1(AF2) | PA2(AF2) | PA3(AF2) |
/* RGB LED control using 3 channels of TIM3 */
/* R=PA6 (TIM3_CH1), G=PA7 (TIM3_CH2), B=PB0 (TIM3_CH3) */
void rgb_set(uint8_t r, uint8_t g, uint8_t b) {
/* r,g,b are 0-255 values */
TIM3_CCR1 = (r * (TIM3_ARR + 1)) / 256;
TIM3_CCR2 = (g * (TIM3_ARR + 1)) / 256;
TIM3_CCR3 = (b * (TIM3_ARR + 1)) / 256;
}
/* Usage — LED color mixing: */
rgb_set(255, 0, 0); /* Full red */
rgb_set(0, 255, 0); /* Full green */
rgb_set(128, 0, 128); /* Purple (red + blue) */
rgb_set(255, 165, 0); /* Orange */
Summary
PWM Frequency
Set by PSC and ARR: f_PWM = f_tim / ((PSC+1) × (ARR+1))
Duty Cycle
Set by CCR: Duty% = CCR / (ARR+1) × 100. Update CCR at runtime.
CCMR1/2
CC1S=00 (output), OC1M=110 (PWM mode 1), OC1PE=1 (preload).
CCER
CC1E=1 to enable channel output. CC1P=0 for active-HIGH.
CR1
ARPE=1 (buffer ARR), CEN=1 (start). Write EGR=1 first to load shadow registers.
Servo Timing
50Hz = 20ms period. 0°=1ms, 90°=1.5ms, 180°=2ms pulse width.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments