專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

STM32中的tic與toc,用SysTick統(tǒng)計(jì)代碼段執(zhí)行時(shí)間

作者:劉蕊飛   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月02日   【字體:

STM32中的systick,一共有4個(gè)寄存器,名稱和地址分別是:

SysTick_CTRL,        0xE000E010  --  
控制寄存器
SysTICK_LOAD,     0xE000E014  --  
重載寄存器
SysTick_VAL,        0xE000E018  --  
當(dāng)前值寄存器
SysTick_CALRB,   0xE000E01C  --   
校準(zhǔn)值寄存器

首先看SysTick->CTRL控制寄存器:寄存器內(nèi)有4個(gè)位具有意義




0位:ENABLESystick 使能位  0:關(guān)閉Systick功能;1:開啟Systick功能)
1位:TICKINT,Systick 中斷使能位    0:關(guān)閉Systick中斷;1:開啟Systick中斷)
2位:CLKSOURCESystick時(shí)鐘源選擇  0:使用HCLK/8 作為Systick時(shí)鐘;1:使用HCLK作為Systick時(shí)鐘)
16位:COUNTFLAG,Systick計(jì)數(shù)比較標(biāo)志,如果在上次讀取本寄存器后,SysTick 已經(jīng)數(shù)到了0,則該位為1。如果讀取該位,該位將自動(dòng)清零

SysTick_LOAD  
重載寄存器: 




 

SysTick_VAL
當(dāng)前值寄存器: 


也是個(gè)24位的寄存器,讀取時(shí)返回當(dāng)前倒計(jì)數(shù)的值;寫它則使之清零,同時(shí)還會(huì)清除在SysTick 控制及狀態(tài)寄存器中的COUNTFLAG 標(biāo)志。


SysTick_CALRB  
校準(zhǔn)值寄存器: 


這個(gè)寄存器好像目前的水平我還用不到,大體意思明白點(diǎn),把英文說明放這吧:
31 NOREF 1=沒有外部參考時(shí)鐘(STCLK 不可用)0=外部參考時(shí)鐘可用
30 SKEW1=校準(zhǔn)值不是準(zhǔn)確的1ms 0=校準(zhǔn)值是準(zhǔn)確的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.

類似matlab里的tic與toc函數(shù),用來統(tǒng)計(jì)程序代碼執(zhí)行需要的時(shí)間:
uint16_t OverFlowTimes=0;

void tic(void)                            //程序開始計(jì)時(shí)
{
SysTick->CTRL |= (1<<2);   //時(shí)鐘選擇,HCLK
SysTick->CTRL |= (1<<1);   //中斷使能
SysTick->VAL=0X00;            //當(dāng)前數(shù)值寄存器清零,并清除溢出標(biāo)志位
SysTick->LOAD=0XFFFFFF;     //計(jì)數(shù)器賦初值
SysTick->CTRL |= (1<<0);     //開啟計(jì)數(shù)器
}

void toc(void)                            //結(jié)束計(jì)時(shí)
{
float ElaspTime;
uint32_t ClkNum;
SysTick->CTRL &= ~(1<<0); //關(guān)閉計(jì)數(shù)器
ClkNum=SysTick->VAL; //讀取計(jì)數(shù)器的值
ElaspTime=(OverFlowTimes*((float)0xffffff/SystemCoreClock)+(float)(0xffffff-ClkNum)/SystemCoreClock); //計(jì)算時(shí)間
OverFlowTimes=0;
printf("\r\nEscaple time is %f\r\n",ElaspTime);

}
/*溢出的次數(shù)*/
void SysTick_Handler(void)
{
OverFlowTimes++;
}


 

關(guān)閉窗口

相關(guān)文章