您的位置:首页 > 其它

[STM32]SysTick, 简单到只有一句话

2011-09-05 21:28 232 查看
多年的开发STM32,遇到一些简单的需要计时的任务,比如延时等,最方便的是其提供的systick。

systick其实本为移植操作系统提供滴答时钟的方便。

前两天再次接触STM32,使用了V3.5的库,突然发现繁琐的Systick用法被简化成一句话。

即:

 void SysTick_Configuration(void)

{

  if (SysTick_Config(SystemCoreClock / 1000000))//72, 1us per tick

  {

    /* Capture error */

    while (1);

  }

}

而Systick_Config函数已经取代了之前所有的设置过程。

systick.c文件也被简除,该函数直接归在了内核文件core_cm3.h里面。

/* ##################################    SysTick function  ############################################ */

#if (!defined (__Vendor_SysTickConfig)) || (__Vendor_SysTickConfig == 0)

/**

 * @brief  Initialize and start the SysTick counter and its interrupt.

 *

 * @param   ticks   number of ticks between two interrupts

 * @return  1 = failed, 0 = successful

 *

 * Initialise the system tick timer and its interrupt and start the

 * system tick timer / counter in free running mode to generate

 * periodical interrupts.

 */

static __INLINE uint32_t SysTick_Config(uint32_t ticks)

{

  if (ticks > SysTick_LOAD_RELOAD_Msk)  return (1);            /* Reload value impossible */

                                                              

  SysTick->LOAD  = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;      /* set reload register */

  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Cortex-M0 System Interrupts */

  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

优点是设置相当简化,

缺点是控制不如以前灵活了,一旦开启,确实没有库函数方便地重载或禁用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  timer system function 任务