This project demonstrates how to generate and control two PWM outputs on an STM32F103C8T6 (Blue Pill board) using STM32CubeIDE and HAL libraries.
It uses TIM2 to drive two channels:
- Channel 1 duty cycle increases gradually from 0 to 255.
- Channel 2 duty cycle decreases gradually from 255 to 0.
- Dual PWM output using TIM2 Channel 1 and Channel 2.
- Dynamic duty cycle adjustment.
- Uses STM32 HAL functions for portability.
- Delay-based update for visual testing with LEDs.
- STM32F103C8T6 board (Blue Pill)
- Two LEDs (or other PWM-controllable devices)
- Resistors for LEDs
- USB to Serial / ST-Link programmer
| Timer Channel | Pin | Alternate Function |
|---|---|---|
| TIM2_CH1 | PA0 | PWM Output |
| TIM2_CH2 | PA1 | PWM Output |
- TIM2 is initialized for PWM mode.
- The code runs in an infinite loop:
pulse_ch1increments (LED1 gradually brightens).pulse_ch2decrements (LED2 gradually dims).- When
pulse_ch1reaches 255, it resets to 0. - When
pulse_ch2reaches 0, it resets to 255.
- The PWM duty cycle is updated using:
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pulse_ch1);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, pulse_ch2);