Skip to main content
Ramazan Gülcan
Associate III
June 16, 2018
Solved

WHILE LOOP PROBLEM FOR MICRO SECONDS WITH TIMER ??

  • June 16, 2018
  • 13 replies
  • 2567 views
Posted on June 16, 2018 at 22:25

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-loop
This topic has been closed for replies.
Best answer by Ramazan Gülcan
Posted on June 17, 2018 at 14:07

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

13 replies

Tilen MAJERLE
ST Employee
June 16, 2018
Posted on June 16, 2018 at 22:29

Hello,

please clarify 'it doesn't work'. Please check some of these questions:

  1. Is the timer running?
  2. Do you use optimizations and while loop was optimized out?
  3. Is your delay longer or shorter than expected?

Best regards,

Tilen

Ramazan Gülcan
Associate III
June 16, 2018
Posted on June 16, 2018 at 22:40

hi,

timer is running.

I do not use optimization.

I want to run the delay time for 20 us as an example.

Tesla DeLorean
Guru
June 16, 2018
Posted on June 16, 2018 at 23:37

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);

}

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Ramazan Gülcan
Associate III
June 17, 2018
Posted on June 17, 2018 at 00:21

htim1.Init.Period = 65535;

unfortunately did not work

henry.dick
Associate II
June 16, 2018
Posted on June 17, 2018 at 00:47

'

Why could it be?'

look up integer promotion. the simplest yet most complex aspect of C.

T J
Senior III
June 17, 2018
Posted on June 17, 2018 at 02:59

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 + us

while( (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)

;

}