你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】stm32之HAL库实现us延时方法

[复制链接]
STMCU小助手 发布时间:2022-3-9 22:00
stm32之HAL库实现us延时方法
HAL_Delay()源码为ms级别的延时如下:

  1. /**
  2.   * @brief This function provides accurate delay (in milliseconds) based
  3.   *        on variable incremented.
  4.   * @note In the default implementation , SysTick timer is the source of time base.
  5.   *       It is used to generate interrupts at regular time intervals where uwTick
  6.   *       is incremented.
  7.   * @note This function is declared as __weak to be overwritten in case of other
  8.   *       implementations in user file.
  9.   *
  10.   * @param Delay: specifies the delay time length, in milliseconds.
  11.   * @retval None
  12.   */
  13. __weak void HAL_Delay(__IO uint32_t Delay)
  14. {
  15.   uint32_t tickstart = 0U;
  16.   tickstart = HAL_GetTick();
  17.   while((HAL_GetTick() - tickstart) < Delay)
  18.   {
  19.   }
  20. }
复制代码

下面是us级别的延时:
  1. void HAL_Delay_us(uint32_t nus)
  2. {
  3. //将systic设置为1us中断
  4. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000);
  5. //延时nus
  6. HAL_Delay(nus-1);
  7. //恢复systic中断为1ms
  8. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  9. }
复制代码

另外需要注意的一点是stm32H7里面时钟配置如下:

7T55[5V7]T2I8[)5}4SSLGQ.png

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

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()*2/1000000);


收藏 评论0 发布时间:2022-3-9 22:00

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版