[APM32F4] 【APM32F402R Micro-EVB】-3-SysTick配置

[复制链接]
 楼主| 南来之风 发表于 2025-7-27 10:26 | 显示全部楼层 |阅读模式
本帖最后由 南来之风 于 2025-7-27 10:30 编辑

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

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


Systick在arm cortex内核特有的片上外设,实现基本的延迟控制LED闪烁也比较简单。首先修改系统时钟为120 MHz
  1. uint32_t SystemCoreClock = 120000000;

设置Systick时基为1ms:
  1. SysTick_Config(SystemCoreClock/1000);
具体实现为:
  1. /**
  2.   \brief   System Tick Configuration
  3.   \details Initializes the System Timer and its interrupt, and starts the System Tick Timer.
  4.            Counter is in free running mode to generate periodic interrupts.
  5.   \param [in]  ticks  Number of ticks between two interrupts.
  6.   \return          0  Function succeeded.
  7.   \return          1  Function failed.
  8.   \note    When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
  9.            function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
  10.            must contain a vendor-specific implementation of this function.
  11. */
  12. __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
  13. {
  14.   if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
  15.   {
  16.     return (1UL);                                                   /* Reload value impossible */
  17.   }

  18.   SysTick->LOAD  = (uint32_t)(ticks - 1UL);                         /* set reload register */
  19.   NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
  20.   SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
  21.   SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
  22.                    SysTick_CTRL_TICKINT_Msk   |
  23.                    SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */
  24.   return (0UL);                                                     /* Function successful */
  25. }

中断函数中处理计数变量:
  1. /*!
  2. * [url=home.php?mod=space&uid=247401]@brief[/url]     This function handles SysTick Handler
  3. *
  4. * @param     None
  5. *
  6. * @retval    None
  7. *
  8. */
  9. void SysTick_Handler(void)
  10. {
  11.         timeElasped_ms++;
  12. }


主循环:
  1.     while (1)
  2.     {
  3.         if(timeElasped_ms%1000 == 0){
  4.                 BOARD_LED_Toggle(LED3);
  5.                 printf("\r\n timeElaspsed_ms = %d", timeElasped_ms);
  6.         }
  7.    }

实物演示:
IMG_7406.GIF
银河漫步 发表于 2025-7-29 11:32 | 显示全部楼层
systick计数器的设计的功能实在是太棒了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

69

主题

290

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

69

主题

290

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部