How does the Arduino library know what the pins map to? - Page 1
Forum section: electronics / general

author: ksoviero
author_rank: Contributor
timestamp: July 23, 2020, 12:32:59 am
content: Code:pinMode(7, INPUT);If you were to run the above, it would set pin PD7 to be an input pin. My question is, how does it know that "7" means PD7 and that "8" means PB0?Going further, I want to start using micro controllers in larger projects where they're attached directly to the board (or a socket), and in such cases, pin IDs of "7" or "8" would become completely meaningless. I'd like to try changing the pin numbers to something more meaningful, maybe the pin number of the IC, like PD7 would be mapped to "14". How would you do that?Thanks!

author: greenpossum
author_rank: Frequent Contributor
timestamp: July 23, 2020, 12:43:27 am
content: That's why you have to set the board model when using the IDE.

author: ataradov
author_rank: Super Contributor
timestamp: July 23, 2020, 01:03:08 am
content: The real answer is to get rid of Arduino if you need more flexibility.But for now you can look in the guts of pinMode() and see what it does.

author: sleemanj
author_rank: Super Contributor
timestamp: July 23, 2020, 01:37:41 am
content: The pin to port, bitmask, pc interrupt and more is defined in the Variants file for the board selected in the IDE.The standard (ie, ATMega328) variant file is thishttps://github.com/arduino/ArduinoCore-avr/blob/master/variants/standard/pins_arduino.hyou can see on line 150 where the pin is mapped to a port.

author: ksoviero
author_rank: Contributor
timestamp: July 23, 2020, 03:15:49 am
content: Quote from: ataradov on July 23, 2020, 01:03:08 amAs much as I'd love to move away from Arduino, going from Serial.begin() tohttps://appelsiini.net/2011/simple-usart-with-avr-libc/is just not even close to worth it for me. I'd much rather extend the Arduino libraries to do more of what I need than move away from them completely.

author: brucehoult
author_rank: Super Contributor
timestamp: July 23, 2020, 03:28:28 am
content: Quote from: ksoviero on July 23, 2020, 03:15:49 amIt's not either-or.You can keep Serial.Begin() etc and use direct writes to port registers and timers etc for other things. Obviously you need to be careful not to stomp on the serial port -- and also on the timer used for millis() and PWM (if you use analogWrite(). But other than that, you can mix and match pretty freely.

author: westfw
author_rank: Super Contributor
timestamp: July 23, 2020, 05:35:46 am
content: QuoteI don't know wht people tend to forget that the Arduino stuff is all open-source, and distributed AS SOURCE.That means that every copy of the IDE that is installed includes the source for digitalWrite() and all the pin-mapping stuff.Note that the mapping for AVR and the mapping for ARM chips is significantly different - the AVR has several parallel arrays for "pin to port", "pin to bitmask", "pin to timer", and etc, while the ARM cores define a structure containing "all the info" and uses one big array of structures. (not a very well-designed structure, but who cares when you have lots of MHz and lots of memory?)If you're trying to "graduate" from Arduino to "real AVR code", reading the Arduino core code until you know which parts are bad, and why, is a pretty useful educational exercise!

author: woofy
author_rank: Frequent Contributor
timestamp: July 23, 2020, 09:39:36 am
content: pinMode is easy to use, but very slow. For a command you typically only use once to set the pin, it really dosn't matter, but same mapping happens with digitalWrite and that can ruin your day if timing can't be met. You don't have to abandon arduino though, you can still use the port directly. Such as:PORTB = addrL;To wait for a signal on a pin:while( (PORTD&0x04) == 0x04 );If the compiled code is still to slow then you can always drop down to assembler:asm (".global addrL\n""in r24, 0x0b\n""lsr r24\n""lsr r24\n""lsr r24\n""lsr r24\n""sts r24, =addr\n");All within the Arduino IDE.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: July 23, 2020, 07:59:37 pm
content: If you're writing software for the "arduino" framework, then you're likely to have all that stuff somewhere on your own computer.First thing to do is ditch the arduino java contraption some dare to call an "ide".Then, for example install platformio, and let it initialize an "arduino" project for you, and also init some kind of (at least) half decent IDE.I did mine with: "pio init --ide codeblocks.Codeblocks is not among the most "advanced" ide's, but it's relatively simple and it "works".In Codeblocks I can right click on "pinMode" then select "Find declaration of: "pinMode"", and it jumps for me to the file "wiring_digital.h" which is hidden somewhere deep in the "arduino" directory structure.Unfortunately it's only the declaration in the header file. The "Find definition" does not always work in Codeblocks. Sometimes you have to add the source files of such a framework manually to the project before Codeblocks can find it.With such a system you can easily find all kinds of hidden details in all the files of the "arduino" (or other) framework.Learn to use such tools effectively! It's one of the next steps of growing up.There is also (partial?) support for a bunch of other IDE's in Platformio, so you can choose another if you prefer.https://platformio.org/