2014-03-27 09:49 AM
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 E5I am having trouble with properly understanding how to set the prescalar and period to get this pwm signal.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 #timer2014-03-27 10:10 AM
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.2014-03-27 08:22 PM
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.2014-03-28 03:22 AM
The system clocks weren't configured properly.
Thanks clive