内置函数Clock()在Atmel Studio 7.0中不起作用 [英] Built in function clock() not working in atmel studio 7.0

查看:271
本文介绍了内置函数Clock()在Atmel Studio 7.0中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个秒表项目,我需要阅读程序运行时所经过的时间,并据此建立我的时基.

I am working on a stopwatch project and I need to read the time that has passed while the program is running and build my time base from that.

我已经包含了time.h库,甚至将.h文件放在我的项目目录中,但是由于某些原因,一旦我使用clock()函数,我的代码就无法在此或任何atmel 7项目上正确构建

I've included the time.h library and even put the .h file in my project directory but for some reason once I use clock() function my code doesn't build correctly on this or any of my atmel 7 projects.

我提供了一个我认为应该编译的简单编码,以及尝试构建时遇到的错误.我怀疑该问题与Atmel 7有关,但任何其他建议将不胜感激.

I included a simple coded that I believe should compile, as well as the errors I get when I try and build. I suspect the problem has something to do with atmel 7, but any other suggestions would be appreciated.

#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);
}

错误:

recipe for target 'clocktest3.elf' failed

undefined reference to 'clock'

id returned 1 exit status

推荐答案

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

It obviously don't work because there is no clock source in your AVR system.

您要做的是启用一个定时器(例如TIMER0)并将其配置为1ms滴答,然后在中断中处理值或仅读取当前计数.但是请记住,计时器可能很快溢出(8位或16位计时器).

What you have to do is to enable one timer, for example TIMER0 and configure it as 1ms ticking and then process values in interrupts or simply read current count. But keep in mind that timer can overflow (8-bit or 16-bit timer) very fast.

这篇关于内置函数Clock()在Atmel Studio 7.0中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆