本帖最后由 yangyurong 于 2024-12-26 12:04 编辑
还是接上篇,我们修改KEIL工程的源代码如下
1.头文件
/* Includes */
#include "user_config.h"
#include "board.h"
#include <stdio.h>
#include "code_time_measurement.h"
#include <stdbool.h>

2.主函数代码
int main(void)
{
CLOCK_SYS_Init(&g_clockConfig);
CODE_TIME_START("开始执行代码时间测量");
COM_Init();
CODE_TIME_EVENT("COM_Init()");
LED_Init();
CODE_TIME_EVENT("LED_Init()");
RTC_Initialize();
CODE_TIME_EVENT("RTC_Initialize()");
CODE_TIME_STOP();
while(1)
{
}
}

3.打开串口助手查看函数执行时间

可以看到串口初始化花了60us,LED初始化花了10us,RTC初始化花了30us
4.上面是在初始化中测试时间,同样的,也可以在while(1)主函数中测量某个代码执行时间,下面我们以LED_Toggle(LED_GREEN);进行测量
while(1)
{
CODE_TIME_START("测量主函数执行时间");
LED_Toggle(LED_GREEN);
CODE_TIME_EVENT("LED_Toggle(LED_GREEN)");
CODE_TIME_STOP();
}
打开串口助手,查看
可以看到LED_Toggle(LED_GREEN)只花费了1us时间,非常迅速,毕竟只是LED灯翻转
|