注意:
/** \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 */
}
SysTick_Config(SystemCoreClock / 1000);
#if defined (STM32F40_41xxx)
uint32_t SystemCoreClock = 168000000;
#endif /* STM32F40_41xxx */
static uint32_t i = 0;
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
i++;
//LED0 100ms闪烁1次
LED0 = (0 == i % 100) ? (~LED0) : LED0;
//LED1 330ms闪烁1次
LED1 = (0 == i % 330) ? (~LED1) : LED1;
//LED2 1500ms闪烁1次
LED2 = (0 == i % 1500) ? (~LED2) : LED2;
//LED3 2200ms闪烁1次
LED3 = (0 == i % 2200) ? (~LED3) : LED3;
}
Tmax = 2^24 *1000ms/168000000 = 99.86ms
操作系统(ucos2 ucos3 freertos....):为操作系统提供精准的定时中断(1ms~50ms)
In many cases you might not want to use the SysTick_Confifig 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. The reload value should be the interval value e1.
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.
参考文档:《Cortex M3与M4权威指南.pdf》第316页
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
原文:https://www.cnblogs.com/risesource/p/11844817.html