1

I am learning about microcontrollers and I am having difficulty understanding how one would use a union to set individual bits on a gpio port.

typedef union _BYTE
{
    byte _byte;
    struct
    {
        unsigned b0:1;
        unsigned b1:1;
        unsigned b2:1;
        unsigned b3:1;
        unsigned b4:1;
        unsigned b5:1;
        unsigned b6:1;
        unsigned b7:1;
    }Bit;
} BYTE;

I am using the above to gain access to the individual bits of a byte, but how do I use this to assign a io port values in the following manner?

MCF_GPIO_PORTDD.Bit.b0 = 1;

I would rather not allocate a type of _BYTE and then assign the port to that.

#define MCF_GPIO_PORTDD             (*(vuint8 *)(&__IPSBAR[0x100009]))

MCF_GPIO_PORTDD is just a memory address.

user623879
  • 4,066
  • 9
  • 38
  • 53

1 Answers1

3

It's casting the port pointer to a volatile unsigned char (8 bit int). You can make a similar macro for your type:

#define PORTDD  (*(volatile BYTE *)(&__IPSBAR[0x100009]))

and then access it with PORTDD.Bit.b0. Note that in some instances you will need to write ._byte directly if you need to ensure multiple bits are set (or cleared) in a single register write. If you don't name Bit (just make it anonymous) your C compiler may let you shorten that to PORTDD.b0.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150