# style question: use of “ports”

**URL:** <https://forum.arduino.cc/t/style-question-use-of-ports/697587>\
**Category:** Programming\
**Created:** [February 24, 2021, 9:45pm UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587 "2021-02-24T21:45:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shloime](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/shloime/32/1126491_2.png) [@shloime](https://forum.arduino.cc/u/shloime)\
**Post date:** [February 24, 2021, 9:45pm UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/1 "2021-02-24T21:45:13Z")

</div>

working with an arduino nano every, i want to use pins a0-a7 as inputs (just because the breakout board groups them neatly in a bunch), to input 2 “nybbles”. the idea is to use 4 pins each to set 2 timers, in the range from 0 to 15. trivial stuff, and i’ve got it all working. (bit fiddling is pretty simple.)

however, the question is, can it be done more elegantly than digital reads, pin by pin? if i could read an entire 8-bit port at once, it would be cleaner and easier to understand.

there is a bewildering array of different ways of referencing pins, but i can’t find a “port”-like definition for pins a0-a7. (those pins actually map into 2 different ports on the 4809 processor, 6 from one and 2 from another.)

suggestions?

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [February 24, 2021, 9:57pm UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/2 "2021-02-24T21:57:18Z")

</div>

[I'd say port C](https://www.arduino.cc/en/Reference/PortManipulation)

> i want to use pins a0-a7 as inputs (just because the breakout board groups them neatly in a bunch), to input

I wouldn't want try a6 & a7 as outputs.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [February 24, 2021, 10:40pm UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/3 "2021-02-24T22:40:59Z")

</div>

If you can find 8 pins that all come from the same physical port then just read the port register directly.

I have not studied the datasheet enough to know if this is possible.

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [February 24, 2021, 11:00pm UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/4 "2021-02-24T23:00:14Z")

</div>

Yep, [very fast, very simple](https://microchipdeveloper.com/8avr:ioports).

```cpp
DDRC = 0x00;     //make PORTC all inputs 
PORTC = 0xFF;    //enable all pull-ups 
data = PINC;     //read PORTC pins into variable data

```

OK, specifically, that was for the Nano (A6 and A7) won't work.

Now for the Nano Every (ATmega4809) ... [Arduino Nano Every port manipulation](https://forum.arduino.cc/index.php?topic=642614.0)

For everything that can be done with registers, its good to look through the **iom4809.h** file, but unfortunately you'll need to install MPLABX IDE (its huge) to get it on your PC.

---

<div class="post-metadata">

**Author:** ![shloime](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/shloime/32/1126491_2.png) [@shloime](https://forum.arduino.cc/u/shloime)\
**Post date:** [February 25, 2021, 2:59am UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/5 "2021-02-25T02:59:33Z")

</div>

i’m quite a novice at this, but i’d say that only port d has 8 pins available at the chip, but they’re also not arranged nicely on the board (on the same side but not in order), and pd6 is missing in action.

so i’m left with addressing them individually, unless someone has a better idea?

for now i’m using:

const int bit1 = A0;  
...  
const int bit8 = A7;

onTime |= digitalRead(bit3) \<\<1; // for each of the bits  
...

fwiw, not quite as bad as a cascade of IF statements.

it works, it just isn’t very elegant. but reading a port register (2 actually) and then juggling the bits around (to match the inputs, which run pd3-pd0, good, but then pa2, pa3, pd4, pd5) isn’t any neater.

time to give up and move on?

---

<div class="post-metadata">

**Author:** ![marco\_c](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/marco_c/32/215057_2.png) [@marco\_c](https://forum.arduino.cc/u/marco_c)\
**Post date:** [February 26, 2021, 1:16am UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/6 "2021-02-26T01:16:42Z")

</div>

Assuming you can do this, my question would be - do you want this code to be portable to other types of processors? If not, then access the hardware ports using assembler code (which is what it is), if you do, then the easiest option is to use the higher level abstractions that the library makes available (ie, digitalRead()).

---

<div class="post-metadata">

**Author:** ![dougp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dougp/32/238908_2.png) [@dougp](https://forum.arduino.cc/u/dougp)\
**Post date:** [February 26, 2021, 1:35am UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/7 "2021-02-26T01:35:02Z")

</div>

Perhaps the [insert bits](https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gcc/AVR-Built-in-Functions.html) built-in function would work for you.

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [February 26, 2021, 7:37am UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/8 "2021-02-26T07:37:41Z")

</div>

How about

```arduino
const int thePin[] = {A0, A1, A2, A3, A4, A5, A6, A7};
uint8_t theByte = 0;
uint8_t i = 8; 
while (i) {
  i--;
  theByte = theByte << 1;
  if (digitalRead(thePin[i])) theByte++; // this sets the bit if the pin is 'HIGH'
}

```

The momoent you start doing things like

> const int bit1 = A0;  
> ...  
> const int bit8 = A7;

You should think "array, array, array !!"

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 26, 2021, 7:38am UTC](https://forum.arduino.cc/t/style-question-use-of-ports/697587/9 "2021-06-26T07:38:25Z")

</div>

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
