cancel
Showing results for 
Search instead for 
Did you mean: 

How to use HAL_GetTick ?

antonius
Senior

Dear Members,

How can I use HAL_GetTick for time stamp ?

Is this right ?

or I need something else ?

Thanks

case PULSE_TRACE_UP:
      if(sensor_value > prev_sensor_value)
      {
        printf("PULSE TRACE_UP\r\n");
				currentBeat = HAL_GetTick();
        lastBeatThreshold = sensor_value;
				printf("Sensor value PULSE_TRACE_UP %f\r\n",sensor_value);
				
      }

8 REPLIES 8
S.Ma
Principal

currentBeat will be a uint32_t type number which is in msec unit and incremented in the background by 1 msec interrupt.

make the currentBeat a global variable (and volatile if needed). Rename it as TimeStamp_ms to be more intuitive.

In your extract code, the variable is not used.

Kent Swan
Senior

HAL_GetTick will give you a 32 bit value from the internal tick cell which will be milliseconds since last reset or boot. The tick word is incremented each millisecond via the SysTick interrupt which is independant of your application code. You can call HAL GetTick at any time in your program. If you want to do something 1340 ticks from now you would use something like:

future_tick_time = HAL_GetTick() + 1340;

Then in your code loop you would use something like:

if(HAL_GetTick() > future_tick_time)

{

// do something now

}

"future_tick_time = HAL_GetTick() + 1340;

 

Then in your code loop you would use something like:

 

if(HAL_GetTick() > future_tick_time)

{

// do something now

}"

That code is profoundly broken, it will behave incorrectly at the roll-over point, every ~49.5 days

What you want is something that uses the unsigned math to contain the wrapping condition

uint32_t start = HAL_GetTick();

if ( (HAL_GetTick() - start) > 1340)

{

// do something

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Doğru olanı yazıyor musun?

 

Translated to English using Google:   Are you writing the right thing?

>>Are you writing the right thing?

Yes

If you subtract, and check the delta, it works across the uint32_t wrap value properly.

The original suggestion of adding WILL FAIL

Say tick = 0xFFFFFF00;

future = tick + 1340; // 0x0000043C

0xFFFFFF00 > 0x0000043C IMMEDIATELY...

Whereas

0xFFFFFF01 - 0xFFFFFF00 = 1

0x0400 - 0xFFFFFF00 = 0x500 (1280d)

 

The naive code is the type of stuff that results in a continous parade of failing implementation causing things to break/misbehave in the 49 - 50 day windows. ie 2**32 milliseconds

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Beni yanlış anladın. Sizce kodun ne olması gerekiyor? Zamanlayıcıları gecikmeli olarak engellemeden beklemenin en iyi yolu nedir?

 

Translated to English using Google: You misunderstood me. What do you think the code should be? What is the best way to wait for the timers without blocking them with a delay?

This is the code I said was the way to do it, you determine how much time has advanced, by subtracting out the starting point, not pick a specific time in the future

 

uint32_t start = HAL_GetTick();
..
if ( (HAL_GetTick() - start) > 1340)
{
  // do something
}

 

For a TIM counting at 1 MHz, micro-seconds, and another at 1 KHz

 

void delay_micro(uint32_t delay)
{
  uint32_t start = HAL_TIM_GetCounter(&hTim2); // 32-bit counter 1 MHz
  while ( (HAL_TIM_GetCounter(&hTim2) - start) < delay) {};
}

void delay_milli(uint32_t delay)
{
  uint32_t start = HAL_TIM_GetCounter(&hTim5); // 32-bit counter 1 KHz
  while ( (HAL_TIM_GetCounter(&hTim5) - start) < delay) {};
}

 

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..