cancel
Showing results for 
Search instead for 
Did you mean: 

PWM generation in STM32F4 Discovery board

kasun_duminda92
Associate III
Posted on March 27, 2014 at 17:49

Hello everybody.

I am trying to generate a 5 PWM signals and all should look like the one below when the duty cycle is set to 50%.

4 of the signals should be generated on TIM 5 (CH1, CH2, CH3, CH4) on pins A0, A1, A2 and A3. The other one should be generated on TIM 9 (CH1) on pin E5

I am having trouble with properly understanding how to set the prescalar and period to get this pwm signal.

0690X00000602rfQAA.jpg

Can I please get a sample code to generate the signals I need or some direction on how to configure the timers TIM5 and TIM9

#pwmoutput #stm32f4 #timer
3 REPLIES 3
chen
Associate II
Posted on March 27, 2014 at 18:10

Hi

Firstly, you are not the only one which has trouble with the timers. The documentation is shockingly bad.

Please refer to the documentation for your processor, the reference manual is your main source of reference here.

The General TIMers all share the same channels, if you use up channel 1, 2, 3 and 4 - they are all used up for ALL TIMers!

It is unclear (to me) if the special timers share the same channels.

See the block diagram for the timers.

If you want the same output from ALL your PWMs - then you can use the same channel for all the outputs.

To work out the pre-scalers - see the timing tree in the documentation.

You set the clock source to the timer (this will be shared with the other peripherals on the AH clock bus).

Work out what freq you want and adjust the prescaler, ARE and CMRRx registers.

The pre-scaler (for the timer) will set the rate at which the CNT increments.

The ARE reg sets the frequence/period of the PWM. ie the value at which CNT is reloaded.

The CMRRx and ARE registers control the PWM ratio.

Posted on March 28, 2014 at 04:22

Understand that they are factors, you should put the smaller one in the prescaler, and they should fit in 16-bit. They are integers, so you might need to adjust system clock to get specific values out. There are some 32-bit timers (on APB1), the prescaler is still limited to 16-bit.

FREQ = TIMCLK / (PRESCALER * PERIOD);

  TIM_TimeBaseStructure.TIM_Prescaler = PRESCALER - 1;

  TIM_TimeBaseStructure.TIM_Period = PERIOD - 1;

..

  TIM_OCInitStructure.TIM_Pulse =  PERIOD / 2; // 50/50 duty

The math is NOT complicated.

Each TIM unit can generate a SINGLE frequency without adjustment.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kasun_duminda92
Associate III
Posted on March 28, 2014 at 11:22

The system clocks weren't configured properly.

Thanks clive