Does HAL_GetTick() return ticks or milliseconds? (and how to measure in microseconds)

后端 未结 6 1238
一整个雨季
一整个雨季 2021-02-07 11:56

I\'m new to using HAL functions. The description of the function HAL_GetTick() says that it \"provides a tick value in millisecond

6条回答
  •  [愿得一人]
    2021-02-07 12:31

    yI needed a timestamp at 1uS precision, and using TIM5 as described above worked, but a few tweaks were necessary. Here's what I came up with.

    /* Initialization */
    __HAL_RCC_TIM5_CLK_ENABLE();
    TIM5->PSC = HAL_RCC_GetPCLK1Freq() / 500000;
    TIM5->CR1 = TIM_CR1_CEN;
    TIM5->CNT = -10;
    
    /* Reading the time */
    uint32_t microseconds = TIM5->CNT << 1;
    

    I did not fully explore why I had to do what I did. But I realized two things very quickly. (1) The prescalar scaling was not working, although it looked right. This was one of several things I tried to get it to work (basically a half-us clock, and divide the result by 2). (2) The clock was already running, and gave strange results at first. I tried several unsuccessful things to stop, reprogram and restart it, and setting the count to -10 was a crude but effective way to just let it complete its current cycle, then very quickly start working as desired. There are certainly better ways of achieving this. But overall this is a simple way of getting an accurate event timestamp with very low overhead.

提交回复
热议问题