更高效處理 micro second 的方式…
以 STM32 為例…
__IO unsigned long sys_tick = 0;
{
HAL_IncTick();
sys_tick += (SysTick->LOAD + 1);
}
unsigned long micros_ticks(void)
{
unsigned long save_sys_tick;
unsigned long st;
do
{
save_sys_tick = sys_tick;
st = SysTick->VAL;
} while (save_sys_tick != sys_tick);
return (save_sys_tick-st);
}Get micro second...
micros = micros_ticks()/(SysTick->LOAD + 1)
or just get ticks for saving cpu calculate time...
microsxconstant = micros_ticks()
*在只需計時的場合直接使用 micros_ticks,可以節省 CPU 運算除法的時間