cancel
Showing results for 
Search instead for 
Did you mean: 

Generating complementary PWM with deadtime using standard peripheral library

srinath mallikarjunan
Associate II

I would like to generate Complementary PWM's with an adjustable deadtime on TIM1,2,3 channels. MCU is STM32F303CB

I understand that this is done by setting the BDTR registers, but can't find a suitable reference/code snippet.

This is what i have so far..

void pwmTimeBase(const TIM_TypeDef *tim, uint32_t period, uint16_t prescaler) {

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  // Time base configuration

  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Period = period - 1;

  TIM_TimeBaseStructure.TIM_Prescaler = prescaler - 1;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;//WL:CounterMode_Up

  TIM_TimeBaseInit((TIM_TypeDef *)tim, &TIM_TimeBaseStructure);

}

void pwmOCInit(const TIM_TypeDef *tim, uint16_t channel, uint32_t inititalValue) {

  TIM_OCInitTypeDef TIM_OCInitStructure;

  TIM_OCStructInit(&TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//WL:clear on compare match

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//previously Disable

  TIM_OCInitStructure.TIM_Pulse = inititalValue;

  switch (channel) {

    case TIM_Channel_1:

      TIM_OC1Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);

      break;

    case TIM_Channel_2:

      TIM_OC2Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);

      break;

    case TIM_Channel_3:

      TIM_OC3Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);

      break;

    case TIM_Channel_4:

      TIM_OC4Init((TIM_TypeDef *)tim, &TIM_OCInitStructure);

      break;

  }

}

Since i want to generate symmetric PWM's, is the above code correct ?

0 REPLIES 0