0

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);
Hussein
  • 41
  • 5
  • Please show the call of this function to make your question useful for further readers. – Martin Zabel Mar 12 '16 at 13:42
  • call of function added – Hussein Mar 12 '16 at 14:01
  • 1
    PORTA is a pointer (an address), need to use it as an address pointing at the port for it to work. Or within the function re-declare it as a pointer (volatile unsigned char *) kind of a thing. – old_timer Mar 12 '16 at 15:08
  • Sorry, your question is still unclear. What do you mean by manipulating the IO register parameter? What is the expected behaviour? And what is not working? – Martin Zabel Mar 12 '16 at 16:11
  • 1
    Those calling this question "unclear" are merely showing that they are not familiar with the subject matter. To the OP, look through your embedded toolchain configurations include files to find where and how PORTA is defined. – Chris Stratton Mar 13 '16 at 19:10

1 Answers1

5

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);
Community
  • 1
  • 1
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • thanks Very useful but recommended to add volatile to port declaration – Hussein Mar 12 '16 at 14:27
  • 3
    Right initial idea, but an unworkably wrong implementation - a port is not an arbitrary variable, it is a very specific memory location, and so you have to declare it as a pointer to an appropriate volatile data type given that address. – Chris Stratton Mar 13 '16 at 19:12