STM32 system tick timer (Systick)

Publisher:WhisperingWaveLatest update time:2018-12-24 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Systick clock source


2. SysTick library function



3. Source code

 


/**


  *@}


 */


 


/** @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;        /*!


#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;        /*!


#elif defined SYSCLK_FREQ_48MHz


  uint32_t SystemCoreClock         = SYSCLK_FREQ_48MHz;        /*!


#elif defined SYSCLK_FREQ_56MHz


  uint32_t SystemCoreClock         = SYSCLK_FREQ_56MHz;        /*!


#elif defined SYSCLK_FREQ_72MHz


  uint32_t SystemCoreClock         = SYSCLK_FREQ_72MHz;        /*!


#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();


}


Keywords:STM32 Reference address:STM32 system tick timer (Systick)

Previous article:Problem with Systick of stm32F407
Next article:STM32-How to use SysTick

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号