STM32使用HAL库自带延时函数HAL_Delay时存在1ms误差

最近要在stm32f103上写一个pwm编解码程序,要对pwm脉宽进行精确计时,无意间发现使用HAL库自带延时函数产生的延时存在+1ms的误差,即:

HAL_Delay(x);
实际延时时间为(x+1)ms

比如在主循环中加入程序:

		HAL_Delay(1);
		HAL_GPIO_TogglePin(LED_GPIO_Port, GPIO_PIN_13);

烧录程序后使用示波器观察方波波形:
在这里插入图片描述
可以看到方波周期为4ms,相邻跳变之间的时间差为2ms,存在+1ms的误差

实际使用中如果延时时间为几百ms或几s,1ms的误差并没有太大影响,而遇到延时时间非常短的情况则会产生巨大影响。

分析HAL_Delay函数定义

观察HAL_Delay函数在stm32f1xx_hal.c中的定义:

/**
  * @brief This function provides minimum 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(uint32_t Delay)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值