0

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))
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
Daniel Grillo
  • 2,368
  • 4
  • 37
  • 62
  • look at the documentation for the part, it should be pretty obvious. Examine the size of the I/O registers for reading a port and/or pin within a port. That should explain the address calculation. I would not expect this to be consistent among vendors, esp from microchip to atmel, that is simply a coincidence in this one case. The compiler is interesting, but the more important detail we need is the part number. Your question is extremely specific to that part or family. – old_timer Sep 18 '13 at 13:55
  • This syntax is not C so much as it is XC8, you may want to adjust the tags accordingly. – Brian Cain Sep 18 '13 at 13:58
  • @BrianCain, this sintax can be explained here: http://stackoverflow.com/questions/15955856/sign-in-c-variable-declaration – Daniel Grillo Sep 18 '13 at 14:01
  • @DanielGrillo, I understand the syntax, but it's not legal C, so you may find that SO frowns on the use of the 'C' tag for stuff that's not *quite* C. – Brian Cain Sep 18 '13 at 14:03

2 Answers2

2

"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.

UncleO
  • 8,299
  • 21
  • 29
1

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