cancel
Showing results for 
Search instead for 
Did you mean: 

Input Capture on both edges to detect signal duration

mubinicyer
Associate II
Posted on May 17, 2014 at 00:52

Hi,

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

initialization:

#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); 
TIM_ITConfig(TIMER_NR, TIM_IT_CC3 | TIM_IT_CC4, ENABLE); 
TIM_Cmd(TIMER_NR, ENABLE); 

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?
 Thanks.

2 REPLIES 2
Posted on May 17, 2014 at 01:15

How can I count the duration of ''1''s and ''0''s, since there is only one counter register?

There are however FOUR latches. The counter is the time base, you latch the count into one latch on the rising edge, and the falling edge on the second latch.

Interrupt on CC1 (Rising)

Current CC1 - Last CC1 = Period in ticks of the Time Base

Current CC1 - Current CC2 = Low time in ticks of the Time Base

Current CC2 - Last CC1 = High time in ticks of the Time Base
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mubinicyer
Associate II
Posted on May 18, 2014 at 00:37

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6f5&d=%2Fa%2F0X0000000bsf%2F.0jLqn4KVfM3UYklXhaQrowewAmhblh7GDdlh_5BsvU&asPdf=false