cancel
Showing results for 
Search instead for 
Did you mean: 

Center aligned PWM is half the desired frequency

G S
Associate II
Posted on November 14, 2017 at 15:41

Hello everyone, I'm new to STM32 microcontrollers. I'm currently working on my first project using this microcontroller, it is for a motor control application. I am trying to set up the PWMs, I'd like to run them at a frequency of 15kHz and they must be center aligned. I am able to get edge aligned signals at this frequency, but when trying to set to center aligned the frequency drops to 58kHz. What am I doing wrong here?

/* Compute the value to be set in ARR register to generate signal frequency at 156 Khz */
 TimerPeriod = (SystemCoreClock / 35156);
/* Compute CCR1 value to generate a duty cycle at 25% for channel 1 */
 Channel1Pulse = (uint16_t) ((TimerPeriod + 1) - ((uint32_t) 25 * (TimerPeriod - 1)) / 1000);
 
 /* Compute CCR2 value to generate a duty cycle at 25% for channel 2 */
 Channel2Pulse = (uint16_t) ((TimerPeriod + 1) - ((uint32_t) 25 * (TimerPeriod - 1)) / 1000);
/* Compute CCR3 value to generate a duty cycle at 0% for channel 3 */
 Channel3Pulse = (uint16_t) ((TimerPeriod + 1) - ((uint32_t) 25 * (TimerPeriod - 1)) / 1000);
/* Time Base configuration */
 TIM_TimeBaseStructure.TIM_Prescaler = 0; /* Do not use a prescaler */
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; /* Timer mode: center aligned */
 TIM_TimeBaseStructure.TIM_Period = TimerPeriod; /* Configure timer period to be TimerPeriod amount of clock ticks before an up count */
 TIM_TimeBaseStructure.TIM_ClockDivision = 0; /* No clock division */
 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 1, 2 and 3 Configuration in PWM mode */
 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; /* In upcounting, channel is inactive as long as TIMx_CNT<TIMx_CCR1 else active */
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /* OC1 signal is output on the corresponding output pin */
 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; /* OC1 signal is output on the corresponding output pin */
 TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; /* Value loaded in the actual capture/compare register (specifies when to assert active pulse) */
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; /* Sets PWM output to active high */
 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; /* Sets complimentary PWM output to active high */
 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; /* When idle, reset PWM output */
 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set; /* When idle, set PWM complimentary output */
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
 TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
 TIM_OC3Init(TIM1, &TIM_OCInitStructure);
/* Automatic Output enable, Break, dead time and lock configuration*/
 TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; /* Off-State selection for Run Mode: When inactive, OC/OCN outputs are enabled with their inactive level as soon as CCxE=1 or CCxNE=1 */
 TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; /* Off-State selection for Idle Mode: When inactive, OC/OCN outputs are forced first with their idle level as soon as CCxE=1 or CCxNE=1 */
 TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; /* Bit write protection for DTG bits in TIMx_BDTR register, OISx and OISxN bits in TIMx_CR2 register and BKE/BKP/AOE bits in TIMx_BDTR register */
 TIM_BDTRInitStructure.TIM_DeadTime = DEAD_TIME; /* Dead-time inserted between the complementary outputs */
 TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
 TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
 TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
/* TIM1 counter enable */
 TIM_Cmd(TIM1, ENABLE);
/* Main Output Enable */
 TIM_CtrlPWMOutputs(TIM1, ENABLE);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

EDIT: Side question - what are the source code tags on this forum? The 'Source Code' button does not seem to do anything for me.

#center-aligned-pwm #stm32f3x-pwm #stm32f
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on November 14, 2017 at 16:20

'Center aligned' implies that the timer counts up to ARR then back down to zero, that's why the period of this cycle is twice of the plain up or down modes (frequency half of them).

EDIT: Side question - what are the source code tags on this forum?  The 'Source Code' button does not seem to do anything for me.

More->Syntax highlighter; be warned that that appears to impose moderation.

JW

View solution in original post

3 REPLIES 3
Posted on November 14, 2017 at 16:20

'Center aligned' implies that the timer counts up to ARR then back down to zero, that's why the period of this cycle is twice of the plain up or down modes (frequency half of them).

EDIT: Side question - what are the source code tags on this forum?  The 'Source Code' button does not seem to do anything for me.

More->Syntax highlighter; be warned that that appears to impose moderation.

JW

Posted on November 14, 2017 at 16:26

Hello!

It is half frequency because in center aligned mode the counter counts up and down .(normal behaviour)

Figure is from 32F4 RM.

0690X0000060PE1QAM.png

Regards

vf

Posted on November 17, 2017 at 09:10

 ,

 ,

HI.I want play anemation gif on ssd1963 &, stm32f103.

Is this possible?

And howØŸ

Regard

Mado

2017-11-14 18:57 GMT+03:30 Vangelis Fortounas <,

st-microelectronics@jiveon.com>,:

STMicroelectronics Community

<,https://community.st.com/?et=watches.email.thread>,

Re: Center aligned PWM is half the desired frequency

reply from Vangelis Fortounas

<,

in STM32 MCUs Forum - View the full discussion

<,https://community.st.com/0D70X000006SsbsSAC