2018-06-16 01:25 PM
Hi friends,
The while loop does not work in the following function. Why could it be?
For STM32F407VG;
void US_BEKLE(uint16_t us)
{ uint16_t baslangic = (uint16_t)__HAL_TIM_GET_COUNTER(&htim1);while(((uint16_t)__HAL_TIM_GET_COUNTER(&htim1)) - baslangic < us);
}
htim1.Instance = TIM1;
htim1.Init.Prescaler = 167; htim1.Init.CounterMode = TIM_COUNTERMODE_UP; htim1.Init.Period = 65000; htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim1.Init.RepetitionCounter = 0;#microsecond-timer #while-loopSolved! Go to Solution.
2018-06-17 07:07 AM
I solved the problem. As I said, there was a problem when the subtraction was negative. It worked smoothly in the following manner.
void US_BEKLE(uint16_t us)
{uint16_t baslangic = (uint16_t)__HAL_TIM_GET_COUNTER(&htim1);
while((((uint16_t)__HAL_TIM_GET_COUNTER(&htim1)) - baslangic < us) && (baslangic - ((uint16_t) __HAL_TIM_GET_COUNTER(&htim1)) < (65535 - us)) );
}
Thanks
2018-06-16 01:29 PM
Hello,
please clarify 'it doesn't work'. Please check some of these questions:
Best regards,
Tilen
2018-06-16 02:37 PM
For the math to work you want
htim1.Init.Period = 65535;
try this
void US_BEKLE(uint16_t us)
{uint16_t baslangic = (uint16_t)__HAL_TIM_GET_COUNTER(&htim1);while( (uint16_t)(__HAL_TIM_GET_COUNTER(&htim1) - baslangic) < us);
}
2018-06-16 03:40 PM
hi,
timer is running.
I do not use optimization.
I want to run the delay time for 20 us as an example.
2018-06-16 03:47 PM
'
Why could it be?'
look up integer promotion. the simplest yet most complex aspect of C.
2018-06-16 05:21 PM
htim1.Init.Period = 65535;
unfortunately did not work
2018-06-16 05:32 PM
I think it's a problem because the subtraction process is not an absolute value.
for example;
us=500
baslangic=65500
current get timer = 300 (autoreload overflow after from 65535 - > +1 CNT = 0 )
substract = 300 - 65500 = -65200
because -65200 < 500 while loop is constantly going on
As a solution, the absolute value must be applied, or the timer module must be reset each time.
2018-06-16 05:43 PM
That's not how unsigned 16-bit math works. Also for the wrapping to work properly the Period needs to be 0xFFFF (65535)
336 < 500
Perhaps the HAL function is masking the volatile nature of the peripheral register, assuming optimization involved.
void US_BEKLE(uint16_t us)
{uint16_t baslangic = (uint16_t)TIM1->CNT;while( (uint16_t)(TIM1->CNT - baslangic) < us);
}
And your complaint is that it isn't exiting the loop?
What tool chain is involved here?
2018-06-16 07:59 PM
this is a similar issue to
Turvey.Clive.002
mention of HAL_timeout.change the order of calculation, (but this is not a good solution)
void US_BEKLE(uint16_t us)
{ uint16_t endTime = (uint16_t)TIM1->CNT + uswhile( (uint16_t)(TIM1->CNT ) != endTime); // must be exact, so can fail, if the timer is tooo fast.
}
the problem there is that you may miss a count, just when you need it.. depending on the clocking rate.
I suggest that you use the output compare function of the timer, to be sure that you have a hit.
void US_BEKLE(uint16_t us)
{ uint16_t endTime = (uint16_t)TIM1->CNT + us// set OutputCompare to endTime
htim->Instance->CCR1 =endTime;
// wait for OC flag;
while( ! htim->Instance->SR & TIM_SR_CC1IF)
;
}
2018-06-17 07:07 AM
I solved the problem. As I said, there was a problem when the subtraction was negative. It worked smoothly in the following manner.
void US_BEKLE(uint16_t us)
{uint16_t baslangic = (uint16_t)__HAL_TIM_GET_COUNTER(&htim1);
while((((uint16_t)__HAL_TIM_GET_COUNTER(&htim1)) - baslangic < us) && (baslangic - ((uint16_t) __HAL_TIM_GET_COUNTER(&htim1)) < (65535 - us)) );
}
Thanks