How to get time intervals on STM32?

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:46:02

First, enable the cycle counter once at startup:

CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

then, you can access its value:

unsigned long t1 = DWT->CYCCNT;
/* do something */
unsigned long t2 = DWT->CYCCNT;
unsigned long diff = t2 - t1;

It counts the elapsed cpu cycles, you have to divide it with the cpu clock frequency to get a value in seconds.

As it's a 32 bit value, it can overflow quite fast at higher clock frequencies, e.g in 19.88 seconds at 216 MHz.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!