C-内置功能时钟()在Atmel Studio 7.0中不起作用



我正在从事秒表项目,我需要阅读程序运行时已经过去的时间并从中构建时间表。

我已经包含了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 = %ldn", start_t);
    printf("Going to scan a big loop, start_t = %ldn", start_t);
    for(i=0; i< 10000000; i++)
    {
    }
    end_t = clock();
    printf("End of the big loop, end_t = %ldn", end_t);
    total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
    printf("Total time taken by CPU: %ldn", total_t  );
    printf("Exiting of the program...n");
    return(0);
}

错误:

recipe for target 'clocktest3.elf' failed
undefined reference to 'clock'
id returned 1 exit status

它显然不起作用,因为您的AVR系统中没有时钟源。

您要做的是启用一个计时器,例如timer0并将其配置为1MS滴答作响,然后在中断中的处理值或简单地读取当前计数。但是请记住,计时器可以非常快地溢出(8位或16位计时器)。

此页面Atmel 7表示芯片必须具有RTC模块。您使用的芯片是否具有该模块?

以下(修改)代码:

  1. 清洁编译
  2. 证明适当的答案是一个很小的部分,而不是某些整数编号

现在代码:

#include <time.h>
//#include <avr/io.h>
#include <stdio.h>
int main()
{
    clock_t start_t, end_t;
    double total_t;
    int i;
    start_t = clock();
    printf("Starting of the program, start_t = %ldn", start_t);
    printf("Going to scan a big loop, start_t = %ldn", start_t);
    for(i=0; i< 10000000; i++)
    {
    }
    end_t = clock();
    printf("End of the big loop, end_t = %ldn", end_t);
    total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
    printf("Total time taken by CPU: %lfn", total_t  );
    printf("Exiting of the program...n");
    return(0);
}

我的计算机上的输出上述代码是:

Starting of the program, start_t = 498
Going to scan a big loop, start_t = 498
End of the big loop, end_t = 33075
Total time taken by CPU: 0.032577
Exiting of the program...

因此,OPS已发布的代码和现实的期望似乎不超过两个数量级。

当OPS发布的代码未链接时,无法显示OPS发布的输出。

btw:这是OPS在逻辑/数学更正之前发布的代码输出。

Starting of the program, start_t = 473
Going to scan a big loop, start_t = 473
End of the big loop, end_t = 33022
Total time taken by CPU: 0
Exiting of the program...

注意" CPU花费的总时间"的0

最新更新