STM32F407學習記錄2:SysTick嘀嗒定時器學習
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