2019-06-05 09:00 PM
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);
}
2019-06-06 07:59 AM
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.
2019-06-19 03:10 PM
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
}
2019-06-19 03:48 PM
"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
}
2024-11-10 04:11 AM - last edited on 2024-11-11 06:27 AM by Andrew Neil
Doğru olanı yazıyor musun?
Translated to English using Google: Are you writing the right thing?
2024-11-10 08:04 AM - edited 2024-11-10 08:08 AM
>>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
2024-11-11 06:09 AM - last edited on 2024-11-11 06:26 AM by Andrew Neil
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?
2024-11-11 06:28 AM - edited 2024-12-19 02:14 AM
Basically the same as you do in Arduino with millis():
https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/
EDIT:
For a worked example, see:
2024-11-11 09:16 AM - edited 2024-11-11 09:16 AM
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) {};
}