如何用SysTick实现测量程序运行时间

发布时间:2024-06-27  

在实际的项目开发过程中,常常遇到需要得到一段代码的运行时间,通常的方法是用示波器来测量,这篇博文将用SysTick来实现精确测量程序运行的时间。STM32F4的内核定时器SysTick是一个24位的定时器,需要注意最大的测量时间。

如何用SysTick实现测量程序运行时间

1,开发环境

1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0

2,编译器:ARMCC V5.06

3,IDE:Keil uVision5

4,操作系统:Windows 10 专业版

2,程序源码

MeasureTime.h文件

[cpp] view plain copy/**

******************************************************************************

* @file MeasureTime.h

* @author XinLi

* @version v1.0

* @date 24-October-2017

* @brief Measure program run time module.

******************************************************************************

* @attention

*

* 《h2》《center》Copyright © 2017 XinLi《/center》《/h2》

*

* This program is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with this program. If not, see 《https://www.gnu.org/licenses/》。

*

******************************************************************************

*/

#ifndef __MEASURETIME_H

#define __MEASURETIME_H

#ifdef __cplusplus

extern “C” {

#endif

/* Header includes -----------------------------------------------------------*/

#include “stm32f4xx.h”

/* Macro definitions ---------------------------------------------------------*/

/* Type definitions ----------------------------------------------------------*/

/* Variable declarations -----------------------------------------------------*/

/* Variable definitions ------------------------------------------------------*/

/* Function declarations -----------------------------------------------------*/

/* Function definitions ------------------------------------------------------*/

/**

* @brief Start measure time.

* @param None.

* @return None.

*/

__STATIC_INLINE void MeasureTimeStart(void)

{

SysTick-》CTRL |= SysTick_CLKSource_HCLK; /* Set the SysTick clock source. */

SysTick-》LOAD = 0xFFFFFF; /* Time load (SysTick-》 LOAD is 24bit)。 */

SysTick-》VAL = 0xFFFFFF; /* Empty the counter value. */

SysTick-》CTRL |= SysTick_CTRL_ENABLE_Msk; /* Start the countdown. */

__nop(); /* Waiting for a machine cycle. */

}

/**

* @brief Stop measure time.

* @param [in] clock: System clock frequency(unit: MHz)。

* @return Program run time(unit: us)。

*/

__STATIC_INLINE double MeasureTimeStop(uint32_t clock)

{

uint32_t count = SysTick-》VAL; /* Read the counter value. */

SysTick-》CTRL &= ~SysTick_CTRL_ENABLE_Msk; /* Close counter. */

double time = 0.0;

if(clock 》 0)

{

time = (double)(0xFFFFFF - count) / (double)clock; /* Calculate program run time. */

}

return time;

}

#ifdef __cplusplus

}

#endif

#endif /* __MEASURETIME_H */

main.c文件

[cpp] view plain copy/**

******************************************************************************

* @file main.c

* @author XinLi

* @version v1.0

* @date 24-October-2017

* @brief Main program body.

******************************************************************************

* @attention

*

* 《h2》《center》Copyright © 2017 XinLi《/center》《/h2》

*

* This program is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with this program. If not, see 《https://www.gnu.org/licenses/》。

*

******************************************************************************

*/

/* Header includes -----------------------------------------------------------*/

#include “main.h”

#include “MeasureTime.h”

/* Macro definitions ---------------------------------------------------------*/

/* Type definitions ----------------------------------------------------------*/

/* Variable declarations -----------------------------------------------------*/

/* Variable definitions ------------------------------------------------------*/

static __IO double runTime = 0.0;

/* Function declarations -----------------------------------------------------*/

__STATIC_INLINE void delay_1us(void);

/* Function definitions ------------------------------------------------------*/

/**

* @brief Main program.

* @param None.

* @return None.

*/

int main(void)

{

for(;;)

{

MeasureTimeStart();

delay_1us();

runTime = MeasureTimeStop(84);

}

}

/**

* @brief One microsecond delay.

* @param None.

* @return None.

*/

__STATIC_INLINE void delay_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();

}


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。

我们与500+贴片厂合作,完美满足客户的定制需求。为品牌提供定制化的推广方案、专属产品特色页,多渠道推广,SEM/SEO精准营销以及与公众号的联合推广...详细>>

利用葫芦芯平台的卓越技术服务和新产品推广能力,原厂代理能轻松打入消费物联网(IOT)、信息与通信(ICT)、汽车及新能源汽车、工业自动化及工业物联网、装备及功率电子...详细>>

充分利用其强大的电子元器件采购流量,创新性地为这些物料提供了一个全新的窗口。我们的高效数字营销技术,不仅可以助你轻松识别与连接到需求方,更能够极大地提高“闲置物料”的处理能力,通过葫芦芯平台...详细>>

我们的目标很明确:构建一个全方位的半导体产业生态系统。成为一家全球领先的半导体互联网生态公司。目前,我们已成功打造了智能汽车、智能家居、大健康医疗、机器人和材料等五大生态领域。更为重要的是...详细>>

我们深知加工与定制类服务商的价值和重要性,因此,我们倾力为您提供最顶尖的营销资源。在我们的平台上,您可以直接接触到100万的研发工程师和采购工程师,以及10万的活跃客户群体...详细>>

凭借我们强大的专业流量和尖端的互联网数字营销技术,我们承诺为原厂提供免费的产品资料推广服务。无论是最新的资讯、技术动态还是创新产品,都可以通过我们的平台迅速传达给目标客户...详细>>

我们不止于将线索转化为潜在客户。葫芦芯平台致力于形成业务闭环,从引流、宣传到最终销售,全程跟进,确保每一个potential lead都得到妥善处理,从而大幅提高转化率。不仅如此...详细>>