Get PORTx from arduino-style pin number

Hello,

I would like to obtain the real avr-style pin name (i.e. PA6) from the arduino-style pin number (i.e. 3)
Is there any way to do it?
Obviusly i mean to do it by coding, i know i could just google it

In pins_arduino.h are defined digital_pin_to_port_PGM[] (using the Arduino AVR Boards standard variant as an example):

and PROGMEM digital_pin_to_bit_mask_PGM[]:

Not in code, because all references are discarded once they are used in the compiling process.

pert:
In pins_arduino.h are defined digital_pin_to_port_PGM[] (using the Arduino AVR Boards standard variant as an example):
Arduino/hardware/arduino/avr/variants/standard/pins_arduino.h at 1.8.3 · arduino/Arduino · GitHub

and PROGMEM digital_pin_to_bit_mask_PGM[]:
Arduino/hardware/arduino/avr/variants/standard/pins_arduino.h at 1.8.3 · arduino/Arduino · GitHub

I m not at the computer right now
So something like
digital_pin_to_port_PGM[0] "plus" digital_pin_to_bit_mask_PGM[0]
Will give me PD0?

Have a look at how digitalWrite() works:
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_digital.c#L138-L163
It takes an Arduino style pin number and uses that to determine the port and bit to write to.

aster94:
I m not at the computer right now
So something like
digital_pin_to_port_PGM[0] "plus" digital_pin_to_bit_mask_PGM[0]
Will give me PD0?

It will give you machine instructions that access that bit of that port. Which the core does for you automatically anyway...

aster94:
I m not at the computer right now
So something like
digital_pin_to_port_PGM[0] "plus" digital_pin_to_bit_mask_PGM[0]
Will give me PD0?

You wouldn't exactly add them if that's what you're after. digital_pin_to_port will give you the port that the pin is on. digital_pin_to_bitmask will give you a bitmask to isolate the right pin from that port. From the bitmask and a little bit of bitmath you could work out the name in the style you want.

Why do you want that style of name? It's not very useful for anything.

pert:
Have a look at how digitalWrite() works:
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_digital.c#L138-L163
It takes an Arduino style pin number and uses that to determine the port and bit to write to.

thanks it helped me a lot

Delta_G:
You wouldn't exactly add them if that's what you're after. digital_pin_to_port will give you the port that the pin is on. digital_pin_to_bitmask will give you a bitmask to isolate the right pin from that port. From the bitmask and a little bit of bitmath you could work out the name in the style you want.

Why do you want that style of name? It's not very useful for anything.

i am using a bluetooth library but there is some kind of problem in the app interface (from the mobile phone if i want to use pin 4 it doesn't match with the real pin number in arduino), I could fix it in a simpler way but i want to understand what there is wrong exactly

by the way the bit math was easy :smiley:

byte counter, port, bit;

void setup() {
  Serial.begin(115200);
  
  for (byte pin = 0; pin < 20; pin++) {

    bit = digitalPinToBitMask(pin);
    port = digitalPinToPort(pin);

    Serial.print("pin\t"); Serial.println(pin);
    Serial.print("port:\tP"); Serial.write(port + 0x40);
    
    while (bit != 1) {
      bit >>= 1;
      counter++;
    }
    Serial.println(counter);
    Serial.println("");
    counter = 0;
  }
}

void loop() {}

Are you just looking for these "standardish" macros (from "Arduino.h")?

// Get the bit location within the hardware port of the given virtual pin.
// This comes from the pins_*.c file for the active board configuration.
// 
// These perform slightly better as macros compared to inline functions
//
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
#define analogInPinToBit(P) (P)
#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )

Various "fast" IO classes will accept a pinNumber and cache the port/bitmask, thereby bypassing some of the overhead of digitalWrite/etc.