2

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?

4 Answers 4

5

The normal AVR chips used in Arduinos do not have a port A register, usually it is B/C/D .

Sign up to request clarification or add additional context in comments.

Comments

3

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);
}

Comments

0

Did you maybe forget to include some library?

1 Comment

I included avr/io.h Forgot to mention in the code. Now updated!
0

I had the same problem...

The thing is that the Arduino type is probably set to Arduino Uno, which has a different DDRA defined(I guess)... You can set the type in

TOOLS->Board->Arduino Mega 2560

if you only want to compile it and see if the code has no error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.