Port Manipulation - bitRead a Port

Hello everyone,

I'm trying to load each value from the IO pins into a byte. For example how might I load the whole of PORTB into a byte? I tried to use bitRead but doesn't seem to work. I assume you would have to read each pin and store it into the byte sequentially but I'm not sure how to do this so any help would be appreciated

Thanks

Shaun

PORTB is a byte.

I understand but how would I read this byte and store it as a variable?

byte x = bitRead(PORTB) didn't work, unless it's just byte x = PORTB?

1. If Port-B is an output port, then --
byte y = PORTB; //reading port's internal latch and not the output pins' values

2. If Port-B is an input port, then ----

byte y;
for(int i = 8, j = 0; i < 14; i++, j++)  //PINB0 = DPin-8, ....., PINB5 = DPin-13
{
    bitWrite(y, j, digitalRead(i)); //(PINB0) ---> y0, ..., (PINB5) ---> y5 
}

This is what I was looking for, I'll try it tomorrow. Thank you very much. I've been learning assembly for the 6502 and you can just do LDA PORTB, it's a shame it's not that simple with the Arduino because that's what it must do at a low level.

I was doing the same assembly for 6802 in 1988 as you did for 6502.

       byte x = PINB;   // read the port

a7

Oh cool! I have a lot of respect for people who wrote games and even simple programmes back in the day because it cannot have been easy.

Remember! All 6502 IO is memory mapped. The Arduino processors are NOT!

Yes! The above is the single line version of the codes of post #5 and now it is very much equivalent to LDA PORTB of @ShaunJV12 .

Ah I'll have to read into this, thank you.

I'm not familiar with memory mapping but I'll read into it, thank you.

Excellent!

Just google

Arduino direct port manipulation

It's almost simple enough to just remember… :expressionless:

a7

Yeah I have written sketches about a year ago with port manipulation but only wrote to ports, I haven't read from them before. Thank you for your help

1. Fig-1, 2 depict the Port structure of the ATmega328P MCU of UNO Board.
50lxy
Figure-1:
pd2
Figure-2:

2. IO (input and output) lines are bi-directional.
3. When the directions of the IO lines are not yet determined, then the port names are usually are:
Port-B, Port-C, and Port-D.

The individual IO lines are designated as:
pb0, ..., pb5; pc0, ..., pc5; pd0, ..., pd7

4. When the directions of the IO lines are determined as outputs, then the port names are usually are:
PORTB, PORTC, and PORTD.

The individual output lines are designated as:

PB0, PB1, PB2, PB3, PB4, PB5 or 8, 9, 10, 11, 12, 13

PC0,  PC1, PC2, PC3, PC4, PC5 or A0, A1, A2, A3, A4, A5 or 14, 15, 16, 17, 18, 19

PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7 or 0, 1, 2, 3 ,4 ,5 ,6 ,7

Example:

pinMode(8, OUPUT); //PBO will work as output line

bitSet(PORTB, PB0);   //HIGH is written on Bit-0 position of PORTB
digitalWrite(8, HIGH); //HIGH is written on Bit-0 position of PORTB

5. When the directions of the IO lines are determined as inputs, then the port names are usually are:
PINB, PINC, and PIND.

The individual input lines are designated as:

PINB0, ..., PINB5 or 8, ..., 13

PINC0, ..., PINC5 or A0, ..., A5 or 14, ..., 19

PIND0, ..., PIND7 or 0, ..., 7

The input lines can be optionally connected with internal pull-up resistor (Rpin = 20k - 50k, Fig-2). It is required to execute the following code:

pinMode(2, INPUT_PULLUP); 

Example:

pinMode(7, INPUT); // Bit-7 of Port-D will work as input line without internal pull-up 

int n = digitalRead(PIND7); //logic value of Bit-7 of PIND is read into variable n
int n = bitRead(PIND, PIND7); //logic value of Bit-7 of PIND is read into variable n
int n = bitRead(PIND, 7);  //logic value of Bit-7 of PIND is read into variable n
int n = digitalRead(7);  //logic value of Bit-7 of PIND is read into variable n

@DrDiettrich @GolamMostafa @alto777 @Paul_KD7HB Just wanted to say thank you everyone for your helpful replies. This is my first time posting here and I'm really impressed at the speed and usefulness of your replies, usually forums are filled with sarcasm but Arduino has a cool community

@GolamMostafa might have bothereed to include the data direction register that each port has.

Three registers and one convention is a minimum level of knowledge, viz:

PORTx - the output port. Write to it.

PINx - Read it for the inputs

DDRx - Set the roll of each pin by bit - 1 is for an output, 0 for input.

Lastly, write 1s to the bits that you've designated as inputs to turn on the internal pullups.

a7

To do io with memory mapped io, you just write or read from certain memory locations, no special commands.