Passing a Port as a variable - AVR

The register macros are basically pointers to the memory location, where the appropriate register resides, so yes, you can use uint8_t volatile *. However, the compiler will not generate the most efficient code this way -- it will use indirect addressing instead of direct writes.

This is what I do instead, using avrlib.

#include <avrlib/porta.hpp>
#include <avrlib/pin.hpp>
using namespace avrlib;

typedef pin<porta, 4> led_pin;

Then you can use the led_pin typedef, e.g.

led_pin::set();

This is how I did it, im not very experienced in AVR,

#include <avr/io.h>

void LED(volatile uint8_t* port, uint8_t pin)
{
    // First set DDRx ; DDRx on ATmega32 is one address below port address
    *(port -1) |= (1<< pin);
    // Now set the pin high
    *port |= (1<< pin);
}

int main(void)
{
   LED(&PORTB,1);
   LED(&PORTC,2);
   LED(&PORTD,3);
    while (1) 
    {
    }
}

Tags:

C++

Io

Avr