在实际的项目开发过程中,常常遇到需要得到一段代码的运行时间,通常的方法是用示波器来测量,这篇博文将用SysTick来实现精确测量程序运行的时间。STM32F4的内核定时器SysTick是一个24位的定时器,需要注意最大的测量时间。
1,开发环境
1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
2,编译器:ARMCCV5.06
3,IDE:KeiluVision5
4,操作系统:Windows10专业版
2,程序源码
MeasureTime.h文件
[cpp]viewplaincopy/**
******************************************************************************
*@fileMeasureTime.h
*@authorXinLi
*@versionv1.0
*@date24-October-2017
*@briefMeasureprogramruntimemodule.
******************************************************************************
*@attention
*
*《h2》《center》Copyright©2017XinLi《/center》《/h2》
*
*Thisprogramisfreesoftware:youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation,eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNUGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram.Ifnot,see《https://www.gnu.org/licenses/》。
*
******************************************************************************
*/
#ifndef__MEASURETIME_H
#define__MEASURETIME_H
#ifdef__cplusplus
extern“C”{
#endif
/*Headerincludes-----------------------------------------------------------*/
#include“stm32f4xx.h”
/*Macrodefinitions---------------------------------------------------------*/
/*Typedefinitions----------------------------------------------------------*/
/*Variabledeclarations-----------------------------------------------------*/
/*Variabledefinitions------------------------------------------------------*/
/*Functiondeclarations-----------------------------------------------------*/
/*Functiondefinitions------------------------------------------------------*/
/**
*@briefStartmeasuretime.
*@paramNone.
*@returnNone.
*/
__STATIC_INLINEvoidMeasureTimeStart(void)
{
SysTick-》CTRL|=SysTick_CLKSource_HCLK;/*SettheSysTickclocksource.*/
SysTick-》LOAD=0xFFFFFF;/*Timeload(SysTick-》LOADis24bit)。*/
SysTick-》VAL=0xFFFFFF;/*Emptythecountervalue.*/
SysTick-》CTRL|=SysTick_CTRL_ENABLE_Msk;/*Startthecountdown.*/
__nop();/*Waitingforamachinecycle.*/
}
/**
*@briefStopmeasuretime.
*@param[in]clock:Systemclockfrequency(unit:MHz)。
*@returnProgramruntime(unit:us)。
*/
__STATIC_INLINEdoubleMeasureTimeStop(uint32_tclock)
{
uint32_tcount=SysTick-》VAL;/*Readthecountervalue.*/
SysTick-》CTRL&=~SysTick_CTRL_ENABLE_Msk;/*Closecounter.*/
doubletime=0.0;
if(clock》0)
{
time=(double)(0xFFFFFF-count)/(double)clock;/*Calculateprogramruntime.*/
}
returntime;
}
#ifdef__cplusplus
}
#endif
#endif/*__MEASURETIME_H*/
main.c文件
[cpp]viewplaincopy/**
******************************************************************************
*@filemain.c
*@authorXinLi
*@versionv1.0
*@date24-October-2017
*@briefMainprogrambody.
******************************************************************************
*@attention
*
*《h2》《center》Copyright©2017XinLi《/center》《/h2》
*
*Thisprogramisfreesoftware:youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation,eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNUGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram.Ifnot,see《https://www.gnu.org/licenses/》。
*
******************************************************************************
*/
/*Headerincludes-----------------------------------------------------------*/
#include“main.h”
#include“MeasureTime.h”
/*Macrodefinitions---------------------------------------------------------*/
/*Typedefinitions----------------------------------------------------------*/
/*Variabledeclarations-----------------------------------------------------*/
/*Variabledefinitions------------------------------------------------------*/
static__IOdoublerunTime=0.0;
/*Functiondeclarations-----------------------------------------------------*/
__STATIC_INLINEvoiddelay_1us(void);
/*Functiondefinitions------------------------------------------------------*/
/**
*@briefMainprogram.
*@paramNone.
*@returnNone.
*/
intmain(void)
{
for(;;)
{
MeasureTimeStart();
delay_1us();
runTime=MeasureTimeStop(84);
}
}
/**
*@briefOnemicroseconddelay.
*@paramNone.
*@returnNone.
*/
__STATIC_INLINEvoiddelay_1us(void)
{
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();
}