STM32F4 (Using SysTick to accurately measure the program running time)

Publisher:心灵律动Latest update time:2018-04-23 Source: eefocusKeywords:STM32F4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the actual project development process, we often need to get the running time of a section of code. The usual method is to use an oscilloscope to measure it. This blog post will use SysTick to accurately measure the program running time. The core timer SysTick of STM32F4 is a 24-bit timer, and you need to pay attention to the maximum measurement time.


1. Development Environment

      1. Firmware library: STM32F4xx_DSP_StdPeriph_Lib_V1.8.0

      2. Compiler: ARMCC V5.06

      3. IDE: Keil uVision5

      4. Operating system: Windows 10 Professional


2. Program source code

      MeasureTime.h file

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

    Copyright © 2017 XinLi

     

  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 measuring 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 file

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

    Copyright © 2017 XinLi

     

  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. }  


Keywords:STM32F4 Reference address:STM32F4 (Using SysTick to accurately measure the program running time)

Previous article:STM32F4(LED)
Next article:STM32F4 (SRAM debugging)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号