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

3 REPLIES 3
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..