Showing results for 
Search instead for 
Did you mean: 

stm32f103 timer input capture



I am trying to read some square signal with timer input capture for both falling and rising edge times. When i start the code, the interrupt routine works once, even if there is no signal. I am using the code below for setup timer and interrupt routine. Any advise ?

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    //GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);

    // Time base configuration
    TIM_TimeBaseStructure.TIM_Period = 0xFFFE;
    TIM_TimeBaseStructure.TIM_Prescaler = 15; 
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

    // Input capture configuration
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; 
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0; 
    TIM_ICInit(TIM1, &TIM_ICInitStructure);

    // Enable the TIM1 global interrupt
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    TIM_Cmd(TIM1, ENABLE);
void TIM1_CC_IRQHandler(void) 
    if (TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET)

            if(capture_value == 0)
                capture_value = 1;
                signalArray[signalIndex++] = TIM_GetCapture3(TIM1);

                TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling ;
                TIM_ICInit(TIM1, &TIM_ICInitStructure);
                capture_value = 0;
                signalArray[signalIndex++] = TIM_GetCapture3(TIM1);
                TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
                TIM_ICInit(TIM1, &TIM_ICInitStructure);
        TIM_ClearITPendingBit(TIM1, TIM_IT_CC3);


What is this, SPL?

> When i start the code, the interrupt routine works once

How do you know?

And do you say the interrupt is never called anymore?

What is the input signal? How did you verify it really is connected to physical pin PA10?

Read out and check/post TIM registers content, possibly at various points.of execution. Note, that debugging is intrusive and reading TIMx_CCRx clears the respective TIMx_SR.CCxIF flags.




Yes, i use SPL.

When i debug the code and put a break point in interrupt, it works there at start up.

I connect an external square signal to PA10 pin, it works normally when i start the external signal, but at start up there is signal, and i can see interrupt works once. 

when i create a variable and increase it one step in interrupt, i can see it increase once while not debugging.

one more thing, when i change the timer polarity for "both edge", it has no effect for reading the signal, i don't understand this, but maybe it is related to F1 series( i am not sure). 

Place breakpoints somewhere in the middle of timer initialization and observe, whether the respective TIMx_SR.CCxIF flag is set (note, that as I've said above, reading of TIMx_CCRx register - even with debugger - clears this flag). Then proceed bisecting that code until you find, at which point is it set.


What is  SPL?

Shakespeare Programming Language (SPL) ??

If you feel a post has answered your question, please click "Accept as Solution".

Standard Peripheral Library, the predecessor to Cube.


Thx, just forgot the old lib, I used on the primer, feels like stone age....

If you feel a post has answered your question, please click "Accept as Solution".