Search code examples
ccompiler-errorsmicrocontrolleravratmelstudio

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:

#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
#include <avr/interrupt.h>

void delay_250ms();
uint8_t t_hr = 0 , t_min = 0 , t_sec = 0 , i = 0 , tempp;
uint8_t start_stopwatch = 0;
DDRA |= 0xFF;
PORTA = 0x00;
DDRD = 0x00;
PORTD |= 0xFF;

then I got that error:

  • expected ) before ( token
  • expected identifier or ( before volatile
  • expected ) before ( token
  • expected identifier or ( before volatile
  • expected ) before ( token …

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 ?


Solution

  • 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.