This project will cover the configuration and implementation of three advanced timer modes on a single STM32 timer unit. It demonstrates generating a single pulse, a continuous PWM signal, and measuring an external pulse's width, showcasing the versatility of STM32 timers.
In One-Pulse Mode (OPM), the timer generates a single PWM pulse with a precise duration upon a software or hardware trigger. This is essential for applications requiring a controlled, one-time activation signal, such as starting a specific process or triggering an external device.
In PWM Generation Mode, the timer continuously outputs a periodic signal with a programmable duty cycle. This mode is fundamental for control applications like adjusting LED brightness, driving motors, or controlling servo position.
In Input Capture Mode, the timer automatically records its counter value at the instant an external signal edge is detected. By capturing the timer value at two successive edges, the pulse width or frequency of an unknown signal can be measured with high precision.
STM32CubeMX Configuration- Create new STM32F103C6 project
- Set system clock to 32MHz (HSI → PLL)
- Configure a Timer (e.g., TIM1):
- Clock Source: Internal Clock.
- Channel 4: Configured as Input Capture Direct Mode
- Prescaler=59, Counter Period=65535.
- NVIC: Enable TIM1 Capture Compare interrupt.
- Configure a Timer (e.g., TIM2):
- Clock Source: Internal Clock.
- Prescaler=79, Counter Period=999.
- Channel 1: Configured as Output Compare PWM Mode 1.
- Channel 2: Configured as Input Capture Direct Mode.
- Configure a Timer (e.g., TIM3):
- Clock Source: Internal Clock.
- Enable "One Pulse Mode"
- Prescaler=5999, Counter Period=10.
- NVIC: Enable TIM3 global interrupt.
- GPIO configuration: Ensure timer output (e.g., PA0) and input capture (e.g., PA1) pins are correctly set.
- Configure USART1 as Asynchronous and set Baude-rate 9600
- Generate initialization code
- Open generated project
- Add application logic in main.c (e.g., start PWM, trigger one-pulse, handle input capture values)
- Implement the Input Capture ISR in stm32f1xx_it.c to calculate pulse width
- Build and debug
- Add STM32F103C6 to schematic
- Connect test components: LED to PWM output, pulse generator to input capture pin, button to trigger one-pulse.
- Load compiled .hex file
- Start simulation and verify signals with virtual oscilloscope
If you have any questions or suggestions don't hesitate to leave a comment below
Comments