你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F4(用SysTick实现精确测量程序运行的时间)

[复制链接]
XinLiYF 发布时间:2018-2-1 21:28
STM32F4(用SysTick实现精确测量程序运行的时间)

转载来源:STM32F4(用SysTick实现精确测量程序运行的时间)

GitHub仓库:http://github.com/XinLiGitHub/STM32F4xx_MeasureTime_Example
PS:博文不再更新,后续更新会在GitHub仓库进行。

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


1,开发环境
      1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
      2,编译器:ARMCC V5.06
      3,IDE:Keil uVision5
      4,操作系统:Windows 10 专业版

2,程序源码
      MeasureTime.h文件

  1. /**
  2.   ******************************************************************************
  3.   * @file    MeasureTime.h
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   Measure program run time module.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. #ifndef __MEASURETIME_H
  29. #define __MEASURETIME_H

  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif

  33. /* Header includes -----------------------------------------------------------*/
  34. #include "stm32f4xx.h"

  35. /* Macro definitions ---------------------------------------------------------*/
  36. /* Type definitions ----------------------------------------------------------*/
  37. /* Variable declarations -----------------------------------------------------*/
  38. /* Variable definitions ------------------------------------------------------*/
  39. /* Function declarations -----------------------------------------------------*/
  40. /* Function definitions ------------------------------------------------------*/

  41. /**
  42.   * @brief  Start measure time.
  43.   * @param  None.
  44.   * @return None.
  45.   */
  46. __STATIC_INLINE void MeasureTimeStart(void)
  47. {
  48.   SysTick->CTRL |= SysTick_CLKSource_HCLK;  /* Set the SysTick clock source. */
  49.   SysTick->LOAD  = 0xFFFFFF;                /* Time load (SysTick-> LOAD is 24bit). */
  50.   SysTick->VAL   = 0xFFFFFF;                /* Empty the counter value. */
  51.   SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* Start the countdown. */
  52.   __nop();                                  /* Waiting for a machine cycle. */
  53. }

  54. /**
  55.   * @brief  Stop measure time.
  56.   * @param  [in] clock: System clock frequency(unit: MHz).
  57.   * @return Program run time(unit: us).
  58.   */
  59. __STATIC_INLINE double MeasureTimeStop(uint32_t clock)
  60. {
  61.   uint32_t count = SysTick->VAL;             /* Read the counter value. */
  62.   SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; /* Close counter. */
  63.   
  64.   double time = 0.0;
  65.   
  66.   if(clock > 0)
  67.   {
  68.     time = (double)(0xFFFFFF - count) / (double)clock; /* Calculate program run time. */
  69.   }
  70.   
  71.   return time;
  72. }

  73. #ifdef __cplusplus
  74. }
  75. #endif

  76. #endif /* __MEASURETIME_H */
复制代码

      main.c文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   Main program body.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "main.h"
  30. #include "MeasureTime.h"

  31. /* Macro definitions ---------------------------------------------------------*/
  32. /* Type definitions ----------------------------------------------------------*/
  33. /* Variable declarations -----------------------------------------------------*/
  34. /* Variable definitions ------------------------------------------------------*/
  35. static __IO double runTime = 0.0;

  36. /* Function declarations -----------------------------------------------------*/
  37. __STATIC_INLINE void delay_1us(void);

  38. /* Function definitions ------------------------------------------------------*/

  39. /**
  40.   * @brief  Main program.
  41.   * @param  None.
  42.   * @return None.
  43.   */
  44. int main(void)
  45. {
  46.   for(;;)
  47.   {
  48.     MeasureTimeStart();
  49.     delay_1us();
  50.     runTime = MeasureTimeStop(84);
  51.   }
  52. }

  53. /**
  54.   * @brief  One microsecond delay.
  55.   * @param  None.
  56.   * @return None.
  57.   */
  58. __STATIC_INLINE void delay_1us(void)
  59. {
  60.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  61.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  62.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  63.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  64.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  65.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  66.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  67.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
  68.   __nop(); __nop(); __nop(); __nop();
  69. }
复制代码



收藏 评论4 发布时间:2018-2-1 21:28

举报

4个回答
ddllxxrr 回答时间:2018-2-2 13:55:41
我看根本就没用库
XinLiYF 回答时间:2018-2-2 19:52:34
ddllxxrr 发表于 2018-2-2 13:55
我看根本就没用库

寄存器的定义也属于库的一部分啊。
ddllxxrr 回答时间:2018-2-3 09:59:03
XinLiYF 发表于 2018-2-2 19:52
寄存器的定义也属于库的一部分啊。

定一下地址就用库了,头次听说
glenxu 回答时间:2019-8-12 12:14:33
耗时延时,没用SYSTICK  !!!???

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

相似分享

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版