打印
[STM32F1]

SysTick系统定时器精准延时

[复制链接]
133|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
geraldbetty|  楼主 | 2024-2-24 13:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


void delay_ms(unsigned int ms)
{
        SysTick->LOAD = 50000000/1000-1; // Count from 255 to 0 (256 cycles)  载入计数值 定时器从这个值开始计数
        SysTick->VAL = 0; // Clear current value as well as count flag  清空计数值到达0后的标记
        SysTick->CTRL = 5; // Enable SysTick timer with processor clock  使能26MHz的系统定时器
        while(ms--)
        {
                while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set  等待
        }
        SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
}
void delay_us(unsigned int us)
{
        SysTick->LOAD = 50000000/1000/1000-1; // Count from 255 to 0 (256 cycles)  载入计数值 定时器从这个值开始计数
        SysTick->VAL = 0; // Clear current value as well as count flag  清空计数值到达0后的标记
        SysTick->CTRL = 5; // Enable SysTick timer with processor clock  使能26MHz的系统定时器
        while(us--)
        {
                while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set  等待
        }
        SysTick->CTRL = 0; // Disable SysTick  关闭系统定时器
}


使用特权

评论回复
沙发
原来是wjc| | 2024-2-24 21:06 | 只看该作者
你使用的是STM32的SysTick定时器来进行延时。在这个代码中,通过设置SysTick->LOAD来实现定时器的预装载值,但在注释中提到“Count from 255 to 0”,这是不是一个8位的定时器?
在STM32中,SysTick是一个24位的定时器,这个注释是不是写错了呢?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

1127

帖子

0

粉丝