Documentation
README
Timers and PWM (Bare-Metal)
Purpose
Configure MCU timers for PWM output, input capture, and periodic scheduling: prescaler/ARR setup, compare channels, and SysTick as a simple tick source.
When to Use
- Motor/LED dimming via PWM
- Measuring pulse width (input capture)
- Bare-metal
millis()without FreeRTOS - Hardware periodic sampling trigger
Workflow
1. PWM on TIM2 CH1 (STM32)
f_pwm = f_timer_clk / ((PSC+1) * (ARR+1))
duty = (CCR / (ARR+1)) * 100%
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->PSC = 83; /* 84MHz / 84 = 1 MHz tick */
TIM2->ARR = 999; /* 1 kHz PWM */
TIM2->CCR1 = 500; /* 50% duty */
TIM2->CCMR1 |= (6U << TIM_CCMR1_OC1M_Pos); /* PWM mode 1 */
TIM2->CCER |= TIM_CCER_CC1E;
TIM2->CR1 |= TIM_CR1_CEN;
This is the opening of the README. Read the full README on GitHub.