其他分享
首页 > 其他分享> > STM32系统Tick定时器的初始化和配置

STM32系统Tick定时器的初始化和配置

作者:互联网

1.首先确定SystemCoreClock系统时钟

 系统时钟需要根据具体的系统时钟配置来确定,这里采用SystemCoreClock = 72MHZ来说明。至于系统时钟的计算和配置,后面单独介绍说明一下吧。

2. 配置SysTick定时器

SysTick_Config(SystemCoreClock/OS_TICKS_PER_SEC);//初始化并使能SysTick定时器

假设我们想配置SysTick定时器中断间隔周期为1ms,即每间隔1ms定时中断来一次。

可定义一个宏定义

#define OS_TICKS_PER_SEC       1000    /* Set the number of ticks in one second  

即每1S SysTick定时器中断触发1000次,下面就需要计算一个每次中断触发间隔多少个时钟周期了。

SystemCoreClock 时钟频率是70000000Hz(72Mhz),那边一个时钟周期T=1/F = 1/72000000(S),  1(S)/T=F=72000000,即1S有72000000个时钟周期

(这里说的可能比较绕,方便部分群体理解吧~~)

下面来说一下SysTick_Config的入参,入参的定义可以见下面代码,@param   ticks   number of ticks between two interrupts   即SysTick每两次中断的间隔时间->间隔多少个ticks系统时钟周期数量, 可由SystemCoreClock /OS_TICKS_PER_SEC计算得到。至于SysTick_Config接口内部实现可见下面代码注释。  

上述说明如有不正确的地方,望指正~

/**
 * @brief  Initialize and start the SysTick counter and its interrupt.
 *
 * @param   ticks   number of ticks between two interrupts
 * @return  1 = failed, 0 = successful
 *
 * Initialise the system tick timer and its interrupt and start the
 * system tick timer / counter in free running mode to generate 
 * periodical interrupts.
 */
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{ 
  if (ticks > SysTick_LOAD_RELOAD_Msk)  return (1);            /* Reload value impossible */
                                                               
  SysTick->LOAD  = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;      /* set reload register */
  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Cortex-M0 System Interrupts */
  SysTick->VAL   = 0;                                          /* 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 (0);                                                  /* Function successful */
}

 

标签:定时器,ticks,STM32,SystemCoreClock,Msk,SysTick,Tick,时钟
来源: https://blog.csdn.net/nihao_2014/article/details/115271103