STM32的系统滴答定时器(Systick)

1.systick时钟来源



2.SysTick库函数





3.源码

 

/**

  *@}

 */

 

/** @defgroup SysTick_clock_source

  *@{

 */

 

#define SysTick_CLKSource_HCLK_Div8    ((uint32_t)0xFFFFFFFB)

#define SysTick_CLKSource_HCLK         ((uint32_t)0x00000004)

#define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \

                                       ((SOURCE) == SysTick_CLKSource_HCLK_Div8))

 

 

/**

  *@brief  Configures the SysTick clocksource.

  *@param  SysTick_CLKSource: specifies theSysTick clock source.

 *   This parameter can be one ofthe following values:

 *     @argSysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clocksource.

 *     @arg SysTick_CLKSource_HCLK:AHB clock selected as SysTick clock source.

  *@retval None

  */

void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)

{

  /* Check the parameters */

  assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));

  if (SysTick_CLKSource == SysTick_CLKSource_HCLK)

  {

   SysTick->CTRL |= SysTick_CLKSource_HCLK;

  }

  else

  {

   SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;

  }

}

 

 

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

 

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

 

/**

 *@brief  Initialize and start the SysTickcounter 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 forCortex-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

 

 

#ifdefSYSCLK_FREQ_HSE

  uint32_t SystemCoreClock         = SYSCLK_FREQ_HSE;        /*!<System Clock Frequency (Core Clock) */

#elif defined SYSCLK_FREQ_24MHz

  uint32_t SystemCoreClock         = SYSCLK_FREQ_24MHz;        /*!< System Clock Frequency (Core Clock) */

#elif defined SYSCLK_FREQ_36MHz

  uint32_t SystemCoreClock         = SYSCLK_FREQ_36MHz;        /*!<System Clock Frequency (Core Clock) */

#elif defined SYSCLK_FREQ_48MHz

  uint32_t SystemCoreClock         = SYSCLK_FREQ_48MHz;        /*!<System Clock Frequency (Core Clock) */

#elif defined SYSCLK_FREQ_56MHz

  uint32_t SystemCoreClock         = SYSCLK_FREQ_56MHz;        /*!<System Clock Frequency (Core Clock) */

#elif defined SYSCLK_FREQ_72MHz

  uint32_t SystemCoreClock         = SYSCLK_FREQ_72MHz;        /*!<System Clock Frequency (Core Clock) */

#else /*!< HSI Selected as System Clock source */

  uint32_t SystemCoreClock         = HSI_VALUE;        /*!< System Clock Frequency (Core Clock) */

#endif

 

 ///

   /* Configure the SysTick */

SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);//8000000/1000=>1ms

 

/**

 *This is the timer interrupt service routine.

 *

 */

void SysTick_Handler(void)

{

   /* enter interrupt */

   rt_interrupt_enter();

   rt_tick_increase();

   /* leave interrupt */

   rt_interrupt_leave();

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值