Search code examples
cportsmsp430

MSP430 ports access


I'm working on a project using msp430. How can i read the value received by a specific port and assign it to a variable? Like P1.5 ? I already set P1DIR to input. I did:

data = (P1IN & SDA_TMP); // Where SDA_TMP is a defined prep

Solution

  • Something like this:

    const bool data = (P1IN & (1 << 5)) != 0;
    

    This uses bitwise and (&) to mask out the fifth bit (whose value is 1 << 5), then does a comparison against zero. The result in data will be true if bit 5 is set, false if it isn't.

    See also this tutorial for more.