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

描述

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

STM32F4

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();

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分