cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030 PWM Frequency changing!

m_a_amirian
Associate II
Posted on November 01, 2015 at 07:54

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);

}

7 REPLIES 7
Amel NASRI
ST Employee
Posted on November 10, 2015 at 17:31

Hi a.m.a,

Did you configured the correct pin (PA9) with alternate function (AF2) to be used for channel2?

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

m_a_amirian
Associate II
Posted on November 16, 2015 at 06:45

Hi Mayla! and sorry for the late response.

Yes. It is done in the main function but i did not copy the whole code above.

Here it is:

 /* GPIOA clock enable */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  

  /* TIM3 channel 1 & TIM1 Channel 2 pin configuration */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Connect TIM1 pins to AF2 */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);  

  

  /* Connect TIM3 pins to AF1 */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);

 

Posted on November 16, 2015 at 17:27

TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCIdleState_Reset; // THIS IS WRONG

Not sure why your code is causing a frequency change, I'd validate the clocks, and monitor the internal clocks via PA8 MCO pin.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
karatepe101
Associate II
Posted on November 17, 2015 at 14:23

What is your clock source(Hse,Hsi...)?

ftiti
Associate II
Posted on November 18, 2015 at 18:09

It seems is a problem related to the systemClock config or a hardware/tool issue.

The following is a functional main() with your case implimented:

/* Private typedef -----------------------------------------------------------*/

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;

  TIM_OCInitTypeDef  TIM_OCInitStruct;

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

static void TIM_Config(void);

/* Private functions ---------------------------------------------------------*/

/**

  * @brief  Main program.

  * @param  None

  * @retval None

  */

int main(void)

{

  /* TIM Configuration */

  TIM_Config();

  /* Infinite loop */

  while (1)

  {

  }

}

static void TIM_Config(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

 

  /* GPIOA and GPIOB clocks enable */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* TIM1 clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  /* GPIO Configuration:*/

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Connect TIM pins to AF */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);

 

 /* 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;           

  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% --> =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);

}

this code is working and validated under these systemClock config:

  *=============================================================================

  *        System Clock source                    | PLL(HSE)

  *-----------------------------------------------------------------------------

  *        SYSCLK(Hz)                             | 48000000

  *-----------------------------------------------------------------------------

  *        HCLK(Hz)                               | 48000000

  *-----------------------------------------------------------------------------

  *        AHB Prescaler                          | 1

  *-----------------------------------------------------------------------------

  *        APB Prescaler                          | 1

  *-----------------------------------------------------------------------------

  *        HSE Frequency(Hz)                      | 8000000

  *----------------------------------------------------------------------------

  *        PLLMUL                                 | 6

  *-----------------------------------------------------------------------------

  *        PREDIV                                 | 1

  *-----------------------------------------------------------------------------

  *        Flash Latency(WS)                      | 1

  *-----------------------------------------------------------------------------

  *        Prefetch Buffer                        | ON

  *-----------------------------------------------------------------------------

A+
m_a_amirian
Associate II
Posted on December 22, 2015 at 08:44

Thank you all my friends!

I think my main problem is with the clock setting. would anyone tell me how to set mcu clock? I have used SystemInit() function in the main code but im not sure the clock is correctly configured or not. I activated timer14 interrupt in 2KHz and in its ISR one of the micro pins is toggled. but the frequency of the signal on this pin is sth about 17KHz and sometimes it suddenly changes. i thing the clock is the main problem. what is your idea?

I should say that HSE_Value in stm32f0xx.h file is set to 8000000 as my crystal frequency.
Posted on December 22, 2015 at 14:35

I think the best place to understand how it works is the Reference Manual (Clock Tree), and the template examples.

You'll need to debug SystemInit() to understand what is going on there, most tool chains call SystemInit() prior to entering main(). You should turn off ''run to main()'' or whatever your tools support and step through the code to understand if the clocks are being turned on successfully.

Is this a custom board, or some thing that people might be familiar with?

Make sure the HSE crystal turns on, and if it comes form an external source (DISCO/NUCLEO) that the HSE Bypass is set.

Add some instrumentation so you can see what's happening internally. Use RCC_GetClocksFreq() to figure out what speed you are actually running at.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..