打印
[APM32F4]

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

[复制链接]
121|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 南来之风 于 2025-7-27 10:30 编辑

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

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


Systick在arm cortex内核特有的片上外设,实现基本的延迟控制LED闪烁也比较简单。首先修改系统时钟为120 MHz
uint32_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 [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 - 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 */
}

中断函数中处理计数变量:
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url]     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计数器的设计的功能实在是太棒了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

69

主题

290

帖子

2

粉丝