C:对 'clock()' 的 undefined reference

标签 c

我对 C 不太熟悉,所以请耐心等待......

我正在为 Atmel Tiny 85 上的看门狗定时器编写一些简单的代码,并在 AtmelStudio 中进行开发。我想访问time.h库的clock()函数:

#include <time.h>

.....

void main() {
  clock_t start = clock();
  ....
}

不幸的是,编译器提示时钟是一个 undefined reference 。我在网上查找的每个代码示例似乎都在做我正在做的事情。我缺少一些基本的东西吗?谢谢!

完整代码如下:

#define F_CPU 1000000UL // 1 MHz
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <time.h>
#include <stdio.h>
#include <avr/wdt.h>
#include <stdint.h>
#include <util/atomic.h>

#define LED_PORT PB1
#define RST0_PORT PINB3
#define RST1_PORT PINB4
#define HEARTBEAT PINB0

volatile int HEARTBEAT_RECEIVED;
#define CLOCKS_PER_SECOND 1000000;
const int TIMEOUT_PERIOD = 120; //reset raspberry pi after this many seconds

void set_output();
void set_input();
void set_interrupts();
void run_timer(clock_t start);
void restart_raspberry_pi();

int main(void)
{
    clock_t start;

    HEARTBEAT_RECEIVED = false;

    //Configure IO pins
    set_output();
    set_input();
    PORTB &= ~(1 << RST0_PORT); // Set pin low (common ground pin for pi reset)
    PORTB &= ~(1 << RST1_PORT); // Set pin low (initialize for no pi reset)

    //Configure and enable interrupts
    set_interrupts();

    while (1) {
        sei();
        start = clock();
        run_timer(start);
    }

    return (0);
}


void run_timer(clock_t start) {

    double time_elapsed;

    do {
        _delay_ms(1000);

        if (HEARTBEAT_RECEIVED) { //If heartbeat detected, reset timer.
            HEARTBEAT_RECEIVED = false;
            return;
        }

        time_elapsed = ( clock() - start ) / CLOCKS_PER_SECOND;
    } while (time_elapsed < TIMEOUT_PERIOD);

    restart_raspberry_pi(); //Timeout period has elapsed, reset the pi
}

ISR(PCINT0_vect) {
    //Indicate that a heartbeat has been received
    HEARTBEAT_RECEIVED = true;
}

void restart_raspberry_pi() {
    cli();

    PORTB |= (1 << RST1_PORT); // Set pin high (sets RUN high to reset)
    _delay_ms(500);
    PORTB &= ~(1 << RST1_PORT); // Set pin low (release reset control)
}

void set_output()
{
    //The DDxn bit in the DDRx Register selects the direction of this pin.
    //If DDxn is written logic one, Pxn is configured as an output pin.
    //If DDxn is written logic zero, Pxn is configured as an input pin.

    //PORTB = (0<<PB0) | (1<<PB3);
    DDRB = (1<<DDB5) | (1<<DDB4) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1); // Set pins as output.
}

void set_input()
{
    DDRB |= (0<<DDB0); // set pin 0 as input. Added |=.
}

void set_interrupts()
{
    GIMSK |= (1 << PCIE);   // pin change interrupt enable
    PCMSK |= (1 << PCINT0); // pin change interrupt enabled for PCINT0
}

最佳答案

@Shawn 说得对......我深入研究了我可用的 time.h 库代码,显然:

Section 7.23.2.1 clock() The type clock_t, the macro CLOCKS_PER_SEC, and the function clock() are not implemented. We consider these items belong to operating system code, or to application code when no operating system is present.

所以我想这对我来说是一个教训。感谢您的帮助。

关于C:对 'clock()' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53734909/

相关文章:

具有无限循环和找零计数器 c 的收银机

c - 在 OS X 中读取其他进程的内存?

c - k&r 与位操作混淆

将 x86 汇编代码片段转换为 C

c - 如何在 Xamarin.iOS 中正确包装 native c 库

c - 编写我自己的增量压缩?

使用 clang 编译最小测试共享库

是否可以使用本地端口通过 UDP 连接到多个远程对等点?

c - 在c中逐行读取文件

c - 初始化结构数组奇怪的行为