AnsweredAssumed Answered

Input Capture on both edges to detect signal duration

Question asked by mubin.icyer on May 17, 2014
Latest reply on May 18, 2014 by mubin.icyer
I am trying to get the duration of a signal with input capture of timer. I have two  signals, which are comming from another chip, CLK and OUT. CLK is 125 KHz and OUT is variable between ~3.5KHz and 4KHz. I clocked the timer4 from clk input, which is connected to CH1 of the timer. OUT is connected to CH3 of the timer4. I want to count the duration of the signal on high level and on low level. In the standard library example it measures only one edge. I set the timer on both edges, one is direct, other is indirect. How can I count the duration of "1"s and "0"s, since there is only one counter register?
chip: STM32F105

#define TIMER_NR TIM4
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBase_InitStructure.TIM_Period = 65535;
TIM_TimeBase_InitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInit(TIMER_NR, &TIM_TimeBase_InitStructure);
TIM_TIxExternalClockConfig(TIMER_NR, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIMER_NR, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI; //ch3
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIMER_NR, &TIM_ICInitStructure);

interrupt handler:

void TIM4_IRQHandler(void)
    static uint16_t value = 0, old_val = 0, diff = 0, sync = 0, insync = 0;
    static uint8_t input_pin, bitcount = 0;
    if(TIM_GetITStatus(TIMER_NR, TIM_IT_CC3) == SET)
        TIM_ClearITPendingBit(TIMER_NR, TIM_IT_CC3);
        IC4ReadValue1 = TIM_GetCapture3(TIMER_NR);
    if(TIM_GetITStatus(TIMER_NR, TIM_IT_CC4) == SET)
        TIM_ClearITPendingBit(TIMER_NR, TIM_IT_CC4);
        IC4ReadValue2 = TIM_GetCapture4(TIMER_NR);

If i subtract both values IC4ReadValue2 and IC4ReadValue1 I can get the delta for high level, how can I get low level as well?