AnsweredAssumed Answered

Center aligned PWM is half the desired frequency

Question asked by G S on Nov 14, 2017
Latest reply on Nov 17, 2017 by abolfazl rodsarabe

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 35.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 17.58kHz.  What am I doing wrong here?

 

/* Compute the value to be set in ARR register to generate signal frequency at 35.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.

Outcomes