cancel
Showing results for 
Search instead for 
Did you mean: 

Complementary PWM not working for STM8S105 MCU

CKhan.1
Associate

Here is the initialization code...

void InitPwm(void)

{

TIM1_DeInit();

TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_CENTERALIGNED1, 800, 0);

TIM1_OC1Init(TIM1_OCMODE_PWM1, 

TIM1_OUTPUTSTATE_ENABLE, 

TIM1_OUTPUTNSTATE_ENABLE, 

100,

TIM1_OCPOLARITY_HIGH,

TIM1_OCNPOLARITY_LOW,

TIM1_OCIDLESTATE_RESET, 

TIM1_OCNIDLESTATE_SET);

 

TIM1_OC2Init(TIM1_OCMODE_PWM1, 

TIM1_OUTPUTSTATE_ENABLE,  

TIM1_OUTPUTNSTATE_ENABLE, 

200,

TIM1_OCPOLARITY_HIGH,  

TIM1_OCNPOLARITY_LOW,

TIM1_OCIDLESTATE_RESET, 

TIM1_OCNIDLESTATE_SET);

 

TIM1_OC3Init(TIM1_OCMODE_PWM1, 

TIM1_OUTPUTSTATE_ENABLE, 

TIM1_OUTPUTNSTATE_ENABLE, 

300,

TIM1_OCPOLARITY_HIGH,

TIM1_OCNPOLARITY_LOW,

TIM1_OCIDLESTATE_RESET, 

TIM1_OCNIDLESTATE_SET); 

TIM1->DTR = 0;

TIM1_CtrlPWMOutputs(ENABLE);

TIM1_ClearITPendingBit(0xFF);

TIM1_ITConfig(TIM1_IT_UPDATE | TIM1_IT_BREAK, ENABLE);

TIM1_Cmd(ENABLE);

}

@near @interrupt void PWM_Isr (void)

{

if ((TIM1->SR1 & TIM1_IT_BREAK) == TIM1_IT_BREAK) //Break Input active

{

TIM1_ClearITPendingBit(TIM1_IT_BREAK);

}

if ((TIM1->SR1 & TIM1_IT_UPDATE) == TIM1_IT_UPDATE) //Overflow/Underflow

{

//duty change code to go here

}

}

This code compiles and PWM is visible on High side channel (OC1, OC2, OC3). However, the PWM on Low side channel (OC1N, OC2N, OC3N) keeps on varying. Sometimes, it's exactly complementary to it's High channel but sometimes it fluctuate randomly.

Please let me know how to solve this problem.

Waveforms are attached as images...

Green - OC2 pwm, High side channel, configured as active high - waveform As expected

Pink - OC2N pwm, Low side channel, configured as active low - waveform as expected in ExpectedPwm.jpg, and random/varied/unexpected in UnexpectedPwm.jpg

0 REPLIES 0