Daniel Grillo
Daniel Grillo

Reputation: 2418

Trying to understand bit ram address

I'm trying to understand this piece of code to address bits:

/*  GPIO bits   */
static bit  GP5 @ (unsigned)&GPIO*8+5;
static bit  GP4 @ (unsigned)&GPIO*8+4;
static bit  GP3 @ (unsigned)&GPIO*8+3;
static bit  GP2 @ (unsigned)&GPIO*8+2;
static bit  GP1 @ (unsigned)&GPIO*8+1;
static bit  GP0 @ (unsigned)&GPIO*8+0;

The GPIO is defined in this way:

static volatile unsigned char   GPIO    @ 0x06;

Why GPIO address is multiplied by 8 and then added by the number of the bit? What the result of this macro and how can I address the bit?

The code above is for XC8 compiler for PIC Microcontrollers. Atmel uses the same when they use the macro IOPORT_CREATE_PIN. This macro is defined as below:

#define IOPORT_CREATE_PIN(port, pin)    ((IOPORT_##port)*8 + (pin))

Upvotes: 0

Views: 160

Answers (2)

UncleO
UncleO

Reputation: 8449

"Why GPIO address is multiplied by 8 and then added by the number of the bit? What the result of this macro and how can I address the bit?"

It's the count of bits from the lowest address: 8 bits per byte plus offset into the byte.

You can address the bit by that name, e.g., GP3 = 1;. The compiler knows it is a single bit. As pointed out, this is a particular compiler extension for the PIC.

Upvotes: 2

user2778477
user2778477

Reputation:

Because each special function register has 8 bits (depends on the microcontroller). The address of GP0 is 0x06*8+0 = 0x30. Ways to address a bit also depends on the microcontroller. Sorry, I don't familiar with PIC. You may figure it out on your own.

enter image description here

Upvotes: 1

Related Questions