STM32中的tic與toc,用SysTick統(tǒng)計(jì)代碼段執(zhí)行時(shí)間
SysTick_CALRB校準(zhǔn)值寄存器:
這個(gè)寄存器好像目前的水平我還用不到,大體意思明白點(diǎn),把英文說明放這吧:
位31NOREF:1=沒有外部參考時(shí)鐘(STCLK不可用)0=外部參考時(shí)鐘可用
位30SKEW:1=校準(zhǔn)值不是準(zhǔn)確的1ms0=校準(zhǔn)值是準(zhǔn)確的1ms
位[23:0]:Calibrationvalue
IndicatesthecalibrationvaluewhentheSysTickcounterrunsonHCLKmax/8asexternalclock.Thevalueisproductdependent,pleaserefertotheProductReferenceManual,SysTickCalibrationValuesection.WhenHCLKisprogrammedatthemaximumfrequency,theSysTickperiodis1ms.Ifcalibrationinformationisnotknown,calculatethecalibrationvaluerequiredfromthefrequencyoftheprocessorclockorexternalclock.
本文引用地址:http://2s4d.com/article/201612/324399.htm
類似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("Escaple time is %f",ElaspTime);
}
/*溢出的次數(shù)*/
void SysTick_Handler(void)
{
OverFlowTimes++;
}
評論