Mahmoud Hamdy Mahmoud Hamdy - 2 months ago 19
C Question

AVR IO Ports can not be global

I was writing an AVR code on Atmel Studio and by mistake I forgot to write the IO port settings (DDRx , PORTx ,...etc) in the main , instead I put it outside the main as shown :
The Code

then I got that error :
The error

When I added them back to the main function there were no errors

The question is why I can't add them outside the main ?
Do I miss some point ?

Answer Source

A line of code like DDRA |= 0xFF is a statement. It performs a specific action (like setting all the bits of DDRA) when it is executed.

A statement must appear in a function, so that it will be executed as part of that function. It can't be placed at the top level of a source file, because source files are not executed -- only functions are.