从零点壹开始学习STM32F4和F7之SysTick滴答定时器

Systick定时器是系统滴答定时器,一个24位的倒计数的定时器,计到0时,将从RELOAD寄存器中自动重装载定时初值。只要不把它在Systick控制及状态寄存器中的使能位清除,就永不停息,即使在睡眠模式下也能工作。

 

SysTick定时器被捆绑在NVIC中,用于产生SYSTICK异常(异常号:15)。

 

SysTick中断的优先级也是可以设置。

 

4个Systick寄存器

      CTRL            SysTick控制和状态寄存器

       LOAD          SysTick重装载数值寄存器

       VAL             SysTick当前值寄存器

       CALIB         SysTick校准值寄存器

 

寄存器是在core_cm4.h或core_cm7.h中定义的

typedef struct

{

      __IOM uint32_t CTRL;

      __IOM uint32_t LOAD;

      __IOM uint32_t VAL;

      __IM    uint32_t CALIB;

}SysTick_Type;

 

 

HAL库中的Systick相关的函数:

       stm32f4xx_hal_cortex.h/stm32f7xx_hal_cortex.h文件中:

void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource); //Systick时钟源选择

core_cm4.h/core_cm4.h文件中:

SysTick_Config(uint32_t ticks); //初始化systick,时钟为HCLK,并开启中断

/* ##################################    SysTick function  ############################################ */
/**
  \ingroup  CMSIS_Core_FunctionInterface
  \defgroup CMSIS_Core_SysTickFunctions SysTick Functions
  \brief    Functions that configure the System.
  @{
 */

#if (__Vendor_SysTickConfig == 0U)

/**
  \brief   System Tick Configuration
  \details Initializes the System Timer and its interrupt, and starts the System Tick Timer.
           Counter is in free running mode to generate periodic interrupts.
  \param [in]  ticks  Number of ticks between two interrupts.
  \return          0  Function succeeded.
  \return          1  Function failed.
  \note    When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
           function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
           must contain a vendor-specific implementation of this function.
 */
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
  if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
  {
    return (1UL);                                                   /* Reload value impossible */
  }

  SysTick->LOAD  = (uint32_t)(ticks - 1UL);                         /* set reload register */
  NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
  SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */
  return (0UL);                                                     /* Function successful */
}

#endif

/*@} end of CMSIS_Core_SysTickFunctions */

 

Systick中断服务函数:

void SysTick_Handler(void);
 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值