Showing results for 
Search instead for 
Did you mean: 

Changing PWM during runtime using timers

Associate III

Went through examples of Nucleo -L073RZ eval board for PWM output

It generates four PWM waves using Timer 2 channel 1 ,channel2 ,channel 3,channel 4

at 50%,25%, duty cycle . So the pwm duty cycle is fixed.

but i have a fan pwm connected to Tim2 channel 1 pin

I want to change pwm during runtime,so as to control fan speed.

How to do it using timer2

and what is PWM mode 1 and PWM mode 2

Peter Mather
Associate III

    htim2.Instance->CCR1 = new_value

Bob S

PWM mode 1 and mode 2 differ in the polarity of the output signal. If you look in the ref manual for the description of the OC1M bit field, mode 1 has the output active from CNT=0, then goes inactive when CNT=CCR1. Mode 2 is the opposite, the output is inactive at CNT=0, then goes active when CNT=CCR1. Note that the actual output value of "active" and "inactive" depend on the CCxP bits in the CCER register. For CCxP=0, "active" is high (1) and "inactive" is low (0). For CCxP=1, "active" is low (0) and "inactive" is high (1).


When there are many PWM, by inverting half of them, at least not all the GPIO swing in the same direction causing EMI and supply droop. Some advanced timers have negated outputs with skew control for this type of need (and motor control high current switches too).