cancel
Showing results for 
Search instead for 
Did you mean: 

TIM0 and TIM2 to count external pulses

craig239955_st
Associate II
Posted on March 21, 2007 at 11:46

TIM0 and TIM2 to count external pulses

3 REPLIES 3
craig239955_st
Associate II
Posted on May 17, 2011 at 09:39

Hi,

I want to use Counter0 and counter2 to count external pulses.

This works fine, but the first 6 pulses, this is exactly the gap between reset value(0xFFFC) and zero, are not counted !. Once zero is reached, it starts counting.

I use this counters to read the position of a quadrature encoder (with sime logic glue..), so every pulse must be counted !

Any idea ?

This is the initialisation of the counters. I use the library function to read the counters.

Code:

void HAL_InitCounter(void) {

TIM_InitTypeDef Timer ;

SCU_TIMCLKSourceConfig(SCU_TIM01,SCU_TIMCLK_EXT);

SCU_TIMCLKSourceConfig(SCU_TIM23,SCU_TIMCLK_EXT);

Timer.TIM_Mode = TIM_PWMI;

Timer.TIM_ICAP1_Edge = TIM_ICAP1_EDGE_RISING;

Timer.TIM_ICAP2_Edge = TIM_ICAP2_EDGE_FALLING;

Timer.TIM_Clock_Source = TIM_CLK_EXTERNAL;

Timer.TIM_Clock_Edge = TIM_CLK_EDGE_RISING;

//Init Counter 0 (encoder down counter)

TIM_Init (TIM0, &Timer);

TIM_CounterCmd(TIM0,TIM_START);

//Init Counter 2 (encoder up counter)

TIM_Init (TIM2, &Timer);

TIM_CounterCmd(TIM2,TIM_START);

}

amira1
Associate II
Posted on May 17, 2011 at 09:39

Hi Robosoft,

It’s not possible to add these 4 cycles to the counter register since it is read only. In fact, you have to take into account that the real counter value is the obtained one mines 4.

With best regards,

mirou

craig239955_st
Associate II
Posted on May 17, 2011 at 09:39

Thank you for the response.

I think I am not clear enough..(English is not my native language..)

After powering up the device I init the Timer/counters with the function in above post. From then on, I read the Counter Value using the STR9x lib function.

The first time I get a value 0xFFFC (the reset value).

After applying a pulse on the external clock, I still get the same Countervalue, same for pulse 2,3 and 4.

After the 5th pulse, the counter value increments.

Once this is done the counter counts every pulse, even when resetting it by applying a pulse on the ICAP1 input ( Timer is in TIM_PWMI mode).

When I apply this reset pulse immediatly after initialisation, the counter value becomes zero, but it stays zero for the next 4 pulses...(After that applying a reset puls resets the counter and it counts from the first pulse)

Is there a workaround for this ? for example first init the counter to count internal clock and then redirect it to count external pulses ?

Thanks

Luc Vercruysse