Pointer of PORTS

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Pointer of PORTS

跳至解决方案
1,633 次查看
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!


标签 (1)
标记 (1)
0 项奖励
回复
1 解答
899 次查看
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 项奖励
回复
1 回复
900 次查看
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 项奖励
回复