2016-02-03 01:51 AM
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!
2016-02-03 02:19 AM
Your calculations is VERY strange.
Why you divide TIM value by FREQ?2016-02-03 08:45 AM
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 ticks2016-02-04 01:25 AM
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).
2016-02-04 10:02 AM
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?