3

I am now doing a program for a MSP430 in C and I am using Port 1.6, 1.7, 2.0, 2.1, and 2.2 to drive some LEDs. Now, in order to turn ON all LEDs I simply have to write:

P1OUT |= 0xC0; 
P2OUT |= 0x07;

However, I would like to know if is possible to create an C structure (with the name LED_ACTIVATION) that points to bits 0, 1, and 2 of the P2OUT Register (0x0029) and to bits 6 and 7 from P1OUT Register (0x0021), that will allow me to write something like this:

LED_ACTIVATION = 0x2F;

where port 2.2 is the most significant bit (bit 5) and port 1.6 is the lowest significant bit(bit 0).

In the MSP we can do this assignment for each register like this:

__no_init volatile union
{
  unsigned char P1OUT;   /* Port 1 Output */

  struct
  {
    unsigned char P0              : 1; /*  */
    unsigned char P1              : 1; /*  */
    unsigned char P2              : 1; /*  */
    unsigned char P3              : 1; /*  */
    unsigned char P4              : 1; /*  */
    unsigned char P5              : 1; /*  */
    unsigned char P6              : 1; /*  */
    unsigned char P7              : 1; /*  */
  }P1OUT_bit;
} @0x0021;

or like this:

#define P1OUT_              (0x0021u)  /* Port 1 Output */
DEFC(   P1OUT             , P1OUT_)

But is it possible to mix both register address?

Thanks.

jegonz
  • 65
  • 5
  • 2
    You can't use the C feature "bit fields" for this purpose, without wading through page after page of compiler documentation. [See this](http://stackoverflow.com/questions/6043483/why-bit-endianness-is-an-issue-in-bitfields/6044223#6044223). Actually, you can't use bit fields in any useful way at all. Tough luck, they are worthless in C. Use bit masks instead. – Lundin May 16 '13 at 14:17
  • 2
    pointing structs at memory (or anything outside the compile domain) is bad, using bitfields for any reason is very bad, avoid them at all costs. For the MSP430 in particular there is nothing better than the code you started with in your question. It is as direct and simple as you are going to get. – old_timer May 16 '13 at 21:29
  • @Lundin and dwelch, thanks for the reply and the link! I guess I will have to stick with the bit masks. – jegonz May 18 '13 at 07:38

1 Answers1

3

The MSP430 is not bit/pin addressable. So, no, you can't create a structure that contains individual pins from multiple ports.

In order to make:

LED_ACTIVATION = 0x2F;

You would need to use a function or a macro.

See also: Accessing individual I/O pin on MSP430

Community
  • 1
  • 1
embedded.kyle
  • 10,976
  • 5
  • 37
  • 56
  • 1
    @jegonz, to keep the assignment operator syntax, I have used C++ operator overloading instead of macros or functions. I much prefer the readability of "LED_ACTIVATION = 0x2F" over "LED_ACTIVATION(0x2F)", but it obviously requires a C++ compiler which you may not have. – benpro May 16 '13 at 14:00
  • @embedded.kyle, thanks for the reply. I am now using functions and macros, but I was just wondering if it was possible with structures. Thanks! – jegonz May 18 '13 at 07:40
  • @benpro, thanks for the reply. I have a C++ compiler as well, but all the project is written in C, so better to stick to C. Thanks anyways, will take this in consideration when working with C++. – jegonz May 18 '13 at 07:42