您现在的位置: 主页 > MCU > 单片机应用 > STM32F4(用SysTick实现精确测量程序运行的时间) -
本文所属标签:
为本文创立个标签吧:

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

来源: 网络用户发布,如有版权联系网管删除 2018-09-08 

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

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

本文引用地址: http://www.21ic.com/app/mcu/201806/764649.htm


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. *@fileMeasureTime.h

  4. *@authorXinLi

  5. *@versionv1.0

  6. *@date24-October-2017

  7. *@briefMeasureprogramruntimemodule.

  8. ******************************************************************************

  9. *@attention

  10. *

  11. *

    Copyright©2017XinLi

  12. *

  13. *Thisprogramisfreesoftware:youcanredistributeitand/ormodify

  14. *itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby

  15. *theFreeSoftwareFoundation,eitherversion3oftheLicense,or

  16. *(atyouroption)anylaterversion.

  17. *

  18. *Thisprogramisdistributedinthehopethatitwillbeuseful,

  19. *butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof

  20. *MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe

  21. *GNUGeneralPublicLicenseformoredetails.

  22. *

  23. *YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense

  24. *alongwiththisprogram.Ifnot,see.

  25. *

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

  27. */

  28. #ifndef__MEASURETIME_H

  29. #define__MEASURETIME_H

  30. #ifdef__cplusplus

  31. extern"C"{

  32. #endif

  33. /*Headerincludes-----------------------------------------------------------*/

  34. #include"stm32f4xx.h"

  35. /*Macrodefinitions---------------------------------------------------------*/

  36. /*Typedefinitions----------------------------------------------------------*/

  37. /*Variabledeclarations-----------------------------------------------------*/

  38. /*Variabledefinitions------------------------------------------------------*/

  39. /*Functiondeclarations-----------------------------------------------------*/

  40. /*Functiondefinitions------------------------------------------------------*/

  41. /**

  42. *@briefStartmeasuretime.

  43. *@paramNone.

  44. *@returnNone.

  45. */

  46. __STATIC_INLINEvoidMeasureTimeStart(void)

  47. {

  48. SysTick->CTRL|=SysTick_CLKSource_HCLK;/*SettheSysTickclocksource.*/

  49. SysTick->LOAD=0xFFFFFF;/*Timeload(SysTick->LOADis24bit).*/

  50. SysTick->VAL=0xFFFFFF;/*Emptythecountervalue.*/

  51. SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;/*Startthecountdown.*/

  52. __nop();/*Waitingforamachinecycle.*/

  53. }

  54. /**

  55. *@briefStopmeasuretime.

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

  57. *@returnProgramruntime(unit:us).

  58. */

  59. __STATIC_INLINEdoubleMeasureTimeStop(uint32_tclock)

  60. {

  61. uint32_tcount=SysTick->VAL;/*Readthecountervalue.*/

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

  63. doubletime=0.0;

  64. if(clock>0)

  65. {

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

  67. }

  68. returntime;

  69. }

  70. #ifdef__cplusplus

  71. }

  72. #endif

  73. #endif/*__MEASURETIME_H*/


main.c文件

  1. /**

  2. ******************************************************************************

  3. *@filemain.c

  4. *@authorXinLi

  5. *@versionv1.0

  6. *@date24-October-2017

  7. *@briefMainprogrambody.

  8. ******************************************************************************

  9. *@attention

  10. *

  11. *

    Copyright©2017XinLi

  12. *

  13. *Thisprogramisfreesoftware:youcanredistributeitand/ormodify

  14. *itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby

  15. *theFreeSoftwareFoundation,eitherversion3oftheLicense,or

  16. *(atyouroption)anylaterversion.

  17. *

  18. *Thisprogramisdistributedinthehopethatitwillbeuseful,

  19. *butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof

  20. *MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe

  21. *GNUGeneralPublicLicenseformoredetails.

  22. *

  23. *YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense

  24. *alongwiththisprogram.Ifnot,see.

  25. *

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

  27. */

  28. /*Headerincludes-----------------------------------------------------------*/

  29. #include"main.h"

  30. #include"MeasureTime.h"

  31. /*Macrodefinitions---------------------------------------------------------*/

  32. /*Typedefinitions----------------------------------------------------------*/

  33. /*Variabledeclarations-----------------------------------------------------*/

  34. /*Variabledefinitions------------------------------------------------------*/

  35. static__IOdoublerunTime=0.0;

  36. /*Functiondeclarations-----------------------------------------------------*/

  37. __STATIC_INLINEvoiddelay_1us(void);

  38. /*Functiondefinitions------------------------------------------------------*/

  39. /**

  40. *@briefMainprogram.

  41. *@paramNone.

  42. *@returnNone.

  43. */



              查看评论 回复



嵌入式交流网主页 > MCU > 单片机应用 > STM32F4(用SysTick实现精确测量程序运行的时间) -
 

"STM32F4(用SysTick实现精确测量程序运行的时间) -"的相关文章

网站地图

围观()