Pointer of PORTS

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 
1,665件の閲覧回数
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 解決策
931件の閲覧回数
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 返信
932件の閲覧回数
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 件の賞賛
返信