cancel
Showing results for 
Search instead for 
Did you mean: 

TIMER GET COUNTER VALUE

akshayamk
Associate II
Posted on February 03, 2016 at 10:51

ulStart = __HAL_TIM_GetCounter(&htim17) / ulClkFrq ; 
do { 
/* TimeOut Check */ 
ulNow = __HAL_TIM_GetCounter(&htim17)/ ulClkFrq; 
// ulNow = __HAL_TIM_SetCounter(&htim17, count)/ ulClkFrq; 
if( ulStart > ulNow ) 
{ 
ulTick = (((TIMER_CNT_MAX_VAL / ulClkFrq + (unsigned long)1) - ulStart) + ulNow); 
} 
else 
{ 
ulTick = (ulNow - ulStart); 
} 
if(250 <= ulTick ) 
{ 
break; /* exit do while */ 
} 
} while( 0xff ==count); 

As seen above, I need to get the difference between ulStart and ulNow. However, ulNow always remains as 0 as the do-while loops. It never increments. Do I have to change the logic of this code to achieve the difference? Thanks!
4 REPLIES 4
Radosław
Senior II
Posted on February 03, 2016 at 11:19

Your calculations is VERY strange.

Why you divide TIM value by FREQ?

Posted on February 03, 2016 at 17:45

Do the math in the units of the tick, don't divide it down a lose any precision you had, plus the wrapping math will fail.

If the timeout is 250 ms, figure out what 250 ms in units of the TIM's tick.

If the clock ticks at 100 KHz, 250 ms would be 25000 ticks
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
akshayamk
Associate II
Posted on February 04, 2016 at 10:25

Hello Clive,

Following your suggestion, I amended my tim17 to this:

void MX_TIM17_Init(void) 
{ 
uint32_t uwPrescalerValue; 
uwPrescalerValue = (uint32_t) (SystemCoreClock / 100000) - 1; //SystemCoreClock = 20MHz; 0X1312D00 
htimInstance = TIM17; 
htimInit.Prescaler = uwPrescalerValue; 
/* Prescaler derivation: ------------------------------------ 
Required free run timer = 1us tick (100kHz) 
So, set tim17 counter clock frequency, CK_CNT =100kHz = 100000 
PSC[15:0] = 20000000/100000 - 1 = 200 - 1 = 199 
/*------------------------------------------------------------------------ */ 
htimInit.CounterMode = TIM_COUNTERMODE_UP; 
htimInit.Period = 0xFFFF; 
htimInit.ClockDivision = 0; 
htimInit.RepetitionCounter = 0; 
HAL_TIM_Base_Init(&htim17); 

}

I need my tim17 to function such that it can count till at least 250ms (required to set baud rate).
Radosław
Senior II
Posted on February 04, 2016 at 19:02

Yoy try do very strange thing.   Why you wait in looop and check differences beetween two time stamps?

Why you can wait for uptade flag or compare?   Why you wonna wait blocked in that loop?