systick 时间修改_如何用SysTick实现测量程序运行时间

本文介绍了如何利用STM32F4的内核定时器SysTick来精确测量程序运行时间,提供了 MeasureTime.h 和 main.c 的源码示例,通过启动和停止SysTick计数器计算时间间隔。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在实际的项目开发过程中,常常遇到需要得到一段代码的运行时间,通常的方法是用示波器来测量,这篇博文将用SysTick来实现精确测量程序运行的时间。STM32F4的内核定时器SysTick是一个24位的定时器,需要注意最大的测量时间。

1,开发环境

1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0

2,编译器:ARMCCV5.06

3,IDE:KeiluVision5

4,操作系统:Windows10专业版

2,程序源码

MeasureTime.h文件

[cpp]viewplaincopy/**

******************************************************************************

*@fileMeasureTime.h

*@authorXinLi

*@versionv1.0

*@date24-October-2017

*@briefMeasureprogramruntimemodule.

******************************************************************************

*@attention

*

*《h2》《center》Copyright©2017XinLi《/center》《/h2》

*

*Thisprogramisfreesoftware:youcanredistributeitand/ormodify

*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby

*theFreeSoftwareFoundation,eitherversion3oftheLicense,or

*(atyouroption)anylaterversion.

*

*Thisprogramisdistributedinthehopethatitwillbeuseful,

*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof

*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe

*GNUGeneralPublicLicenseformoredetails.

*

*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense

*alongwiththisprogram.Ifnot,see《https://www.gnu.org/licenses/》。

*

******************************************************************************

*/

#ifndef__MEASURETIME_H

#define__MEASURETIME_H

#ifdef__cplusplus

extern“C”{

#endif

/*Headerincludes-----------------------------------------------------------*/

#include“stm32f4xx.h”

/*Macrodefinitions---------------------------------------------------------*/

/*Typedefinitions----------------------------------------------------------*/

/*Variabledeclarations-----------------------------------------------------*/

/*Variabledefinitions------------------------------------------------------*/

/*Functiondeclarations-----------------------------------------------------*/

/*Functiondefinitions------------------------------------------------------*/

/**

*@briefStartmeasuretime.

*@paramNone.

*@returnNone.

*/

__STATIC_INLINEvoidMeasureTimeStart(void)

{

SysTick-》CTRL|=SysTick_CLKSource_HCLK;/*SettheSysTickclocksource.*/

SysTick-》LOAD=0xFFFFFF;/*Timeload(SysTick-》LOADis24bit)。*/

SysTick-》VAL=0xFFFFFF;/*Emptythecountervalue.*/

SysTick-》CTRL|=SysTick_CTRL_ENABLE_Msk;/*Startthecountdown.*/

__nop();/*Waitingforamachinecycle.*/

}

/**

*@briefStopmeasuretime.

*@param[in]clock:Systemclockfrequency(unit:MHz)。

*@returnProgramruntime(unit:us)。

*/

__STATIC_INLINEdoubleMeasureTimeStop(uint32_tclock)

{

uint32_tcount=SysTick-》VAL;/*Readthecountervalue.*/

SysTick-》CTRL&=~SysTick_CTRL_ENABLE_Msk;/*Closecounter.*/

doubletime=0.0;

if(clock》0)

{

time=(double)(0xFFFFFF-count)/(double)clock;/*Calculateprogramruntime.*/

}

returntime;

}

#ifdef__cplusplus

}

#endif

#endif/*__MEASURETIME_H*/

main.c文件

[cpp]viewplaincopy/**

******************************************************************************

*@filemain.c

*@authorXinLi

*@versionv1.0

*@date24-October-2017

*@briefMainprogrambody.

******************************************************************************

*@attention

*

*《h2》《center》Copyright©2017XinLi《/center》《/h2》

*

*Thisprogramisfreesoftware:youcanredistributeitand/ormodify

*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby

*theFreeSoftwareFoundation,eitherversion3oftheLicense,or

*(atyouroption)anylaterversion.

*

*Thisprogramisdistributedinthehopethatitwillbeuseful,

*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof

*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe

*GNUGeneralPublicLicenseformoredetails.

*

*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense

*alongwiththisprogram.Ifnot,see《https://www.gnu.org/licenses/》。

*

******************************************************************************

*/

/*Headerincludes-----------------------------------------------------------*/

#include“main.h”

#include“MeasureTime.h”

/*Macrodefinitions---------------------------------------------------------*/

/*Typedefinitions----------------------------------------------------------*/

/*Variabledeclarations-----------------------------------------------------*/

/*Variabledefinitions------------------------------------------------------*/

static__IOdoublerunTime=0.0;

/*Functiondeclarations-----------------------------------------------------*/

__STATIC_INLINEvoiddelay_1us(void);

/*Functiondefinitions------------------------------------------------------*/

/**

*@briefMainprogram.

*@paramNone.

*@returnNone.

*/

intmain(void)

{

for(;;)

{

MeasureTimeStart();

delay_1us();

runTime=MeasureTimeStop(84);

}

}

/**

*@briefOnemicroseconddelay.

*@paramNone.

*@returnNone.

*/

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

}

在嵌入式开发中,测量程序运行时间是优化性能和调试的重要手段。CS+(CubeSuite+)是由瑞萨电子提供的一款集成开发环境(IDE),主要用于RX、RL78等系列MCU的开发。虽然CS+主要面向瑞萨微控制器,但其测量程序执行时间的方法与其它IDE类似。 ### 使用硬件定时器测量程序运行时间 可以通过单片机内部的定时器模块来实现对代码段执行时间的精确测量。以STM32为例,使用通用定时器(如TIM2或TIM3)可以实现高精度的时间测量。 ```c #include "stm32f10x.h" void delay_us(uint32_t us) { // 假设系统时钟为72MHz,预设计数器值为72-1,使计数频率为1MHz SysTick->LOAD = 72 - 1; SysTick->VAL = 0; SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; for (; us > 0; us--) { while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)); } SysTick->CTRL = 0; } uint32_t measure_execution_time(void (*func)(void)) { uint32_t start, end; start = HAL_GetTick(); func(); end = HAL_GetTick(); return end - start; } ``` 此方法适用于粗略测量函数执行时间[^1]。 ### 使用调试接口(如SWO)测量时间 如果目标芯片支持SWO(Serial Wire Output)功能,可以通过ITM(Instrumentation Trace Macrocell)输出时间戳,从而测量代码段的执行时间。这种方法通常需要配置调试器(如ST-Link或J-Link)并启用ITM功能。 ```c #include "core_cm3.h" void enable_ITM(void) { // 启用ITM和DWT CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; } uint32_t get_cycle_count(void) { return DWT->CYCCNT; } // 示例:测量某个函数执行周期数 void example_function(void) { for (volatile int i = 0; i < 1000; i++); } int main(void) { enable_ITM(); uint32_t start = get_cycle_count(); example_function(); uint32_t end = get_cycle_count(); uint32_t cycles = end - start; // 输出cycles变量到调试控制台 } ``` 该方法适合进行细粒度的时间测量,尤其是在没有操作系统干预的情况下。 ### 使用CS+内置的调试功能 在CS+中,开发者可以通过调试界面设置断点,并利用“Run to Cursor”功能配合观察寄存器或变量变化来估算时间。此外,CS+还支持查看指令执行周期的功能,具体操作可能因版本不同而有所差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值