Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top