×
单片机 > 单片机程序设计 > 详情

stm32成长记之滴答时钟

发布时间:2020-05-30 发布时间:
|

SysTick具体操作可参照:


http://www.st.com/content/ccc/resource/technical/document/programming_manual/5b/ca/8d/83/56/7f/40/08/CD00228163.pdf/files/CD00228163.pdf/jcr:content/translations/en.CD00228163.pdf


The system timer, SysTick, is a 24-bit count-down timer. Use this as a Real TimeOperating System (RTOS) tick timer or as a simple counter.


简介:系统定时器--SysTick 是一个24位自动递减定时器。它的作用是作为实时操作系统的定时器和简单的计数器。


注意:24位标志着最大的计数值为 0xffffff=16777215  。


The processor has a 24-bit system timer, SysTick, that counts down from the reload value tozero, reloads (wraps to) the value in the LOAD register on the next clock edge, then countsdown on subsequent clocks.When the processor is halted for debugging the counter does not decrement .


介绍:处理器拥有一个24位的系统定时器 -- SysTick ,每当VALUE寄存器的值为0时,从LOAD寄存器重载初值,随后每个时钟周期递减一,直到为0。



SysTick包括四个寄存器:


①控制和状态寄存器  SysTick control and status register (STK_CTRL)


第 0 位:使能位


第 1 位:为0时,VALUE寄存器为0时,不产生任何动作;为1时,产生异常;


第 2 位:为0时,时钟周期为AHB八分频;为1时,为处理器时钟;


第 16位:VALUE寄存器为0时,自动置1;


②重载值寄存器  SysTick reload value register (STK_LOAD)


注意:以我使用的STM32开发板为例,8M外接晶振经过SystemInit函数配置后AHB=72M,STK_CTRL第二位为0,则为滴答时钟时钟周期为9M;则最大值为9/10^6*0xffffff;


③当前值寄存器  SysTick current value register (STK_VAL)


最大值0xffffff


④校准值寄存器  SysTick calibration value register (STK_CALIB)



void delay_us(u32 i)

{

u32 temp;

SysTick->LOAD=9*i; //设置重载值,72MHz时,9为1us延时

SysTick->CTRL=0X01; //使能,减到零无异常,采用外部中断源

SysTick->VAL=0;     //计数器清零

do

{

temp=SysTick->CTRL;    //读取当前CTRL,为下面判断做准备

 

}

while((temp&0x01)&&(!(temp&(1<<16)))); //中断开启,且溢出为未置位

SysTick->CTRL=0; //失能定时器

SysTick->VAL=0; //清零计数器

}


void delay_ms(u32 i)

{

u32 temp;

SysTick->LOAD=9000*i;   //1ms延时

SysTick->CTRL=0X01;

SysTick->VAL=0;

do

{

temp=SysTick->CTRL;   

}

while((temp&0x01)&&(!(temp&(1<<16))));

SysTick->CTRL=0;

SysTick->VAL=0;

}

由上文可知最大延时,9/10^6*0xffffff=1us*16777215≈16S;因此,超过20S的延时必须采用:


delay_ms(10*1000);

delay_ms(10*1000);

 

不可直接采用



delay_ms(20*1000);//将会造成延时不准确



delay_ms(10*1000);




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
浅谈AVR中定时器几种工作模式