Go Down

Topic: Compound digital IO pins to a byte? (Read 481 times) previous topic - next topic

Timmotec

I'm currently making a project using an arduino nano to make a cable tester with 8 lines to test, due to me wanting to use D0 and D1 for TX and RX I have used D2-D9 for the test lines. Would it be possible to compound these I/O pins so I can read a byte of data rather than addressing them individually?

If so, at each read of the port one of the I/O lines will be set as a high output (while the rest will all be inputs), would this just be read as a high or would it stop the read from working?

Thanks,
Paul.

LarryD

The way you have it in your schematic isn't the same as how you have it wired up!

CrossRoads

You can read a port at a time - D2 thruy D9 are across 2 ports.
portDdata = PIND; // read D7-0
portBdata = PINB, // read D13-D8 (upper 2 bits are not connected to IO pins)

Manipulate away, masking as needed for the bits you are not using it for decision making.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Timmotec

Thanks for your replies!

LarryD - that was the phrase I couldn't think of thanks for helping me search!
CrossRoads - You've confirmed both what I thought would be the case and what I was reading.... :smiley-roll:

So really as I'll be doing this multiple times I should have a little function for it, something like this?
Code: [Select]
int VirtualPortByte = 0;
for(i = 3; i < 9; i++)
{
  VirtualPortByte = VirtualPortByte << 1;
  if (digitalRead(i) == HIGH)
  {
    VirtualPortByte | 1;
  }
}


As above will the fact that one of the I/O pins being set as an output cause the read to fail or be set to a default output?

Timmotec

Starting to find out more!

From here:

Quote from: Mitch_CA
I believe so yes.  From the atmega datasheet section 2.4...

"Independent of the setting of Data Direction bit DDxn, the port pin can be read through the
PINxn Register bit."

The Arduino digitalRead() function most certainly has to read the PINxn register to decide whether the pin is HIGH or LOW.

CrossRoads

Sort of - for Port D, you don't want to be setting bits 0,1 & messing up your serial comm's.
I don't know what you're doing with VirtualPortByte.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Timmotec

Sorry I took so long to get back onto this, busy at work!

I'm using VirtualPortByte as the storage for the byte of information I want to read (1 byte, containing 6 bits for D2-7 on port D and 2 bits for D8-9 on port B).

I will be trying the following code later!

Code: [Select]
void ReadVirtualPortByte() {
  byte VirtualPortByte = 0;
  for(i = 2; i < 9; i++)
  {
    bitWrite(VirtualPortByte,i-2,digitalRead(i))
  }
}


Henry_Best

I tend to go for the simplest option.
Assuming MSB is on pin 2:
Code: [Select]

mybyte = 0;
for(i =2;i>=10;i++){
mybyte = (mybyte*2) + digitalRead(i);
}

Reverse the for statement if the LSB is on pin 2.

Go Up