AnsweredAssumed Answered

STM32F030 PWM Frequency changing!

Question asked by a.m.a on Nov 1, 2015
Latest reply on Dec 22, 2015 by Clive One
Hi!
Actually I'm new in STM32.
I have a problem in creating pwm. while making pwm in 20KHz with timer1, the frequency suddenly changes during the run! it is programmed to be 20KHz but it jumps to 2 or 3KHz.
i want to know if it is something with clock of the mcu or something else?
please help me!
the timer1 config function is shown below. SystemInit function is called in the main function too.

void TIM1_Config(void)
{
  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  
  /* Time Base configuration */
  TIM_TimeBaseInitStruct.TIM_Prescaler = 0;         //Prescaler=0  => Timer clock=48MHz
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInitStruct.TIM_Period =0x095F;             //TIM1_Period = (SystemCoreClock / 20000)-1
  TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
  TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;


  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
  
  TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;
  TIM_OCInitStruct.TIM_Pulse = 0x0258;                //25% Duty Cycle--> =25 * (TimerPeriod - 1)) / 100)=600
  TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High;
  TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCIdleState_Reset;


  TIM_OC2Init(TIM1, &TIM_OCInitStruct);
  
    /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);
  
  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

Outcomes