在實際的項目開發過程中,常常遇到需要得到一段代碼的運行時間,通常的方法是用示波器來測量,這篇博文將用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文件
[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文章
194瀏覽量
28057 -
Systick
+關注
關注
0文章
62瀏覽量
13092
發布評論請先 登錄
相關推薦
評論