为什么当我把它添加到json中时,智能感知说它找不到DDRB定义,它甚至可以让我偷看定义?Visual Studio Code

svmlkihl  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(59)

我有这个简单的blinky.c代码为AVR微控制器。它编译和运行没有问题。但是我的vscode中的智能感知有点疯狂

#define F_CPU 16000000UL
#define LED_PIN 0

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

void main()
{
    DDRB |= (1 << LED_PIN);
    while(1)
    {
        PORTB ^= (1 << LED_PIN);
        _delay_ms(500);
    }
}

首先,我编辑json以添加所需的路径:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/avr/include",
                "/usr/avr/include/avr"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "gcc-x86",
            "compilerArgs": [
                "-Wall "
            ]
        }
    ],
    "version": 4
}

它可以看到函数等,除了DDRBPORTB

但是当我去“去定义”它甚至让我看到它。那么为什么它会给我这个智能感知错误呢?

要明确的是,我现在在Linux上(Manjaro)。我已经安装了avr s到pamac所需的所有库,它们都可以工作。

gwo2fgha

gwo2fgha1#

似乎是因为编译成功,编译器在某个编译器选项中定义了__AVR_ATmega328P__。可能是-mmcu=<mcu>
当您在源代码中定义它时,它可能会与那些编译器选项混淆。
一个“更好”的方法是正确地设置您的智能感知配置文件,这样它就不会影响您的编译。
c_cpp_properties.json:

{
"configurations": [
    {
        ...
        "defines": ["__AVR_ATmega328P__"],
        ...
    }
],
"version": 4
}
bxgwgixi

bxgwgixi2#

好吧,我找到解决办法了。它是由avr/io.h文件的内部结构引起的。它包括一个特定的文件,其中定义基于源代码中给定微控制器的#define。所以当我不指定控制器时,它不会在io.h#include,因此它对智能感知不可见,但编译可以看到它,因为我指定了它。所以为了使io.h包含定义,我们需要。我们必须告诉它我们将使用哪个控制器。像这样:

#define __AVR_ATmega328P__

所以现在我的代码看起来像这样:

#define F_CPU 16000000UL
#define LED_PIN 0
#define __AVR_ATmega328P__

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

void main()
{
    DDRB |= (1 << LED_PIN);
    while(1)
    {
        PORTB ^= (1 << LED_PIN);
        _delay_ms(500);
    }
}

你可以在io.h文件中清楚地看到它:

#if defined (__AVR_AT94K__)
#  include <avr/ioat94k.h>
#elif defined (__AVR_AT43USB320__)
#  include <avr/io43u32x.h>
#elif defined (__AVR_AT43USB355__)
#  include <avr/io43u35x.h>
#elif defined (__AVR_AT76C711__)
#  include <avr/io76c711.h>
#elif defined (__AVR_AT86RF401__)
#  include <avr/io86r401.h>
#elif defined (__AVR_AT90PWM1__)
#  include <avr/io90pwm1.h>
...

这基本上是500行检查我们使用的控制器。如果你去这个文件,用ctrl+f或类似的东西,你可以很容易地找到你的控制器是如何定义的,并包括它。这解决了问题,因为DDRBPORTB现在通过解析的预处理器语句定义。

yfwxisqw

yfwxisqw3#

解决这个问题的另一种方法是包含avr/io.havr/ioXXXXX.h:就像...

[line 0] #include <avr/io.h>
[line 1] #include <avr/ioXXXXX.h>

当然,你可以使用#define __AVR_XXXXX__来代替第1行,如io.h文件所示:

#if defined (__AVR_AT94K__)
#  include <avr/ioat94k.h>
#elif defined (__AVR_AT43USB320__)
#  include <avr/io43u32x.h>
#elif defined (__AVR_AT43USB355__)
#  include <avr/io43u35x.h>
#elif defined (__AVR_AT76C711__)
#  include <avr/io76c711.h>
#elif defined (__AVR_AT86RF401__)
#  include <avr/io86r401.h>
#elif defined (__AVR_AT90PWM1__)
#  include <avr/io90pwm1.h>

.......

相关问题