0

I'm programming with an AVR and I'm trying to create a function to set a pin as an output. I made a struct to hold the Register and Pin number like this:

typedef struct Pin{
    uint8_t pinNo;
    volatile uint8_t* pinReg;
};

I then have this function to set a pin an output.

void pin_output(struct Pin pin){
    //DDRA |= _BV(DDA6); 
    *(pin.pinReg) |= _BV(pin.pinNo);
}

Called like this:

struct Pin pin6;
pin6.pinNo = DDA6;
pin6.pinReg = DDRA; 
pin_output(pin6);

This works fine but the led brightness very dim. If I set the output like this:

DDRA |= _BV(DDA6);

The pin is at its expect brightness about 3-5x more bright then with the function. What is getting lost in the function causing the problem with the brightness?

Nick
  • 9,285
  • 33
  • 104
  • 147

1 Answers1

2

Apparently macro DDRA hides some sort of lvalue tied to some hardware register. Changing that lvalue changes the register and the brightness. If the actual type if that lvalue is uint8_t, then in order to pass it through your struct Pin you should initialize the corresponding member of your struct as

pin6.pinReg = &DDRA; 

Note the & operator.

The pin6.pinReg = DDRA that you have in your code should not even compile or at least should produce warnings for incompatible types in assignment.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Sorry that was some debug code left over. I updated question. I assigned pin6.pinReg as you said but the brightness is still dim. – Nick Oct 03 '13 at 21:14
  • @Nick: There's no reason for it not to work. Once you add that `&` it should work. – AnT stands with Russia Oct 03 '13 at 21:19
  • Sorry, I am dumb. I updated the question code and just assumed I updated the actual code and did not... haha. Thanks works great now. – Nick Oct 03 '13 at 21:27