这个寄存器好像目前的水平我还用不到,大体意思明白点,把英文说明放这吧:
位31 NOREF :1=没有外部参考时钟(STCLK 不可用)0=外部参考时钟可用
位30 SKEW:1=校准值不是准确的1ms 0=校准值是准确的1ms
位[23:0] :Calibration value
Indicates the calibration value when the SysTick counter runs on HCLK max/8 as external clock. The value is product dependent, please refer to the Product Reference Manual, SysTick Calibration Value section. When HCLK is programmed at the maximum frequency, the SysTick period is 1ms. If calibration information is not known, calculate the calibration value required from the frequency of the processor clock or external clock.
SysTick定时器除了能服务于操作系统之外,还能用于其它目的:如作为一个闹铃,用于测量时间等。要注意的是,当处理器在调试期间被喊停(halt)时,则SysTick定时器亦将暂停运作。
下面我们就应用SysTick定时器来裸奔,把它作为一个定时器来用,还是老一套,在寄存器头文件中添加定义寄存器:
//*****************************************************************
//* SystemTick-Register
//*******************************************************************
#define SYSTICK_TENMS (*((volatile unsigned long *)0xE000E01C))
#define SYSTICK_CURRENT (*((volatile unsigned long *)0xE000E018))
#define SYSTICK_RELOAD (*((volatile unsigned long *)0xE000E014))
#define SYSTICK_CSR (*((volatile unsigned long *)0xE000E010))
配置systick寄存器:
void SysTick_Configuration(void)
{
SYSTICK_CURRENT=0; //当前值寄存器
SYSTICK_RELOAD=20000; //重装载寄存器,系统时钟20M中断一次1mS
SYSTICK_CSR|=0x06;// HCLK作为Systick时钟,Systick中断使能位
}
中断处理:
void SysTick_Handler(void) //中断函数
{
extern unsigned long TimingDelay; // 延时时间,注意定义为全局变量
SYSTICK_CURRENT=0;
if (TimingDelay != 0x00)
TimingDelay--;
}
利用systick的延时函数:
unsigned long TimingDelay; // 延时时间,注意定义为全局变量
void Delay(unsigned long nTime) //延时函数
{
SYSTICK_CSR|=0x07; // 使能SysTick计数器
TimingDelay = nTime; // 读取延时时间
while(TimingDelay != 0); // 判断延时是否结束
SYSTICK_CSR|=0x06;// 关闭SysTick计数器
}
int main()
{
SystemInit0(); //系统(时钟)初始化
stm32_GpioSetup (); //GPIO初始化
SysTick_Configuration(); //配置systick定时器
while(1)
{
GPIO_PORTB_ODR|=(1<<5);
Delay(1000); //1S
GPIO_PORTB_ODR&=~(1<<5);
Delay(1000); //1S
}
}
完成!Delay(1000);实现了1S的精确延时,利用Delay(unsigned long nTime);配合systick定时器可以实现任意时间的精确延时,当然通过定时器TIMx也是可以这样做的,我只是用它来说明systick定时器的用法。