title: Passing a Port as a variable - AVR
body: <p>Is it possible to use an AVR port as a variable which can be passed around?</p>
  
  <p>For example </p>
  
  <pre><code>LED myLed(PORTA,7);   //myLED hooked to PORTA, Pin 7
  </code></pre>
  
  <p>I would like to make LED be able to take any PORT / Pin combination, so I would rather not hard code it in.</p>
  
  <p>Note that the PORTs are defined as:</p>
  
  <pre><code>#define PINA  _SFR_IO8(0x00)
  #define DDRA  _SFR_IO8(0x01)
  #define PORTA _SFR_IO8(0x02)    
  </code></pre>
  
  <p>PORTA symbol resolves to (*(volatile uint8_t *)((0x02) + 0x20))</p>
  
  <p>I believe this would allow me to do something like the following, but I am unsure whether I would need the volatile keyword or not, nor whether it will actually work as expected</p>
  
  <pre><code>class LED{
  public:
      LED(volatile uint8_t* port, uint8_t pin);
      {
          Port=port;
          Pin=pin;
      }
      void write(bool val)
      {
          if(val) (*Port) |= 1 &lt;&lt; Pin;
          else    (*Port) &amp;= ~(1 &lt;&lt; Pin);
      }
  
  private:
      uint8_t  Pin
      volatile uint8_t* Port;
  }
  </code></pre>
  
  <p>Finally, is there a way to set Port / Pin as an Output from the LED Constructor?
  This would involve finding the relative DDR# register for the Given PORT#.
  Can I assume &amp;DDR# will always be &amp;PORT#-1?</p>