[STM32F1] SysTick系统定时器精准延时

[复制链接]
783|1
 楼主| geraldbetty 发表于 2024-2-24 13:00 | 显示全部楼层 |阅读模式


  1. void delay_ms(unsigned int ms)
  2. {
  3.         SysTick->LOAD = 50000000/1000-1; // Count from 255 to 0 (256 cycles)  载入计数值 定时器从这个值开始计数
  4.         SysTick->VAL = 0; // Clear current value as well as count flag  清空计数值到达0后的标记
  5.         SysTick->CTRL = 5; // Enable SysTick timer with processor clock  使能26MHz的系统定时器
  6.         while(ms--)
  7.         {
  8.                 while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set  等待
  9.         }
  10.         SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
  11. }
  12. void delay_us(unsigned int us)
  13. {
  14.         SysTick->LOAD = 50000000/1000/1000-1; // Count from 255 to 0 (256 cycles)  载入计数值 定时器从这个值开始计数
  15.         SysTick->VAL = 0; // Clear current value as well as count flag  清空计数值到达0后的标记
  16.         SysTick->CTRL = 5; // Enable SysTick timer with processor clock  使能26MHz的系统定时器
  17.         while(us--)
  18.         {
  19.                 while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set  等待
  20.         }
  21.         SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
  22. }


原来是wjc 发表于 2024-2-24 21:06 | 显示全部楼层
你使用的是STM32的SysTick定时器来进行延时。在这个代码中,通过设置SysTick->LOAD来实现定时器的预装载值,但在注释中提到“Count from 255 to 0”,这是不是一个8位的定时器?
在STM32中,SysTick是一个24位的定时器,这个注释是不是写错了呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

36

主题

1645

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部