Is it possible to use an AVR port as a variable which can be passed around?
For example
LED myLed(PORTA,7); //myLED hooked to PORTA, Pin 7
I would like to make LED be able to take any PORT / Pin combination, so I would rather not hard code it in.
Note that the PORTs are defined as:
#define PINA _SFR_IO8(0x00)
#define DDRA _SFR_IO8(0x01)
#define PORTA _SFR_IO8(0x02)
PORTA symbol resolves to (*(volatile uint8_t *)((0x02) + 0x20))
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
class LED{
public:
LED(volatile uint8_t* port, uint8_t pin);
{
Port=port;
Pin=pin;
}
void write(bool val)
{
if(val) (*Port) |= 1 << Pin;
else (*Port) &= ~(1 << Pin);
}
private:
uint8_t Pin
volatile uint8_t* Port;
}
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 &DDR# will always be &PORT#-1?
struct port{ UINT8 _port_1_0:1; UINT8 _port_1_2:1; ... };- this is better/nicer in C. But if you have C++ and templates avakar example is better.