cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with PWM

florianaugustin9
Associate II
Posted on October 27, 2012 at 20:36

Dear STM-Experts,

i tried my first PWM with the STM32F4Discovery. It should create a 7 MHz PWM with 50 % duty-cycle. But i can only get a changing 8 bzw 6 Mhz Pwm like shown here:

0690X0000060526QAA.png

Here is the code (Picked up and then changed from STM Std. Peripheral Libraries):

 //GPIOE Clocks enable

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE , ENABLE);

  //GPIOE settings

  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP ;

  //GPIOE Configuration: Channel 2 as alternate function push-pull

  GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_11 ;

  GPIO_Init(GPIOE, &GPIO_InitStructure2);

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);

  //TIM1 clock enable

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

  //Time Base configuration

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseStructure.TIM_Period = 23;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  //Channel 2 Configuration in PWM mode

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 11;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OC2Init(TIM1, &TIM_OCInitStructure);

  //TIM1 counter enable

  TIM_Cmd(TIM1, ENABLE);

  //TIM1 Main Output Enable

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

Thank you a lot for your help!

Florian
3 REPLIES 3
Posted on October 27, 2012 at 22:50

TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 24 - 1; // 168 / 24 = 7 MHz, confirm system clock
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//Channel 2 Configuration in PWM mode
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 24 / 2; // 50/50 duty ie 12 cycles
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
//TIM1 Main Output Enable
TIM_CtrlPWMOutputs(TIM1, ENABLE);
//TIM1 counter enable
TIM_Cmd(TIM1, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
florianaugustin9
Associate II
Posted on October 27, 2012 at 23:40

I used the formulas from the STM example, adapted to my values:

 /* Compute the value to be set in ARR regiter to generate signal frequency at 7 Mhz*/

 TimerPeriod = (SystemCoreClock / 7000000 ) - 1;

 /* Compute CCR2 value to generate a duty cycle at 50% for channel 2 and 2N */

 Channel2Pulse = (uint16_t) (((uint32_t)5 * (TimerPeriod - 1)) / 10);

And you are right, 168 Mhz clock. But thats not the Thing, I don't know why the PWM is looking weird like it does :(

Posted on October 28, 2012 at 02:14

The pulse width computation is definitely wrong.

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