cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Input Capture on Both Rising and Falling Edge

psandesh007
Associate II
Posted on June 06, 2014 at 13:14

Hi all,

           Presently i am capturing input signal only on its rising edge and also i can capture the same signal only on its falling edge. But i need to capture on both rising and falling edges, please let me know i can or i cannot.

Part No: STM32F103RCT7

Cascaded Timers: Timer 1 and Timer 2 
5 REPLIES 5
Posted on June 06, 2014 at 14:04

You should be able to set up two channels in the same timer, one in direct mode to catch one edge, and the second in indirect mode (ie CH2 using TI1) to catch the other edge.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
psandesh007
Associate II
Posted on June 06, 2014 at 14:24

Thanks for your reply, 

Actually i used all other channels for some other purpose, can i capture using only one channel by changing my timer configuration. If not can u suggest me other part number which can able to capture on both edges. Timers configuration is below,

/*-------------------TIMER1_CONFIGURATION AS MASTER FOR CH1,CH2,CH3-------------------*/

void sar_Timer1_Master(void)   

TIM_ICInitTypeDef TIM_ICInitstructure;

/*This function is used to enable TIMER1 clock

[RCC->APB2ENR----- WRITE BIT NO's 11 TO 1]*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

/*This function is used to configure TIMER1 prescaler

[TIM1->PSC----- 0x0000]*/

TIM_PrescalerConfig(TIM1,0X0000,TIM_PSCReloadMode_Update);

/*This function is used to load TIMER1 Auto Reload Register value

[TIM1->ARR----- 0xFFFF]*/

TIM_SetAutoreload(TIM1,0xFFFF);

/*This function is used to enable TIMER1 Auto Reload Preload

[TIM1->CR1----- WRITE BIT NO's 7 TO 1]*/

TIM_ARRPreloadConfig(TIM1,ENABLE);

/*This functional structure is used to configure TIMER1 capture channel-1

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM1->CCMR1------ WRITE BIT NO's 1,0 TO 0,1]

[TIM1->CCER------ WRITE BIT NO's 1 TO 0]*/

    TIM_ICInitstructure.TIM_Channel=(TIM_Channel_1);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM1,&TIM_ICInitstructure);

TIM_SetIC1Prescaler(TIM1,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER1 capture channel-2

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM1->CCMR1------ WRITE BIT NO's 9,8 TO 0,1]

[TIM1->CCER------ WRITE BIT NO's 5 TO 0]*/

TIM_ICInitstructure.TIM_Channel=(TIM_Channel_2);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM1,&TIM_ICInitstructure);

TIM_SetIC2Prescaler(TIM1,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER1 capture channel-3

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM1->CCMR2------ WRITE BIT NO's 1,0 TO 0,1]

[TIM1->CCER------ WRITE BIT NO's 9 TO 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_ICFilter=0x3;

TIM_ICInit(TIM1,&TIM_ICInitstructure);

TIM_SetIC3Prescaler(TIM1,TIM_ICPSC_DIV1);

/*This functional structure is used to enable TIMER1 capture channel-1 interrupt for Input capture

[TIM1->CCER------ WRITE BIT NO's 0 TO 1]*/

TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE);

/*This functional structure is used to enable TIMER1 capture channel-2 interrupt for Input capture

[TIM1->CCER------ WRITE BIT NO's 4 TO 1]*/

TIM_ITConfig(TIM1,TIM_IT_CC2,ENABLE);

/*This functional structure is used to enable TIMER1 capture channel-3 interrupt for Input capture

[TIM1->CCER------ WRITE BIT NO's 8 TO 1]*/

TIM_ITConfig(TIM1,TIM_IT_CC3,ENABLE);

TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update);

}

/*-------------------TIMER2_CONFIGURATION AS SLAVE FOR CH1,CH2,CH3-------------------*/

void sar_Timer2_Slave(void)

{

TIM_ICInitTypeDef TIM_ICInitstructure;

/*This function is used to enable TIMER2 clock

[RCC->APB1ENR----- WRITE BIT NO's 0 TO 1]*/

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

/*This function is used to configure TIMER2 prescaler

[TIM2->PSC----- 0x0000]*/

TIM_PrescalerConfig(TIM2,0X0000,TIM_PSCReloadMode_Update);

/*This function is used to load TIMER2 Auto Reload Register value

[TIM2->ARR----- 0xFFFF]*/

TIM_SetAutoreload(TIM2,0xFFFF);

/*This function is used to enable TIMER2 Auto Reload Preload

[TIM2->CR1----- WRITE BIT NO's 7 TO 1]*/

TIM_ARRPreloadConfig(TIM2,ENABLE);

/*This function is used to enable TIMER2 to trigger when COUNTER of TIMER1 is Overflow

[TIM1->SMCR----- WRITE BIT NO's 7 TO 1]*/

TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_External1);

/*This function is used to configure TIMER2 as SLAVE for TIMER1

[TIM1->SMCR----- WRITE BIT NO's 2,1,0 TO 0,0,0]*/

TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ITR0);

    /*This functional structure is used to configure TIMER2 capture channel-1

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM2->CCMR1------ WRITE BIT NO's 1,0 TO 0,1]

[TIM2->CCER------ WRITE BIT NO's 1 TO 0]*/

    TIM_ICInitstructure.TIM_Channel=(TIM_Channel_1);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM2,&TIM_ICInitstructure);

TIM_SetIC1Prescaler(TIM2,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER2 capture channel-2

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM2->CCMR1------ WRITE BIT NO's 9,8 TO 0,1]

[TIM2->CCER------ WRITE BIT NO's 5 TO 0]*/

TIM_ICInitstructure.TIM_Channel=(TIM_Channel_2);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM2,&TIM_ICInitstructure);

TIM_SetIC2Prescaler(TIM2,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER2 capture channel-3

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM2->CCMR2------ WRITE BIT NO's 1,0 TO 0,1]

[TIM2->CCER------ WRITE BIT NO's 9 TO 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_ICFilter=0x3;

TIM_ICInit(TIM2,&TIM_ICInitstructure);

TIM_SetIC3Prescaler(TIM2,TIM_ICPSC_DIV1);

/*This functional structure is used to enable TIMER2 capture channel-1 interrupt for Input capture

[TIM2->CCER------ WRITE BIT NO's 0 TO 1]*/

TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);

/*This functional structure is used to enable TIMER2 capture channel-2 interrupt for Input capture

[TIM2->CCER------ WRITE BIT NO's 4 TO 1]*/

TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE);

/*This functional structure is used to enable TIMER2 capture channel-3 interrupt for Input capture

[TIM2->CCER------ WRITE BIT NO's 8 TO 1]*/

TIM_ITConfig(TIM2,TIM_IT_CC3,ENABLE);

/*This function is used to enable TIMER2 counter*/

TIM_Cmd(TIM2,ENABLE);

/*This function is used to enable TIMER1 counter*/

TIM_Cmd(TIM1,ENABLE);

}
psandesh007
Associate II
Posted on June 06, 2014 at 14:24

Thanks for your reply, 

Actually i used all other channels for some other purpose, can i capture using only one channel by changing my timer configuration. If not can u suggest me other part number which can able to capture on both edges. Timers configuration is below,

/*-------------------TIMER1_CONFIGURATION AS MASTER FOR CH1,CH2,CH3-------------------*/

void sar_Timer1_Master(void)   

TIM_ICInitTypeDef TIM_ICInitstructure;

/*This function is used to enable TIMER1 clock

[RCC->APB2ENR----- WRITE BIT NO's 11 TO 1]*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

/*This function is used to configure TIMER1 prescaler

[TIM1->PSC----- 0x0000]*/

TIM_PrescalerConfig(TIM1,0X0000,TIM_PSCReloadMode_Update);

/*This function is used to load TIMER1 Auto Reload Register value

[TIM1->ARR----- 0xFFFF]*/

TIM_SetAutoreload(TIM1,0xFFFF);

/*This function is used to enable TIMER1 Auto Reload Preload

[TIM1->CR1----- WRITE BIT NO's 7 TO 1]*/

TIM_ARRPreloadConfig(TIM1,ENABLE);

/*This functional structure is used to configure TIMER1 capture channel-1

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM1->CCMR1------ WRITE BIT NO's 1,0 TO 0,1]

[TIM1->CCER------ WRITE BIT NO's 1 TO 0]*/

    TIM_ICInitstructure.TIM_Channel=(TIM_Channel_1);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM1,&TIM_ICInitstructure);

TIM_SetIC1Prescaler(TIM1,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER1 capture channel-2

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM1->CCMR1------ WRITE BIT NO's 9,8 TO 0,1]

[TIM1->CCER------ WRITE BIT NO's 5 TO 0]*/

TIM_ICInitstructure.TIM_Channel=(TIM_Channel_2);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM1,&TIM_ICInitstructure);

TIM_SetIC2Prescaler(TIM1,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER1 capture channel-3

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM1->CCMR2------ WRITE BIT NO's 1,0 TO 0,1]

[TIM1->CCER------ WRITE BIT NO's 9 TO 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_ICFilter=0x3;

TIM_ICInit(TIM1,&TIM_ICInitstructure);

TIM_SetIC3Prescaler(TIM1,TIM_ICPSC_DIV1);

/*This functional structure is used to enable TIMER1 capture channel-1 interrupt for Input capture

[TIM1->CCER------ WRITE BIT NO's 0 TO 1]*/

TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE);

/*This functional structure is used to enable TIMER1 capture channel-2 interrupt for Input capture

[TIM1->CCER------ WRITE BIT NO's 4 TO 1]*/

TIM_ITConfig(TIM1,TIM_IT_CC2,ENABLE);

/*This functional structure is used to enable TIMER1 capture channel-3 interrupt for Input capture

[TIM1->CCER------ WRITE BIT NO's 8 TO 1]*/

TIM_ITConfig(TIM1,TIM_IT_CC3,ENABLE);

TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update);

}

/*-------------------TIMER2_CONFIGURATION AS SLAVE FOR CH1,CH2,CH3-------------------*/

void sar_Timer2_Slave(void)

{

TIM_ICInitTypeDef TIM_ICInitstructure;

/*This function is used to enable TIMER2 clock

[RCC->APB1ENR----- WRITE BIT NO's 0 TO 1]*/

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

/*This function is used to configure TIMER2 prescaler

[TIM2->PSC----- 0x0000]*/

TIM_PrescalerConfig(TIM2,0X0000,TIM_PSCReloadMode_Update);

/*This function is used to load TIMER2 Auto Reload Register value

[TIM2->ARR----- 0xFFFF]*/

TIM_SetAutoreload(TIM2,0xFFFF);

/*This function is used to enable TIMER2 Auto Reload Preload

[TIM2->CR1----- WRITE BIT NO's 7 TO 1]*/

TIM_ARRPreloadConfig(TIM2,ENABLE);

/*This function is used to enable TIMER2 to trigger when COUNTER of TIMER1 is Overflow

[TIM1->SMCR----- WRITE BIT NO's 7 TO 1]*/

TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_External1);

/*This function is used to configure TIMER2 as SLAVE for TIMER1

[TIM1->SMCR----- WRITE BIT NO's 2,1,0 TO 0,0,0]*/

TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ITR0);

    /*This functional structure is used to configure TIMER2 capture channel-1

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM2->CCMR1------ WRITE BIT NO's 1,0 TO 0,1]

[TIM2->CCER------ WRITE BIT NO's 1 TO 0]*/

    TIM_ICInitstructure.TIM_Channel=(TIM_Channel_1);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM2,&TIM_ICInitstructure);

TIM_SetIC1Prescaler(TIM2,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER2 capture channel-2

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM2->CCMR1------ WRITE BIT NO's 9,8 TO 0,1]

[TIM2->CCER------ WRITE BIT NO's 5 TO 0]*/

TIM_ICInitstructure.TIM_Channel=(TIM_Channel_2);

TIM_ICInitstructure.TIM_ICPolarity=TIM_ICPolarity_Rising;

TIM_ICInitstructure.TIM_ICSelection=TIM_ICSelection_DirectTI;

TIM_ICInitstructure.TIM_ICFilter=0x3;

TIM_ICInit(TIM2,&TIM_ICInitstructure);

TIM_SetIC2Prescaler(TIM2,TIM_ICPSC_DIV1);

/*This functional structure is used to configure TIMER2 capture channel-3

as Rising edge polarity and Direct interrupt method ,Input capture is done on everey rising edge

[TIM2->CCMR2------ WRITE BIT NO's 1,0 TO 0,1]

[TIM2->CCER------ WRITE BIT NO's 9 TO 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_ICFilter=0x3;

TIM_ICInit(TIM2,&TIM_ICInitstructure);

TIM_SetIC3Prescaler(TIM2,TIM_ICPSC_DIV1);

/*This functional structure is used to enable TIMER2 capture channel-1 interrupt for Input capture

[TIM2->CCER------ WRITE BIT NO's 0 TO 1]*/

TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);

/*This functional structure is used to enable TIMER2 capture channel-2 interrupt for Input capture

[TIM2->CCER------ WRITE BIT NO's 4 TO 1]*/

TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE);

/*This functional structure is used to enable TIMER2 capture channel-3 interrupt for Input capture

[TIM2->CCER------ WRITE BIT NO's 8 TO 1]*/

TIM_ITConfig(TIM2,TIM_IT_CC3,ENABLE);

/*This function is used to enable TIMER2 counter*/

TIM_Cmd(TIM2,ENABLE);

/*This function is used to enable TIMER1 counter*/

TIM_Cmd(TIM1,ENABLE);

}
Posted on June 06, 2014 at 16:35

There is an TIM_ICPolarity_BothEdge, but I'm not sure under what conditions it's usable on F1 series parts.

I believe it could be done with F3, F2 and F4 parts.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
psandesh007
Associate II
Posted on June 07, 2014 at 07:33

Thanks clive,

Yes i tried that method using TIM_ICPolarity_BothEdge, but its not present in F1 series you are right.