硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3272|回复: 1
收起左侧

[客户分享] STM32F4(用SysTick实现精确测量程序运行的时间)

[复制链接]

19

主题

15

回帖

72

积分

初级会员

积分
72
发表于 2018-2-1 23:45:34 | 显示全部楼层 |阅读模式
STM32F4(用SysTick实现精确测量程序运行的时间)

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

GitHub仓库:https://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 &#169; 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 <https://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 &#169; 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 <https://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. }
复制代码



评分

参与人数 1金币 +50 收起 理由
eric2013 + 50 赞一个!

查看全部评分

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107628
QQ
发表于 2018-2-2 01:38:57 | 显示全部楼层
非常感谢楼主分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-6-8 20:34 , Processed in 0.149852 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表