0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

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

算法&编程学院 来源:网络整理 作者:工程师3 2018-05-09 14:07 次阅读

在实际的项目开发过程中,常常遇到需要得到一段代码的运行时间,通常的方法是用示波器来测量,这篇博文将用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();

}

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

    关注

    3

    文章

    192

    浏览量

    27658
  • Systick
    +关注

    关注

    0

    文章

    62

    浏览量

    12938
收藏 人收藏

    评论

    相关推荐

    如何缩短Vivado的运行时间

    在Vivado Implementation阶段,有时是有必要分析一下什么原因导致运行时间(runtime)过长,从而找到一些方法来缩短运行时间
    的头像 发表于 05-29 14:37 1.4w次阅读
    如何缩短Vivado的<b class='flag-5'>运行时间</b>

    获取单片机运行时间

    测试代码的运行时间的两种方法 使用单片机内部定时器,在待测程序段的开始启动定时器,在待测程序段的结尾关闭定时器。为了测量的准确性,要进行多次测量
    的头像 发表于 08-26 20:26 1535次阅读
    获取单片机<b class='flag-5'>运行时间</b>

    LabVIEW程序运行时间与循环时间冲突

    的是:1,如何使循环读取的时间运行时间一致; 2,使用定时循环结构设定循环时间为什么不能达到循环时间运行时间一致。
    发表于 11-30 14:24

    请问6747如何测量代码运行时间

    1. 我想用片上的硬件定时器的方法测量代码运行时间,使用timer,加头文件:csl_timer.h,但是6747没有csl,我下载了6747的cslr package,发现里面也没有
    发表于 07-28 10:25

    请问stm32如何利用通用定时器实现函数运行时间精确测量

    请问stm32如何利用通用定时器实现函数运行时间精确测量
    发表于 12-01 07:58

    测量嵌入式软件运行时间的方法

    关注、星标公众号,不错过精彩内容整理:黄工素材来源:最后一个Bug程序运行时间,对一个系统比较重要。有的地方要求精确延时Nus,有的地方要求程序运行时间不能超过Nus。所以,今天给大
    发表于 12-21 08:21

    介绍几种测量单片机程序运行时间的方法

      有时我们需要知道自己的单片机程序需要花费多长时间,delay延时的精确时间等。今天来介绍几种测量程序运行时间的方法。  1.单片机内部定
    发表于 03-23 14:55

    C语言教程之显示程序运行时间

    C语言教程之显示程序运行时间,很好的C语言资料,快来学习吧。
    发表于 04-25 16:09 0次下载

    基于STM32单片机通过使用宏assert_param来实现运行时间检测

    固件函数库通过检查库函书的输入来实现运行时间错误侦测。通过使用宏assert_param来实现运行时间检测。所有要求输入参数的函数都使用这个宏。它可以检查输入参数是否在允许的范围之内。
    发表于 10-22 15:12 1312次阅读
    基于STM32单片机通过使用宏assert_param来<b class='flag-5'>实现</b><b class='flag-5'>运行时间</b>检测

    电机运行时间进行排列 是分为两个部分来完成这个程序的设计的

    前几天有个学员咨询一个程序设计的问题,程序的控制要求如下:需要控制5台电机的运行,每台电机运行时需要记录运行时间,电机启动
    的头像 发表于 07-19 08:57 6419次阅读
    电机<b class='flag-5'>运行时间</b>进行排列 是分为两个部分来完成这个<b class='flag-5'>程序</b>的设计的

    如何高效测量ECU的运行时间

    ,最终可能会引起运行时间方面的问题。这在项目后期需要大量的时间和金钱来解决。如果不能掌握系统的运行状态,则很难发现系统内缺陷的根源。 解决方案 将TA软件工具套件与VX1000测量标定
    的头像 发表于 10-28 11:05 1815次阅读

    浅析STM32代码运行时间的技巧

    前言     测试代码的运行时间的两种方法: 使用单片机内部定时器,在待测程序段的开始启动定时器,在待测程序段的结尾关闭定时器。为了测量的准确性,要进行多次
    的头像 发表于 11-09 09:52 3328次阅读
    浅析STM32代码<b class='flag-5'>运行时间</b>的技巧

    几种测量程序运行时间的方法

    如果你的程序没有多余的定时器可用,那么可以借助外部工具来测量。在待测程序开始时将一个GPIO置高,在待测程序结束时将GPIO置低。用示波器或者其他设备
    发表于 08-03 14:35 2588次阅读

    AN021 测量MCU代码运行时间的几种方法

    AN021 测量MCU代码运行时间的几种方法
    发表于 02-27 18:23 0次下载
    AN021 <b class='flag-5'>测量</b>MCU代码<b class='flag-5'>运行时间</b>的几种方法

    ch32v307记录程序运行时间

    ch32v307记录程序运行时间程序开发中,很重要的一项任务就是对程序运行时间进行评估。对于大型的
    的头像 发表于 08-22 15:53 425次阅读