SysTick定时器
SysTick定时器被捆绑在NVIC中,用于产生SysTick异常(异常号:15)。
Systick 部分内容属于NVIC控制部分,一共有4个寄存器,名称和地址分别是:
STK_CSR, 0xE000E010 – 控制寄存器
STK_LOAD, 0xE000E014 – 重载寄存器
STK_VAL, 0xE000E018 – 当前值寄存器
STK_CALRB, 0xE000E01C – 校准值寄存器
所有的Cortex‐M3处理器内部都包含了这个定时器,软件在不同 CM3器件间的移植工作得以化简。该定时器的时钟源可以是内部时钟(FCLK,CM3上的*运行时钟free clock),或者是外部时钟( CM3处理器上的STCLK信号)。不过,STCLK的具体来源则由芯片设计者决定。
对于STM32芯片而言,STCLK为AHB的8分频。
因此,在利用systick进行延时函数编写时,就有一个SYSTICK来源的选择问题。对应的stm32固件库函数是SysTick_CLKSourceConfig,函数原型为void SysTick_CLKSourceConfig(u32 SysTick_CLKSource),其中的SysTick_CLKSource值为:
寄存器编程时,延迟初始化函数为
利用systick编写延时函数有2种:查询法和中断法。
查询法:
#include "delay.h"
#include "sys.h“
static u8 fac_us=0;//us延时倍乘数
static u16 fac_ms=0;//ms延时倍乘数
void delay_init()
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //外部 HCLK/8
fac_us=SystemCoreClock/8000000; //为系统时钟的1/8
fac_ms=(u16)fac_us*1000; //非ucos下,代表每个ms需要的systick时钟数
};
void delay_us(u32 nus)
{
u32 temp;
SysTick->LOAD=nus*fac_us; //时间加载
SysTick->VAL=0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do { temp=SysTick->CTRL;
} while(temp&0x01&&!(temp&(1<<16))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}
//延时nms。注意nms的范围,SysTick->LOAD为24位寄存器,所以,最大延时为:
//nms<=0xffffff*8*1000/SYSCLK,SYSCLK单位为Hz,nms单位为ms。对72M下,nms<=1864
void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms; //时间加载(SysTick->LOAD为24bit)
SysTick->VAL =0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do {
temp=SysTick->CTRL;
} while(temp&0x01&&!(temp&(1<<16))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}
中断法:
void SysTick_Configuration(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); /* 设置AHB时钟为SysTick时钟*/
/*设置SysTicks中断抢占优先级 3, 从优先级0*/
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
SysTick_SetReload(72000); /* HCLK=72MHz 每1ms发生一次SysTick中断*/
SysTick_ITConfig(ENABLE); /* Enable the SysTick Interrupt */
}
void Delay(u32 nTime)
{
SysTick_CounterCmd(SysTick_Counter_Enable); /* 允许SysTick计数器*/
TimingDelay = nTime;
while(TimingDelay != 0) ; //等待计数至0
SysTick_CounterCmd(SysTick_Counter_Disable); /*禁止SysTick计数器*/
SysTick_CounterCmd(SysTick_Counter_Clear); /* 清零SysTick计数器*/
}
// 中断函数,定时器减至零时调用,放在stm32f10x_it.c文件中
void SysTickHandler(void)
{ TimingDelay--; }
来自 http://blog.csdn.net/yangqiwei2012/article/details/16355711
中断法2: 延时函数写的 很有意思
#define SYS_TICKS_PER_SEC 1000
#define SYS_LED_PINMASK 0x0020 //PB5
#define CLK_TO_1US (SYSCLK_FREQ_72MHz / 1000000)
static volatile uint32_t sys_counts;
void SysTick_Handler(void)
{
sys_counts++;
//led flicker
if((sys_counts & 0x1FF) == 0)
{
sys_led_toggle();
if (sdmmc_flick)
{
sdmmc_led_toggle();
}
if (burn_flick)
{
burn_led_toggle();
}
}
}
void arch_sys_init(void)
{
SysTick_Config(SYSCLK_FREQ_72MHz / SYS_TICKS_PER_SEC);
}
void arch_sys_delay(uint32_t milliSec)
{
unsigned int old_ms;
old_ms = sys_counts;
/* check if (oscr+clk_count) wraps around */
if( ((unsigned int)0xffffffff - milliSec) < old_ms)
{
/* adjust clk_count */
milliSec = milliSec - (0xffffffff - old_ms);
while(sys_counts >= old_ms);
old_ms = 0;
}
while(sys_counts < (old_ms + milliSec));
}
void arch_sys_delayus(uint32_t us)
{
uint32_t oscr, clk_count;
uint32_t counter = 0;
/* Determine nbr SysTick increments in OS_TICKS_PER_SEC. */
uint32_t peroid = SYSCLK_FREQ_72MHz / SYS_TICKS_PER_SEC;
/* take OSCR snapshot */
oscr = SysTick->VAL;
clk_count = (us * CLK_TO_1US); // 10000;
if(clk_count > peroid)
{
//to long than one tick
return;
}
/* check if (oscr+clk_count) wraps around */
if( (peroid - clk_count) < oscr)
{
/* adjust clk_count */
clk_count = clk_count - (peroid - oscr);
while(SysTick->VAL >= oscr)
{
; /* wait until OSCR wraps around */
}
oscr = 0;
}
counter = oscr + clk_count;
while(counter > SysTick->VAL)
{
if(SysTick->VAL < oscr)
break;
}
}
int arch_sys_tick(void)
{
return sys_counts;
}
贴出cm3.h的配置函数
__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 Systick Interrupt */
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 */
}
//系统滴答配置1ms中断
void SysTick_Configuration(void)
{
SYSCTRL_ClocksTypeDef System_Clocks;
SYSCTRL_GetClocksFreq(&System_Clocks);
// printf("PLL_Frequency is %d,HCLK_Frequency is %d, PCLK_Frequency is %d",
// System_Clocks.PLL_Frequency,System_Clocks.HCLK_Frequency,System_Clocks.PCLK_Frequency);
SysTick_Config(System_Clocks.HCLK_Frequency/1000); //SysTick_Config(96000);
}