本帖最后由 XinLiOV 于 2018-2-1 22:51 编辑
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文件
[mw_shl_code=c,true]/**
******************************************************************************
* @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 */
[/mw_shl_code]
main.c文件
[mw_shl_code=c,true]/**
******************************************************************************
* @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();
}
[/mw_shl_code]
一周热门 更多>