Associate II
January 21, 2015
Question
STM32F4 Two Complementary PWM Outputs does not have the same frequency
- January 21, 2015
- 5 replies
- 1453 views
Posted on January 21, 2015 at 05:29

Have anyone ever had trouble when updating frequency can cause the two complementary pwm outputs not have the same frequency?
I'm using STM32F407VGT6 Discovery, controlling the PWM frequency.
static void TIM1_Configuration(void)
{
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
TIM_TimeBaseStructure.TIM_Period = Timer_Period;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // update event is generated at each counter overflow
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM1->EGR = TIM_PSCReloadMode_Update;
TIM1->CR1 |= TIM_CR1_URS;
TIM1->CR1 &= (uint16_t)~TIM_OPMode_Single;
/* Channel 1, 3 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Co the dung 2 lenh nay de ngat xung
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Channel_Pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Khong lien quan den trang thai sau khi ngat xung
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //Khong lien quan den trang thai sau khi ngat xung
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1PreloadConfig(TIM1, TIM_CCMR1_OC1PE);
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM1->CR1 |= TIM_CR1_ARPE; // Set the ARR Preload Bit
/* Automatic Output enable, Break, dead time and lock configuration*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 0;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);//1Tcy
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void TIM1_PWM_Freq_Update(void)
{
TIM1->CR1 |= TIM_CR1_UDIS;
TIM1->ARR = Timer_Period;
TIM1->CR1 &= (uint16_t)~TIM_CR1_UDIS;
}
