SysTick系统滴答定时器

工作原理

SysTick系统滴答定时器是一个24位递减计数器计数器,向下计数,最大计数值为(

SysTick系统滴答定时器,每个时钟周期-1,减到0后申请中断,并且会自动重装初值。

注意:

  • 定时器的位数越多,定时时间更长。

  • 通过计数值间接计算定时时间,不能像操作系统直接调用函数实现延时或定时功能

系统滴答定时器的用途

没有操作系统:只用于延时(使用内核的SysTick定时器来实现延时,可以不占用系统定时器,节约资源)

有操作系统:为操作系统提供精准的系统时基。

SysTick的时钟源

可以来自两个地方:

外部参考时钟:AHB时钟8分频

内核时钟:HCLK时钟/(AHB时钟)

SysTick实现中断

1.系统定时器配置

core_cm3.h文件中 SysTick_Config(uint32_t ticks),默认的时钟源是选择的用的HCLK(内核)时钟。

示例程序:

    //系统定时器触发1KHz的中断,中断周期时间T= 1/f = 1000ms/1000=1ms
    //系统定时器连接到PLL输出的72MHz时钟
    //只要系统定时器进行72000000次计数,就是1秒时间的到达
    //只要系统定时器进行72000次计数,就是1ms时间的到达
    //只要系统定时器进行72次计数,就是1us时间的到达    
    SysTick_Config(SystemCoreClock/1000);
   void  SysTick_Handler(void)//SysTick中断服务函数

2.最大定时时间

确定最大的计数值2^24 -1,若计算到0,则进行2^24次计数

Tmax = 2^24 *1000ms/72000000 = 233ms。

3.测试结果

初始化系统定时器,1S 内核触发 1000 次中断,定时 1ms,能够成功

SysTick_Config(SystemCoreClock/1000);

初始化系统定时器,1S 内核触发 4 次中断,定时 250ms,现象失败

SysTick_Config(SystemCoreClock/4);

初始化系统定时器,1S 内核触发 5 次中断,定时200ms,能够成功

SysTick_Config(SystemCoreClock/5);

总结:填写中断频率值不能小于5,否则定时时间不准确。

SysTick_Config(SystemCoreClock/5);

SysTick实现软件延时

In many cases you might not want to use the SysTick_Config function because you might want to use the reference clock or you might not want to enable the SysTick interrupt. In these cases you need to program the SysTick registers directly,and the following sequence is recommended:

1). Disable the SysTick timer by writing 0 to SysTick->CTRL. This step is optional.It is recommended for reusable code because the SysTick could have been enabled previously.

2). Write the new reload value to SysTick->LOAD.

3). Write to the SysTick Current Value register SysTick->VAL with any value to clear the current value to 0.

4). Write to the SysTick Control and Status register SysTick->CTRL to start the SysTick timer.

If you want to use the SysTick timer in polling mode, you can use the count flag in the SysTick Control and Status Register (SysTick->CTRL) to determine when the timer reaches zero. For example, you can create a timed delay by setting the SysTick timer to a certain value and waiting until it reaches zero:

官方流程代码:

SysTick->CTRL = 0; // Disable SysTick
SysTick->LOAD = 0xFF; // Count from 255 to 0 (256 cycles)
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set
SysTick->CTRL = 0; // Disable SysTick

使用SysTick编写延时函数,就是操控SysTick的几个寄存器

微秒级延时函数

//宏定义
#define AHB_INPUT  72  //请按RCC中设置的AHB时钟频率填写到这里(单位MHz)

/********************************************************************************
函数:delay_us(u32 uS)
功能:微秒级延时函数
参数:u32 uS
返回值:无
********************************************************************************/
void delay_us(u32 uS)      //uS微秒级延时程序(参考值即是延时数,72MHz时最大值233015)
{     
    SysTick->LOAD=AHB_INPUT*uS;      //重装计数初值(当主频是72MHz,72次为1微秒)
    SysTick->VAL=0x00;        //清空定时器的计数器
    SysTick->CTRL=0x00000005;  //时钟源HCLK,打开定时器 5是令CTRL的第0和第二位 置1
    while(!(SysTick->CTRL&0x00010000)); //等待计数到0  寄存器第17位 标志位 置1
    SysTick->CTRL=0x00000004;//关闭定时器
}

毫秒级延时函数

/********************************************************************************
函数:delay_ms(u16 ms)
功能:毫秒级延时函数
参数:u16 mS
返回值:无
********************************************************************************/
void delay_ms(u16 ms)    //mS毫秒级延时程序(参考值即是延时数,最大值65535)
{                      
    while( ms-- != 0)
    {
        delay_us(1000);    //调用1000微秒的延时
    }
}

秒级延时函数

/********************************************************************************
函数:delay_s(u16 s)
功能:秒级延时函数
参数:u16 mS
返回值:无
********************************************************************************/
void delay_s(u16 s)        //S秒级延时程序(参考值即是延时数,最大值65535)
{                      
    while( s-- != 0)
    {
        delay_ms(1000);    //调用1000毫秒的延时
    }
} 

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值