Skip to main content
psandesh007
Associate II
June 24, 2013
Question

timer capture compare

  • June 24, 2013
  • 15 replies
  • 2932 views
Posted on June 24, 2013 at 15:22

Im going to capture simultaneously 4 input signals(same inputs for 4 channels) using Timer 8 by enabling all the interrupts in TIM->CCER register .. But it was not capturing correctly and im getting  channel number  2 and 4 continuously compared to 1 and 3 channels while im giving same frequency for all the channels..

please help me to solve this problem..
    This topic has been closed for replies.

    15 replies

    Tesla DeLorean
    Guru
    June 24, 2013
    Posted on June 24, 2013 at 16:49

    On which board/part?

    Make sure you aren't using PWM Input Capture mode.

    Regular input capture should be able to latch the time base independently on each of the 4 channels. The CCR registers are latches.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    psandesh007
    Associate II
    June 25, 2013
    Posted on June 25, 2013 at 09:04

    Thank you for your reply... I am using STM32F103ZET6 for frequency capturing. Is it possible to capture 4 channels of Timer8 at once by enabling all the 4 interrupts at same time. 

    psandesh007
    Associate II
    June 25, 2013
    Posted on June 25, 2013 at 10:04

    Im finding only TIM1_CC_IRQ Handler and TIM8_CC_IRQ Handler for frequency capturing in STM32F103ZET6, and im trying to do the same in TIM3 and TIM4 by configuring same as the TIM1 and TIM8,  but I don't know how to capture the frequency using these timers, can u please help me to overcome this problem..

    Tesla DeLorean
    Guru
    June 25, 2013
    Posted on June 25, 2013 at 14:09

    TIM3_IRQHandler, the interrupts all share a common handler.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    June 26, 2013
    Posted on June 26, 2013 at 14:25

    Not your part, but perhaps illustrative. [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/PWM%20Input%20capture%20doesnt%20work%20on%20Ch34%20pair&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx&currentviews=32]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Discovery%2fPWM%20Input%20capture%20doesnt%20work%20on%20Ch34%20pair&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https%3A%2F%2Fmy.st.com%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FAllItems.aspx¤tviews=32

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    psandesh007
    Associate II
    June 27, 2013
    Posted on June 27, 2013 at 09:54

    Thank you.. (STM32F103ZET6) Now it is capturing 6 channels 4 from TIM8(CH1 & CH2 & CH3 & CH4) and 2 from TIM1(CH1 & CH4) and if i gave 6th channels TIM1_CH1 or TIM1_CH4 then it will fluctuates all the other 5 channels .. And also if i increase the frequency of all channels expect channels CH2 and CH3 of TIM8,then it was capturing the lower frequencies of all other channels , if im keep on increasing TIM8_CH1 and TIM8_CH4 and TIM1_CH1, then TIM8_CH2 and TIM8_CH3 frequencies are fluctuating more... please help me to solve this problems.... below is the code

    void TIM8_CC_IRQHandler(void)

    {

    if((TIM8->SR & 0x2)!=0)// && (TIM8->SR & 0x1000)!=0)

        {

            TIM8->SR&=0xEFFC;

            ch1_CAP=TIM8->CCR1;

            ch1_CAP2=TIM5->CNT;

            if(ch1_CAP2>=ch1_CAP3)

            {

                gl85_lsb_captval_CH1=((65535*ch1_CAP2)+ch1_CAP)-((65535*ch1_CAP3)+ch1_CAP1);

            }

            else

            {

                   ch1_CAP3=65535-ch1_CAP3;

                gl85_lsb_captval_CH1=(65535*(ch1_CAP2+ch1_CAP3))+(ch1_CAP-ch1_CAP1);

            }

            ch1_CAP1=TIM8->CCR1;

            ch1_CAP3=TIM5->CNT;

            No_of_Captures85_ch1++;

            if(No_of_Captures85_ch1>1)

            {

                No_of_Captures85_ch1=0;

                gl_capture_ch85_flag=1;

                TIM8_CH1_CAP_FLAG=1;

            }

        }

        else if((TIM8->SR & 0x4)!=0)// && (TIM8->SR & 0x0800)!=0)

        {

            TIM8->SR&=0xF7FA;

            ch2_CAP=TIM8->CCR2;

            ch2_CAP2=TIM5->CNT;

            if(ch2_CAP2>=ch2_CAP3)

            {

                gl85_lsb_captval_CH2=((65535*ch2_CAP2)+ch2_CAP)-((65535*ch2_CAP3)+ch2_CAP1);

            }

            else

            {

                   ch2_CAP3=65535-ch2_CAP3;

                gl85_lsb_captval_CH2=(65535*(ch2_CAP2+ch2_CAP3))+(ch2_CAP-ch2_CAP1);

            }

            ch2_CAP1=TIM8->CCR2;

            ch2_CAP3=TIM5->CNT;

            No_of_Captures85_ch2++;

            if(No_of_Captures85_ch2>2)

            {

                No_of_Captures85_ch2=0;

                gl_capture_ch85_flag=1;

                TIM8_CH2_CAP_FLAG=1;

            }

        }

        else if((TIM8->SR & 0x8)!=0)// && (TIM8->SR & 0x0400)!=0)

        {

            TIM8->SR&=0xFBF6;

            ch3_CAP=TIM8->CCR3;

            ch3_CAP2=TIM5->CNT;

            if(ch3_CAP2>=ch3_CAP3)

            {

                gl85_lsb_captval_CH3=((65535*ch3_CAP2)+ch3_CAP)-((65535*ch3_CAP3)+ch3_CAP1);

            }

            else

            {

                   ch3_CAP3=65535-ch3_CAP3;

                gl85_lsb_captval_CH3=(65535*(ch3_CAP2+ch3_CAP3))+(ch3_CAP-ch3_CAP1);

            }

            ch3_CAP1=TIM8->CCR3;

            ch3_CAP3=TIM5->CNT;

            No_of_Captures85_ch3++;

            if(No_of_Captures85_ch3>2)

            {

                No_of_Captures85_ch3=0;

                gl_capture_ch85_flag=1;

                TIM8_CH3_CAP_FLAG=1;

            }    

        }

        else if((TIM8->SR & 0x10)!=0)// && (TIM8->SR & 0x0200)!=0)

        {

            TIM8->SR&=0xFDEE;

            ch4_CAP=TIM8->CCR4;

            ch4_CAP2=TIM5->CNT;

            if(ch4_CAP2>=ch4_CAP3)

            {

                gl85_lsb_captval_CH4=((65535*ch4_CAP2)+ch4_CAP)-((65535*ch4_CAP3)+ch4_CAP1);

            }

            else

            {

                   ch4_CAP3=65535-ch4_CAP3;

                gl85_lsb_captval_CH4=(65535*(ch4_CAP2+ch4_CAP3))+(ch4_CAP-ch4_CAP1);

            }

            ch4_CAP1=TIM8->CCR4;

            ch4_CAP3=TIM5->CNT;

            No_of_Captures85_ch4++;

            if(No_of_Captures85_ch4>3)

            {

                No_of_Captures85_ch4=0;

                gl_capture_ch85_flag=1;

                TIM8_CH4_CAP_FLAG=1;

            }

        }

    }

    Tesla DeLorean
    Guru
    June 27, 2013
    Posted on June 27, 2013 at 13:58

    I'm not sure I'm invested in wading through that.

     

    What frequency ranges are we talking about?

    Don't use TIMx->SR &=  this is not how to clear status and causes hazards.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    psandesh007
    Associate II
    June 28, 2013
    Posted on June 28, 2013 at 07:14

    Thank u Clive now the code is working and we are talking about 5Hz-1KHz by cascading the timers.. Now also im clearing the status register like TIMx->SR&= .. But before im clearing 'Status Register' by writting '0' into it, that was clearing all the interrupts flag so i used this way.. Can u tell me the other method of clearing the status registers..

    Tesla DeLorean
    Guru
    June 28, 2013
    Posted on June 28, 2013 at 13:51

    if (TIM8->SR & 2) // TIM_IT_CC1

        {

            TIM8->SR = ~2;

    ..
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    psandesh007
    Associate II
    June 29, 2013
    Posted on June 29, 2013 at 07:42

    Thanks for your support..

    Frequency is still fluctuating little if i use that math ..

            ch4_CAP=TIM3->CCR1;

            ch4_CAP2=TIM1->CNT;

            if(ch4_CAP2>=ch4_CAP3)

            {

                gl31_captval_CH4=((65535*ch4_CAP2)+ch4_CAP)-((65535*ch4_CAP3)+ch4_CAP1);

            }

            else

            {

                   ch4_CAP3=65535-ch4_CAP3;

                gl31_captval_CH4=(65535*(ch4_CAP2+ch4_CAP3))+(ch4_CAP-ch4_CAP1);

            }

            ch4_CAP1=TIM3->CCR1;

            ch4_CAP3=TIM1->CNT

    Can you Help me to do the math for both timers..