cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate PWM output using timers on PB0 and PB1

marco2
Associate III
Posted on January 21, 2016 at 15:58

Hi all,

I have the stm32F429 micro and I need to generate 2 PWM Output (see below) using pin PB0 and PB1. I tried with toggle mode and TM3 on ch3 and 4, but the duty is 50/50 and is not good. There is a phase shift of 2,4us. Do you have any suggestions?

0690X00000605KoQAI.png

stm32f429

11 REPLIES 11
marco2
Associate III
Posted on February 05, 2016 at 09:56

Thanks to all for your precious suggestions. Below the code to generate PWM as the picture posted above. Little trick on TIM1_PWMOut_Disable (maybe yet a little settings problem??)

#define CMD_SIGNAL_HZ    208000  //200..210 kHz

void TIM1_IOConfig(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

                         

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

   

  GPIO_Init(GPIOB, &GPIO_InitStructure);

    

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM1);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM1);

}

void TIM1_Configuration(void)

{

uint16_t TimerPeriod = 0;

  TIM_DeInit(TIM1);

 

  TimerPeriod = ((SystemCoreClock / 2) / CMD_SIGNAL_HZ) - 1;

  /* Time Base configuration */

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;

  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* Channel 3 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 = (uint16_t) (((uint32_t) 66 * (TimerPeriod - 1)) / 100);

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;

  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  /* 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 = (uint16_t) (((uint32_t) 33 * (TimerPeriod - 1)) / 100);

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

 

  /* Automatic Output enable, Break, dead time and lock configuration*/

  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;

  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;

  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;

  TIM_BDTRInitStructure.TIM_DeadTime = 0;

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

}

void TIM1_PWMOut_Enable(void)

{

  TIM_Cmd(TIM1, ENABLE);

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

}

void TIM1_PWMOut_Disable(void)

{

  TIM_Cmd(TIM1, DISABLE);

  TIM_OC3Init(TIM1, &TIM_OCInitStructure); //trick!!

  TIM_DeInit(TIM1);

}
marco2
Associate III
Posted on February 12, 2016 at 17:24

Unfortunately the steps to disable PWM is not exactly what I want.

How can disable PWM at logic low?