关于STM32库中超时函数tick溢出的分析

本文探讨了在STM32官方库中,当使用HAL_Delay进行延迟时,如何处理uwTick变量可能的溢出问题。通过举例说明,在无符号整数的溢出情况下,延迟时间不会因uwTick溢出而发生错误,关键在于理解无符号数的计算方式,特别是计算机原码、反码和补码的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STM32的官方库文件stm32f1xx_hal.c部分摘录如下

__IO uint32_t uwTick;

/**
  * @brief This function is called to increment  a global variable "uwTick"
  *        used as application time base.
  * @note In the default implementation, this variable is incremented each 1ms
  *       in SysTick ISR.
  * @note This function is declared as __weak to be overwritten in case of other
  *      implementations in user file.
  * @retval None
  */
__weak void HAL_IncTick(void)
{
  uwTick += uwTickFreq;
}

/**
  * @brief Provides a tick value in millisecond.
  * @note  This function is declared as __weak to be overwritten in case of other
  *       implementations in user file.
  * @retval tick value
  */
__weak uint32_t HAL_GetTick(void)
{
  return uwTick;
}

/**
  * @brief This function provides minimum delay (in milliseconds) based
  *        on variable incremented.
  * @note In the default implementation , SysTick tim
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值