我正在开发一个秒表项目,我需要读取程序运行时已经过去的时间,并从中构建我的时间基准。
我已经包括了time.h
库,甚至将 .h 文件放在我的项目目录中,但由于某种原因,一旦我使用clock()
我的代码无法在此项目或我的任何 atmel 7 项目上正确构建。
我包含了一个我认为应该编译的简单编码,以及我尝试构建时遇到的错误。我怀疑这个问题与 atmel 7 有关,但任何其他建议将不胜感激。
#include <time.h>
#include <avr/io.h>
#include <stdio.h>
int main()
{
clock_t start_t, end_t, total_t;
int i;
start_t = clock();
printf("Starting of the program, start_t = %ld\n", start_t);
printf("Going to scan a big loop, start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("End of the big loop, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken by CPU: %ld\n", total_t );
printf("Exiting of the program...\n");
return(0);
}
ERRORS:
recipe for target 'clocktest3.elf' failed
undefined reference to 'clock'
id returned 1 exit status
它显然不起作用,因为你的 AVR 系统中没有时钟源。
您所要做的就是启用一个定时器,例如 TIMER0,并将其配置为 1ms 计时,然后在中断中处理值或只是读取当前计数。但请记住,定时器可能会很快溢出(8 位或 16 位定时器)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)