Custom Bitfield & Port Mask

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would like a more elegant way to address a 16bit peripheral when using two ports. I have no idea how I would go about it but is it possible to do this:

MY_PORT = 0x99FF;

that would output data as:

PORTA = 0x99;
PORTD = 0xFF;

Or even better if I could have bit control and arbitrarily output all 16bits to any port/pin combo.

I know ARM's have a port masking register and its easier when their ports are 32 long but is their any easy way to do this on an AVR?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define MY_PORT(val) {PORTA=(val)>>8; PORTD=(val);}
...
    MY_PORT(0x99FF);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yea... That not quite what I was after. I am currently doing this in my routines but would like a better method that allows me to use pins from wherever I want to. I figured it would need to be a structure of sorts.

I have no idea if the example below works but I figure it will be something like it

struct bits {
  uint8_t b0:1;
  uint8_t b1:1;
  uint8_t b2:1;
  uint8_t b3:1;
  uint8_t b4:1;
  uint8_t b5:1;
  uint8_t b6:1;
  uint8_t b7:1;
} __attribute__((__packed__));

#define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin)

struct MY_PORT {
  SBIT(PORTA, 0);
  SBIT(PORTD, 4);
        .
        .
        .
  SBIT(PORTB, 3);
  SBIT(PORTB, 6);
}

MY_PORT = 0x99FF;