cancel
Showing results for 
Search instead for 
Did you mean: 

TIM_ICPolarity_BothEdge not available for most timers???

laurie
Associate II
Posted on June 06, 2012 at 07:36

Hi. I need to read a digital signal coming into an STM32F100.

Timing how long the signal is in a low state and also knowing if the counter overflowed would give me the two pieces of information I need (value of the data bit and a timeout indication).

Timer update without a capture (and reload) would indicate a timeout.

Using both edges of the signal to capture a timer seemed the way to go.

I note in the std peripeherals lib (3.5.0) that

TIM_ICPolarity_BothEdge is NOT available for TIM2 but only for the basic timers.

If that's true, is there another way to do what I need?

Thanks            ...Laurie:{) 
3 REPLIES 3
laurie
Associate II
Posted on June 17, 2012 at 05:26

Answering my own questions:

Yes, you can only set rising OR falling, but you can capture on both (see the PWM mode example in the reference manual).

I wanted to be able to count the timer ticks while the pulse was low and have now done so using this setup:

    TIM2->CR1 = 0;         // reset command register 1

    TIM2->CR2 = 0;          // reset command register 2

 TIM2->PSC = 0 ;         // set prescaler to 1 (full clock tick rate)

 TIM2->ARR = 0xFFFF;  // Set maximum count value

 TIM2->SMCR = 0x054;     // Filtered in T1 input and Slave reset mode. Described as PWM mode in ref manaul 14.3.9

 TIM2->CR1 = 0x4;        // Only update on overflow

 TIM2->CCMR1 = 0x201; // CC2 channel is configured as input, IC2 is mapped on TI1 & CC1 channel is configured as input, IC1 is mapped on TI1

 TIM2->CCER = 0x13;     // Capture Cap 2 and Cap 1. Cap 2 on rising, cap 1 on falling edge

 TIM2->DIER = 0x4;       // enable interrupt for Cap 2 Event

CPU is only interrupted on each falling edge and CCR2 holds the tick count for the time it was low. This setup is then tolerant of interrupt latency as all the edge triggering and capturing is being done in hardware.

         ...Laurie:{)

ss882
Associate II
Posted on September 01, 2012 at 16:13

Hey thanks.

I am trying to do pretty much same thing. I want to measure ON time of the pulse.

In PWM Input mode example of stdperiph examples, are they using ccr1 value as counter value during ON time?

  */

void TIM3_IRQHandler(void)

{

  /* Clear TIM3 Capture compare interrupt pending bit */

  TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

  /* Get the Input Capture value */

  IC2Value = TIM_GetCapture2(TIM3);

  if (IC2Value != 0)

  {

    /* Duty cycle computation */

    DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;

    /* Frequency computation */

    Frequency = SystemCoreClock / IC2Value;

  }

  else

  {

    DutyCycle = 0;

    Frequency = 0;

  }

}

laurie
Associate II
Posted on September 07, 2012 at 03:09

Hi Shiraskar,

I'd need to look at the example again but it seems like CC2 is the total cycle time (hence it can be used to calculate the frequency and CC1 will either be the on or off time (that's the bit you'll need to check in the rest of the example) and CC2 - CC1 should then be the remainder of the cycle (off or on time).

...Laurie:{)