Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code's intellisense doesn't recognize Arduino port managing

I'm trying to use VScode for Arduino Uno early development. I'd like to use intellisense but in the "Problems tab" it shows me errors:

identifier "DDRD" is undefined
identifier "PIND" is undefined
identifier "PORTD" is undefined
identifier "PORTD" is undefined

I can compile and upload the code flawlessly and it works.

I read everywhere that the solution to this is adding at the very top of the code the macro definition of the microcontroller, but it seems that this doesn't work for me only.

This is my code

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

int main(void) {
    int switchState = 0;

    DDRD &= ~(1 << 2);
    DDRD |= 1 << 3;
    DDRD |= 1 << 4;
    DDRD |= 1 << 5;

    for (;;) {
        switchState = 0x01 & (PIND >> 2);

        if (!switchState) {
            PORTD |= 1 << 3;
            PORTD &= ~(1 << 4);
            PORTD &= ~(1 << 5);
        } else {
            PORTD &= ~(1 << 3);
            PORTD &= ~(1 << 4);
            PORTD |= 1 << 5;
            _delay_ms(250);
            PORTD |= 1 << 4;
            PORTD &= ~(1 << 5);
            _delay_ms(250);
        }
    }

    return 0;
}

If I right-click a DDRD underlined macro and select "Go to definition" it brings me to the DDRD macro definition in io76c711.h, while I expect been redirected in iom328p.h, because io.h contains

#elif defined (__AVR_ATmega328P__)
#  include <avr/iom328p.h>

It seems that AVR_AT76C711 is defined somewhere else but I can't wonder where.

Do you have any suggestion? I also post my c_cpp_properties.json if there is something wrong in the one generated by the VScode Arduino extension

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\Program Files (x86)\\Arduino\\tools\\**",
                "D:\\Arduino\\libraries\\**",
                "C:\\Program Files (x86)\\Arduino\\libraries\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\**",
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**"
            ],
            "forcedInclude": [
                "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h"
            ],
            "defines": [
                "USBCON"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14"
        }
    ],
    "version": 4
}

1 Answers

Add "__AVR_ATmega328P__" to the define section of your c_cpp_properties.json file as follows:

"defines": [
    "__AVR_ATmega328P__",

Then it will be able to find the correct AVR IO include file.

In your case, AVR_AT76C711 was just one possibility for the definition, but it likely wasn't actually defined in your project.

Note: the define will automatically be removed the next time that the IntelliSense Configuration is rebuilt (e.g.: Verify or Upload). This issue is currently tracked here.

like image 110
gunderbolt Avatar answered Oct 26 '25 07:10

gunderbolt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!