STM32 notes of Systick (tick timer)

EDITORIAL:  This article aims to summarize the backup, to facilitate future inquiries, as is the personal summary, if wrong, please correct me; in addition, most of the content from the Internet, books, and various manuals, should please inform the infringement, immediately delete posts to apologize.

 

table of Contents

A, Systick Introduction

Two, Systick clock analysis

Three, Systick clock beat code for


 

A, Systick Introduction

There the ARM Cortex-M3 core Systick a timer, which is a 24bit timer tick can be used in the system, the system timer tick Cortex-M3 can be used for rapid migration processor chip (ps: positive because of this characteristic, most are based on running the RTOS Systick used to beat the clock in order to transplant)

 

Attach two Cortex-M3 simplified view, and then attach the two links on Cortex-M3 core to know:

"STM32F10xxx Cortex-M3 Programming Manual"

"Cortex ™ -M3 Technical Reference Manual."

 

Two, Systick clock analysis

According to the clock tree, we can see after SYSCLK it might be thought that the system clock is ticking timer 1/8, actually not, the timer ticking clock can be either HCLK / 8, can also be HCLK, this is by CTRL register set (can be found by looking at the "STM32F10xxx Cortex-M3 programming Manual") as shown below:

Systick relevant register description:

Realization of ideas: the use of down-counter systick timer, and set the initial value after it could, it will be every clock cycle counter of a system under reduced until the count when 0, SysTick counter will automatically reload the initial value and continues counting and triggers an interrupt.

Initial count: a clock based on the system configuration used as the 72MHz clock, the counter decrements by one time is 1 / 72MHz, we assume the initial value of the counter is 72000, then each time the counter reaches zero, the time elapsed (1 / 72M) * 72000 = 0.001, i.e., 1ms. (Can be understood as: a clock frequency of 72MHz, he represents the count 1s 72000000 views (number of cycles), then counting 72,000 times to 1ms, the count value is 72,000) 

 

Three, Systick clock beat code for

bsp.c 源文件

#include "bsp.h"


static __IO u32 TimingDelay = 0;

/* Setup SysTick Timer for 1 msec interrupts.
 ------------------------------------------
1. The SysTick_Config() function is a CMSIS function which configure:
   - The SysTick Reload register with value passed as function parameter.
   - Configure the SysTick IRQ priority to the lowest value (0x0F).
   - Reset the SysTick Counter register.
   - Configure the SysTick Counter clock source to be Core Clock Source (HCLK).
   - Enable the SysTick Interrupt.
   - Start the SysTick Counter.

2. You can change the SysTick Clock source to be HCLK_Div8 by calling the
   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8) just after the
   SysTick_Config() function call. The SysTick_CLKSourceConfig() is defined
   inside the misc.c file.

3. You can change the SysTick IRQ priority by calling the
   NVIC_SetPriority(SysTick_IRQn,...) just after the SysTick_Config() function 
   call. The NVIC_SetPriority() is defined inside the core_cm3.h file.

4. To adjust the SysTick time base, use the following formula:
						
	 Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)

   - Reload Value is the parameter to be passed for SysTick_Config() function
   - Reload Value should not exceed 0xFFFFFF
*/

/************************************************
函数名称 : SysTick_Init
功    能 : 启动系统滴答定时器 SysTick
参    数 : 无
返 回 值 : 无
*************************************************/
void SysTick_Init(void)
{
	/* SystemCoreClock / 1000    1ms中断一次
	 * SystemCoreClock / 100000  10us中断一次
	 * SystemCoreClock / 1000000 1us中断一次
	 */

	/* 嘀嗒定时器每计数一次为 1/72M,此处计数 72个数,即1uS中断一次 */
	if(SysTick_Config(SystemCoreClock / 1000000))	// ST V3.5.0库版本
	{ 
		/* Capture error */ 
		while(1);
	}
	
	// 关闭滴答定时器  
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;	
}

/************************************************
函数名称 : Delay_us
功    能 : us延时程序,1us为一个单位
参    数 : nTime ---- 时间次数
返 回 值 : 无
*************************************************/
void Delay_us( __IO u32 nTime )
{ 
	TimingDelay = nTime;	

	// 使能滴答定时器  
	SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;

	while( TimingDelay != 0 );
	
	// 关闭滴答定时器  
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;	
}

/************************************************
函数名称 : TimingDelay_Decrement
功    能 : 获取节拍程序
参    数 : 无
返 回 值 : 无
注    意 : 在 SysTick 中断函数 SysTick_Handler()调用
*************************************************/
void TimingDelay_Decrement(void)
{
	if(TimingDelay != 0x00)
	{ 
		TimingDelay--;
	}
	
}


/*---------------------------- END OF FILE ----------------------------*/


根据上面的注释,选择对应参数传参进行 SysTick_Config() 配置,本篇是制作以微秒为单位的延时定时器,所以传入的参数为 SystemCoreClock / 1000000次,SystemCoreClock的数值,在上一篇 STM32笔记之系统时钟上有讲到

 

bsp.h 头文件

#ifndef __BSP_H
#define __BSP_H


#include "stm32f10x.h"

void SysTick_Init(void);
void Delay_us( __IO u32 nTime );
void TimingDelay_Decrement(void);
void SystemSoftReset(void);


#endif	/* __BSP_H */


/*---------------------------- END OF FILE ----------------------------*/

 

接着我们再在 stm32f10x_it.c 文件中的 SysTick_Handler()函数里调用 TimingDelay_Decrement()函数进行计数递减

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
	TimingDelay_Decrement();
}

最后,我们在主函数中调用函数 SysTick_Init() 完成初始化,就可以使用利用 Systick获取节拍的时钟延时 Delay_us()函数了

发布了40 篇原创文章 · 获赞 14 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_42992084/article/details/104088111