stm32之HAL库实现us延时方法
·
stm32之HAL库实现us延时方法
HAL_Delay()源码为ms级别的延时如下:
/**
* @brief This function provides accurate delay (in milliseconds) based
* on variable incremented.
* @note In the default implementation , SysTick timer is the source of time base.
* It is used to generate interrupts at regular time intervals where uwTick
* is incremented.
* @note This function is declared as __weak to be overwritten in case of other
* implementations in user file.
*
* @param Delay: specifies the delay time length, in milliseconds.
* @retval None
*/
__weak void HAL_Delay(__IO uint32_t Delay)
{
uint32_t tickstart = 0U;
tickstart = HAL_GetTick();
while((HAL_GetTick() - tickstart) < Delay)
{
}
}
下面是us级别的延时:
void HAL_Delay_us(uint32_t nus)
{
//将systic设置为1us中断
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000);
//延时nus
HAL_Delay(nus-1);
//恢复systic中断为1ms
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
}
另外需要注意的一点是stm32H7里面时钟配置如下:

其中systic是系统时钟直接给systic的而不是HCLK,HAL_RCC_GetHCLKFreq()正好是系统时钟的1/2所以上面配置1us时钟源要改为如下:
更多推荐


所有评论(0)