quick background: new to AVR but have programming background. Using ATmega (started arduino uno) programmed via eclipse and dapa bit banging (got to love old hardware) to play with various home project ideas.
Having little previous embedded experience I have little concern with littering my code with if/else and case statements. In an effort to be more conscious of memory use and to better understand the basics of uC I've been using more direct register access. My question concerns reading PORT into conditionals.
My concern is that a multi-level conditional that reads PORT at each comparison would have invalid values as PORT may change at any time. Ex:
if (PORTB ^ 2) { // do something } elsif (PORTB ^ 4) { // PORTB might have changed so we are // really only "if" not "else" at this point } else { // whatever }
The obvious fix is
byte portB = PORTB; // store the value if (portB ^ 2) { // do something } elsif (portB ^ 4) { // PORTB might have changed but we are using a // stored value so the current register value is // irrelevant to us } else { // whatever }
Am I understanding the nature of reading the PORT register correctly?
Sorry if this seems obvious. I did do a few searches but my search-foo is greatly hampered by a coffee deficiency. That't my excuse and i'm sticking with it.