Pointer of PORTS

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Pointer of PORTS

Jump to solution
1,125 Views
electronicfan
Contributor II
Hello,

I'm looking for a solution for an array like this:

unsigned int LED[2]={PTAD_PTAD0, PTCD_PTCD3}

I hope somebody can help me.

Thanks!


Labels (1)
Tags (1)
0 Kudos
1 Solution
391 Views
Lundin
Senior Contributor IV
First of all, I would strongly advise against using the bitfield macros PTAD_PTAD0 etc since there is plenty of undefined behavior asociated with bitfields, making your program behave randomly in case you ever port it to another compiler/CPU. Also, the Codewarrior headers with those macros don't follow ISO C, so it is best to avoid them entirely.

I think what you are looking for is something like this:

typedef enum
{
  LED0 = 0x01,  /* bit masks for the port */
  LED1 = 0x02,
  LED2 = 0x04,
  ...
} LedType;

static const unsigned char LED_MASK[N] =
{
  LED0,
  LED1,
  LED2,
  ...
};

static volatile unsigned char* leds[N] =
{
  &PTAD,
  &PTCD,
  ...
};


void set_led (unsigned int n, BOOL active)
{
  if(active)
  {
    *leds[n] |= LED_MASK[n];
  }
  else
  {
    *leds[n] &= (unsigned char) ~LED_MASK[n];
  }
}

View solution in original post

0 Kudos
1 Reply
392 Views
Lundin
Senior Contributor IV
First of all, I would strongly advise against using the bitfield macros PTAD_PTAD0 etc since there is plenty of undefined behavior asociated with bitfields, making your program behave randomly in case you ever port it to another compiler/CPU. Also, the Codewarrior headers with those macros don't follow ISO C, so it is best to avoid them entirely.

I think what you are looking for is something like this:

typedef enum
{
  LED0 = 0x01,  /* bit masks for the port */
  LED1 = 0x02,
  LED2 = 0x04,
  ...
} LedType;

static const unsigned char LED_MASK[N] =
{
  LED0,
  LED1,
  LED2,
  ...
};

static volatile unsigned char* leds[N] =
{
  &PTAD,
  &PTCD,
  ...
};


void set_led (unsigned int n, BOOL active)
{
  if(active)
  {
    *leds[n] |= LED_MASK[n];
  }
  else
  {
    *leds[n] &= (unsigned char) ~LED_MASK[n];
  }
}
0 Kudos