TIMER GET COUNTER VALUE
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-02-03 1:51 AM
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!
This discussion is locked. Please start a new topic to ask your question.
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-02-03 2:19 AM
Posted on February 03, 2016 at 11:19
Your calculations is VERY strange.
Why you divide TIM value by FREQ?Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-02-03 8:45 AM
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..
Up vote any posts that you find helpful, it shows what's working..
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-02-04 1:25 AM
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).
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-02-04 10:02 AM
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?