Hussein
Hussein

Reputation: 41

Can I pass an IO port as a parameter to a function?

I try to make function that take IO register parameter (AVR 32 PORTA) to manipulate it .but it doesn't work .

/*my function */
U8_t set_bits( U8_t port, U8_t mask)
{
port |= mask;
return port ;
}


/*call of function*/
PORTA=set_bits(PORTA , 0xF0);

Upvotes: 0

Views: 1393

Answers (1)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

Welll...it works, but you need to return the port. Currently it is a parameter (passed by value). Use:

void set_bits(volatile uint8_t *port, uint8_t mask)
{
    *port |= mask;
}

and call with:

    uint8_t port;
    set_bits( &port, mask);

Or you can return the port:

uint8_t set_bits(uint8_t port, uint8_t mask)
{
    port |= mask;
    return port;
}

and call with:

    uint8_t port;
    port= set_bits(port, mask);

Upvotes: 5

Related Questions