style question: use of “ports”

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


I'd say port C

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.

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.

Yep, very fast, very simple.

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

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.

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?

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()).

Perhaps the insert bits built-in function would work for you.

How about

const int thePin[] = {A0, A1, A2, A3, A4, A5, A6, A7};
uint8_t theByte = 0;
uint8_t i = 8; 
while (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 !!”

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