Systick嘀嗒定时器是一个24位的递减计数器。该定时器的时钟源可以是内部时钟,也可以是外部时钟。 M4的Systick有四个寄存器,分别是
1. Systick control and status register(STK_CTRL) 控制和状态寄存器;
2. Systick reload value register(STK_LOAD) 重装数值寄存器;
3. Systick current value register(STK_VAL) 当前计数值寄存器;
4. Systick calibration value register(STK_CALIB) 校准数值寄存器。
STM32F4中Systick相关函数主要在misc.c和core.cm4.h中。在misc.c中 void Systic_CLKSourceConfig(uint32_t SysTick_CLKSource) 主要实现时钟源的选择。
/**
* @brief Configures the SysTick clock source.
* @param SysTick_CLKSource: specifies the SysTick clock source.
* This parameter can be one of the following values:
* @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
* @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
* @retval None
*/
void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
{
/* Check the parameters */
assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
{
SysTick->CTRL |= SysTick_CLKSource_HCLK;
}
else
{
SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
}
}
在core_cm4.h中函数SysTick_Config(uint32_t ticks)实现了嘀嗒定时器的配置。
/* ################################## SysTick function ############################################ */
/** \ingroup CMSIS_Core_FunctionInterface
\defgroup CMSIS_Core_SysTickFunctions SysTick Functions
\brief Functions that configure the System.
@{
*/
#if (__Vendor_SysTickConfig == 0)
/** \brief System Tick Configuration
The function 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 - 1) > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
SysTick->LOAD = ticks - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Systick Interrupt */
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 */
}
#endif
/*@} end of CMSIS_Core_SysTickFunctions */
嘀嗒定时器使用方法:
1. 函数SysTick_Config()是属于CMSIS里面的一个函数,实现配置如下
- 函数的参数是重载寄存器reload要赋予的初值
- 配置嘀嗒定时器的优先级是最低优先级15
- 配置HCLK作为系统的时钟源
- 使能嘀嗒定时器中断
- 启动嘀嗒定时器计数
2. 用户可以在调用函数SysTick_Config()后通过函数 SysTick_CLKSourceConfig()
更改嘀嗒定时器的时钟源为HCLK/8。SysTick_CLKSourceConfig()在文件misc.c里面
3. 用户可以在调用函数SysTick_Config()后通过函数 NVIC_SetPriority()修改优先级,
函数NVIC_SetPriority()在文件core_cm4.h文件里面
4. 通过下面的公式调整时基
Reload Value = SysTick Counter Clock (Hz) x Desired Time base (s)
Reload Value 是函数SysTick_Config()的参数,但是不能超过0xFFFFFF