Author Topic: Measuring 3 Channel PWM Signal Duty Cycle  (Read 2331 times)

0 Members and 1 Guest are viewing this topic.

Offline PrayerTopic starter

  • Contributor
  • Posts: 12
Measuring 3 Channel PWM Signal Duty Cycle
« on: March 06, 2014, 04:51:07 pm »
Hi All

I want to measure PWM output of RGB controller with STM8S105 @16Mhz internal oscillator. The PWM signals are phase align and PWM signal frequency is 500 Hz.. PWM signal duty is changing between 0% and 100%. System start 0%, 100% or any value between 0% and 100%.


I connected TIM1 Channel1 and Channel2 to Red_PWM, TIM1 Channel3 and Channel 4 to Green_PWM channels. I can catch all values when I apply a input PWM. If I apply second or third channel or all of them, I can not catch 2-3% PWM each channel. I read random variables.


I think that is cause from resetting TIM1 counter (TIM1_SetCounter(0x00);) but I didn't handle. Could anyone suggest a solution ?

Code: [Select]
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
    if((TIM1->SR1 & TIM1_FLAG_CC1) == TIM1_FLAG_CC1)
    {
        TIM1_ClearFlag(TIM1_FLAG_CC1);
        if(bCapture1_Rising == FALSE)
        {
            bCapture_Timer_Reset = TRUE;
            bCapture1_Rising = TRUE;
        }
        else
        {
            wRed_Period = TIM1_GetCapture1();
            wRed_PWM = (100 * (DWORD)wRed_On_Time) / wRed_Period;
            bCapture_Timer_Reset = TRUE;
            wRed_OVF_Flag = 0;
        }
    }
     
    if((TIM1->SR1 & TIM1_FLAG_CC2) == TIM1_FLAG_CC2)
    {
        TIM1_ClearFlag(TIM1_FLAG_CC2);
        wRed_On_Time = TIM1_GetCapture2();
    }
     
    if((TIM1->SR1 & TIM1_FLAG_CC3) == TIM1_FLAG_CC3)
    {
        TIM1_ClearFlag(TIM1_FLAG_CC3);
        if(bCapture2_Rising == FALSE)
        {
            bCapture_Timer_Reset = TRUE;
            bCapture2_Rising = TRUE;
        }
        else
        {
            wGreen_Period = TIM1_GetCapture3();
            wGreen_PWM = (100 * (DWORD)wGreen_On_Time) / wGreen_Period;
            bCapture_Timer_Reset = TRUE;
             
        }
    }
     
    if((TIM1->SR1 & TIM1_FLAG_CC4) == TIM1_FLAG_CC4)
    {
        TIM1_ClearFlag(TIM1_FLAG_CC4);
        wGreen_On_Time = TIM1_GetCapture4();
    } 
 
    if(bCapture_Timer_Reset == TRUE)
    {
        bCapture_Timer_Reset = FALSE;
        TIM1_SetCounter(0x00);
    }
}
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Measuring 3 Channel PWM Signal Duty Cycle
« Reply #1 on: April 12, 2014, 06:45:17 pm »
Then don't reset the timer, there is no need for that.
Code: [Select]
uint32_t a = capture;
uint32_t b = prev_capture;
uint32_t delta;
const uint32_t max = 0xFFFFFFFF;
if(a > b)
  delta = a - b;
else
  delta = max - b + a;
« Last Edit: April 12, 2014, 06:48:20 pm by Jeroen3 »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf