2024-07-18 07:38 AM
Hi,
I basically have two PWM signals going into two pin that are set to be TIM4_CH1 and TIM4_CH2.
I setup both channels in slave mode and enabled the input capture mode on Rising mode.
When I enable only channel 1 and the code below get executed I retrieve the correct CCR1 values and consecutively I calculate the correct duty cycle.
Problem: When I enable both channels I get the same CCR1 and CCR2 value, all the time, so the duty cycle is always 100%
Input capture mode on "Both_Edges" mode work strangely because instead of getting the rising and falling edge captures consecutively I get them randomly: sometimes I just get the falling, sometimes only the rising and most of the time random pattern ( ex. rising, rising, falling, rising). So I opted to switch edge detection mode everytime I capture the value.
void TIM4_IRQ_PWM(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) {
tmyPWMInput *pPWM4_1 = &myCTRL.myHW.myPortDCfg.myPWM4_1; //PIN12
// Clear TIM8 Capture Compare1 interrupt pending bit
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
if ( is_rising_ch1 )
{
pPWM4_1->FullPeriod = TIM_GetCapture1(TIM4);
TIM_CH1_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_CH1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_CH1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_CH1_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_CH1_ICInitStructure);
is_rising_ch1 = 0;
}
else
{
pPWM4_1->HighPeriod = TIM_GetCapture1(TIM4);
TIM_CH1_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_CH1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_CH1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_CH1_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_CH1_ICInitStructure);
is_rising_ch1 = 1;
}
//To prevent division by 0 on duty cycle calculation
if ( pPWM4_1->FullPeriod != 0 )
myPWMCalc(pPWM4_1);
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET) {
tmyPWMInput *pPWM4_2 = &myCTRL.myHW.myPortDCfg.myPWM4_2; //PIN13
// Clear TIM8 Capture Compare1 interrupt pending bit
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
if ( is_rising_ch2 )
{
pPWM4_2->FullPeriod = TIM_GetCapture2(TIM4);
TIM_CH2_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_CH2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_CH2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_CH2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_CH2_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_CH2_ICInitStructure);
is_rising_ch2 = 0;
}
else
{
pPWM4_2->HighPeriod = TIM_GetCapture2(TIM4);
TIM_CH2_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_CH2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_CH2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_CH2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_CH2_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_CH2_ICInitStructure);
is_rising_ch2 = 1;
}
//To prevent division by 0 on duty cycle calculation
if ( pPWM4_2->FullPeriod != 0 )
myPWMCalc(pPWM4_2);
}
}