Built in function clock() not working in atmel studio 7.0

孤街醉人 提交于 2020-01-15 10:23:05

问题


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.

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.

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

ERRORS:

recipe for target 'clocktest3.elf' failed

undefined reference to 'clock'

id returned 1 exit status

回答1:


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

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.




回答2:


this page atmel 7 indicates that the chip must have the RTC module. Does the chip your using have that module?

the following (modified) code:

  1. cleanly compiles
  2. demonstrates that the appropriate answer is a small fraction not some integer number

and now the code:

#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 = %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: %lf\n", total_t  );
    printf("Exiting of the program...\n");

    return(0);
}

The output, on my computer, of the above code is:

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...

So it seems the expectation of the OPs posted code and reality are off by (at least) two orders of magnitude.

There is no way the OPs posted output can be displayed when the OPs posted code does not link.

BTW: here is what the OPs posted code outputs before the logic/math corrections.

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...

Note the 0 for the "Total time taken by CPU"



来源:https://stackoverflow.com/questions/46335353/built-in-function-clock-not-working-in-atmel-studio-7-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!