[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Best practice for external variable (newbie-ish quest

From: Russell Shaw
Subject: Re: [avr-gcc-list] Best practice for external variable (newbie-ish question)
Date: Sun, 20 Nov 2005 12:15:48 +1100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.11) Gecko/20050914 Debian/1.7.11-1

Matthew Smith wrote:
Hi All

I am writing a small programme where I need a couple of variables to be
accessible to various routines, including an ISR.

I have declared the variables in the main body of the programme as
volatile, as I believe that this is required for access by the ISR:

volatile uint8_t foo;
volatile uint8_t bar;

1) Am I correct in thinking that I need to declare this in the ISR thus:

        extern uint8_t foo;
        // Other stuff

yes, but maybe it should be:

  extern volatile uint8_t foo;
  // Other stuff

2) Is this the correct way to access it in an ordinary routines, or
should I be doing something with references?

void startclock(void)
        extern uint8_t bar;
        // Various stuff

Is ok because it is an atomic (single-part) variable.

Comments/suggestions would be greatly appreciated.

If you modify an integer or other multi-byte variable
that is also used in an isr, you should disable the isr
so that the variable is not read or written while
manipulation on it is only half complete.

Also, there is less chance of errors if you put

  extern volatile uint8_t foo;
  extern volatile uint8_t bar;

in the header file that belongs to the file where they are defined,
and #include the header wherever you need them.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]