[Top][All Lists]

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

Re: [avr-gcc-list] efficiency of assigning bits

From: E. Weddington
Subject: Re: [avr-gcc-list] efficiency of assigning bits
Date: Mon, 14 Mar 2005 13:12:53 -0700
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

David Willmore wrote:

Yes. PORTD and all of the AVR registers are defined as volatile. That is why you would want to combine the statements if you want simultaneous changing of the state of those two pins. If you don't care, then you're free to have them as two statements, but it would also be inefficient in terms of code space. Your choice.

Ahh, okay, thank you.  I knew it had to be something like that which
I was missing.  I assume this was done to keep newbies from asking a
bunch of questions and having to learn what a volatile is.  Hmm, I'd
be tempted to have another define that doesn't declare them as volatile
so that the compiler can optimize them more.  I guess it would be smarter
to just use a temp variable for that and then assign it to the port
where I would have put a 'memory barrier' otherwise.  So, guess it's
pretty prudent to declare them volatile after all. Just took me a while to come to that realization. :)

It's prudent to declare them as volatile because you *don't* want the optimizer to optimize away statements involving hardware registers. It's important that when you set or clear a bit in a register that it actually happens in the way and order that you specify in order to control the hardware and also because the hardware may change those values in ways that the compiler cannot see beforehand.


PS. Reply to the list and not to me personally.

reply via email to

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