Search code examples
arduinoavr

How to compile AVR code in Arduino?


Why does the following code not work in Arduino?

#include<avr/io.h>
void setup()
{
    DDRA = 0xFF;
}
void loop()
{
    PORTA = 0xAA;
    _delay_ms(1000);
    PORTA = 0x55;
    _delay_ms(1000);
}

I get the following error. "DDRA was not declared in this scope."

As I know, arduino uses AVR microcontrollers, so why can't we use AVR code in arduino boards?


Solution

  • User261391 has the first issue with your code. You will then quickly find you also need to include delay.h for the delay to work.

    Revised Example:

    #include<avr/io.h>
    #include<avr/delay.h>
    void setup()
    {
        DDRB = 0xFF;
    }
    void loop()
    {
        PORTB = 0xAA;
        _delay_ms(1000);
        PORTB = 0x55;
        _delay_ms(1000);
    }