南来之风 发表于 2025-7-27 10:26

【APM32F402R Micro-EVB】-3-SysTick配置

本帖最后由 南来之风 于 2025-7-27 10:30 编辑

SysTick—系统定时器是属于CM4内核中的一个外设,内嵌在NVIC中。系统定时器是一个24bit的向下递减的计数器, 计数器每计数一次的时间为1/SYSCLK,对于APM32F402,我们设置系统时钟SYSCLK等于120M。当重装载数值寄存器的值递减到0的时候,系统定时器就产生一次中断,以此循环往复。

因为SysTick是属于CM4内核的外设,系统定时器一般用于操作系统,用于产生时基,维持操作系统的心跳。


Systick在arm cortex内核特有的片上外设,实现基本的延迟控制LED闪烁也比较简单。首先修改系统时钟为120 MHzuint32_t SystemCoreClock = 120000000;
设置Systick时基为1ms:
SysTick_Config(SystemCoreClock/1000);具体实现为:
/**
\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 ticksNumber of ticks between two interrupts.
\return          0Function succeeded.
\return          1Function 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 */
}
中断函数中处理计数变量:
/*!
* @brief   This function handles SysTick Handler
*
* @param   None
*
* @retval    None
*
*/
void SysTick_Handler(void)
{
      timeElasped_ms++;
}

主循环:
    while (1)
    {
      if(timeElasped_ms%1000 == 0){
                BOARD_LED_Toggle(LED3);
                printf("\r\n timeElaspsed_ms = %d", timeElasped_ms);
      }
   }
实物演示:

银河漫步 发表于 2025-7-29 11:32

systick计数器的设计的功能实在是太棒了
页: [1]
查看完整版本: 【APM32F402R Micro-EVB】-3-SysTick配置