Skip to main content
HNguy.25
Associate III
November 29, 2022
Solved

Uneven/increasing/decreasing elapsed time by stm32 timer

Hello everyone,

I am working with the board STM32F446RE with SX1261 on mbed os.

SITUATION: I want to measure the time difference between the send moment and the receive moment of a packet (transmitted by LoRa). Basically, when I send packet A (send time) from 1st board, the second board receives the packet and immediately responds back. 1st board receives (receive time) the response. I measured this period 30 times to get the time difference but this value kept increasing/decreasing until it reached a point before dropping/rocketing.

SET UP: I used timer 2 with 90MHz clock frequency. Two boards are at fixed distance.

WHAT I HAVE DONE: Considering that changing measurement, I also measured the time difference between the receive time and the response time on the 2nd board with timer 2. The same dropping/rocketing still occurred.

QUESTION: What might be the cause of this change? I suspect it might be the timer that has a rollover.

The pattern is something as below. “Dedicated�? means the 1st board sends a packet and receives a response. The 2nd board receives the packet and sends a response. “Reverse�? means the opposite.

0693W00000WJm7QQAT.pngKind regards,

Huy Nguyen.

This topic has been closed for replies.
HNguy.25
HNguy.25Best answer
Associate III
November 30, 2022

My friend just found out the cause. It is because of the HAL_delay(). The resolution of that delay function is 1ms. So with the 90MHz frequency, 1ms is equal to 90000 ticks which is A LOT. That is why there is a periodic drop/rocket.

Once my friend removes the delay function, the large fluctuation does not happen anymore.

Thank you,

Huy Nguyen.

3 replies

Bob S
Super User
November 29, 2022

Can't help without seeing your code. It look like you may not be updating your "start" time, so the "end minus start" calculation keeps increasing until the 32 bit values roll over.

HNguy.25
HNguy.25Author
Associate III
November 30, 2022

Thank you for your reply, @Bob S​ 

​

Indeed, that is something I never thought of bcuz I assume the "start" get updated based on my code.

And sorry for not giving the code. I will update the post.

Thank you,

Huy Nguyen.​

HNguy.25
HNguy.25AuthorBest answer
Associate III
November 30, 2022

My friend just found out the cause. It is because of the HAL_delay(). The resolution of that delay function is 1ms. So with the 90MHz frequency, 1ms is equal to 90000 ticks which is A LOT. That is why there is a periodic drop/rocket.

Once my friend removes the delay function, the large fluctuation does not happen anymore.

Thank you,

Huy Nguyen.