PlatformIO ATMega324P util/delay.h 不准确

PlatformIO ATMega324P util/delay.h not accurate

我正在使用 PlatformIO 和 CLion 对 ATMega324P 微控制器进行编程。该项目是在 mac 上使用 PlatformIO 创建的,并在 CLion 中打开。我可以在 ATMega324p 上成功构建程序并运行。我运行下面的代码成功了。

main.cpp

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRB |= 1 << PINB0; 

    while (true)
    {
        PORTB ^= 1 << PINB0; 
        _delay_ms(100);
    }
}

Platformio.ini

[env:mightycore324]
platform = atmelavr
board = mightycore324
framework = arduino
upload_protocol = usbtiny
board_f_cpu = 16000000L

尽管此代码 运行 延迟似乎明显不准确。我需要做些什么来确保延迟正常工作吗?

您的 MCU 可能 运行关闭其内部 RC 振荡器。这个振荡器不是特别精确——它指定为 8 MHz,但个别部件可能 运行 7.3 到 8.1 MHz 之间的任何地方。

为了获得更准确的计时,您需要连接一个外部 crystal 并对时钟保险丝进行相应的编程。

以下设置解决了我的问题。

Platformio.ini

[env:mightycore324]
platform = atmelavr
board = mightycore324
framework = arduino
upload_protocol = usbtiny
board_f_cpu = 800000L