×
单片机 > 单片机程序设计 > 详情

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

发布时间:2020-08-31 发布时间:
|

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


     
  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 

  25.   * 

  26.   ****************************************************************************** 

  27.   */  

  28.   

  29. #ifndef __MEASURETIME_H  

  30. #define __MEASURETIME_H  

  31.   

  32. #ifdef __cplusplus  

  33. extern "C" {  

  34. #endif  

  35.   

  36. /* Header includes -----------------------------------------------------------*/  

  37. #include "stm32f4xx.h"  

  38.   

  39. /* Macro definitions ---------------------------------------------------------*/  

  40. /* Type definitions ----------------------------------------------------------*/  

  41. /* Variable declarations -----------------------------------------------------*/  

  42. /* Variable definitions ------------------------------------------------------*/  

  43. /* Function declarations -----------------------------------------------------*/  

  44. /* Function definitions ------------------------------------------------------*/  

  45.   

  46. /** 

  47.   * @brief  Start measure time. 

  48.   * @param  None. 

  49.   * @return None. 

  50.   */  

  51. __STATIC_INLINE void MeasureTimeStart(void)  

  52. {  

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

  54.   SysTick->LOAD  = 0xFFFFFF;                /* Time load (SysTick-> LOAD is 24bit). */  

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

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

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

  58. }  

  59.   

  60. /** 

  61.   * @brief  Stop measure time. 

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

  63.   * @return Program run time(unit: us). 

  64.   */  

  65. __STATIC_INLINE double MeasureTimeStop(uint32_t clock)  

  66. {  

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

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

  69.     

  70.   double time = 0.0;  

  71.     

  72.   if(clock > 0)  

  73.   {  

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

  75.   }  

  76.     

  77.   return time;  

  78. }  

  79.   

  80. #ifdef __cplusplus  

  81. }  

  82. #endif  

  83.   

  84. #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.   * 


     
  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 

  25.   * 

  26.   ****************************************************************************** 

  27.   */  

  28.   

  29. /* Header includes -----------------------------------------------------------*/  

  30. #include "main.h"  

  31. #include "MeasureTime.h"  

  32.   

  33. /* Macro definitions ---------------------------------------------------------*/  

  34. /* Type definitions ----------------------------------------------------------*/  

  35. /* Variable declarations -----------------------------------------------------*/  

  36. /* Variable definitions ------------------------------------------------------*/  

  37. static __IO double runTime = 0.0;  

  38.   

  39. /* Function declarations -----------------------------------------------------*/  

  40. __STATIC_INLINE void delay_1us(void);  

  41.   

  42. /* Function definitions ------------------------------------------------------*/  

  43.   

  44. /** 

  45.   * @brief  Main program. 

  46.   * @param  None. 

  47.   * @return None. 

  48.   */  

  49. int main(void)  

  50. {  

  51.   for(;;)  

  52.   {  

  53.     MeasureTimeStart();  

  54.     delay_1us();  

  55.     runTime = MeasureTimeStop(84);  

  56.   }  

  57. }  

  58.   

  59. /** 

  60.   * @brief  One microsecond delay. 

  61.   * @param  None. 

  62.   * @return None. 

  63.   */  

  64. __STATIC_INLINE void delay_1us(void)  

  65. {  

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

  69.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();  

  70.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();  

  71.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();  

  72.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();  

  73.   __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();  

  74.   __nop(); __nop(); __nop(); __nop();  

  75. }  




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
Keil(MDK-ARM)系列教程(七)_菜单