I'm currently defining some macros for pins for a standalone Atmega328 project board. I have made a mistake of incorrectly corelating pins labelled as PB3 with a digital pin in Arduino IDE a few times. This got me thinking, is it possible to just use PB2, PC2 etc to directly address those pins? Or perhaps is there a definitions file already available out there, that I can include in the sketch that would allow me to use port and pin names directly as I see them on the symbol of the atmega328 in kiCad?
If you did so, the code would not be portable between different Arduinos, and error prone when editing in the case that the project wiring changes.
Try this and see if it does what you want:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(PB3);
digitalWrite(PB3, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
}
No, not in general, because the predefined PB6 is the same value as the predefined PD6 (and etc.)
There is a convention followed in some "cores" to define PIN_Pxx values that map the pin names to their Arduino pin numbers (perhaps "many" cores? All of Spence Konde's and the Hans "MCUDude" cores seem to do it.)
For example, from MegaCore's "100pin Arduino Mega" variant: