Pages: [1]   Go Down
Author Topic: Compound digital IO pins to a byte?  (Read 388 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Faraday Member
**
Karma: 66
Posts: 2635
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Search:
port manipulation arduino
Logged

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void ReadVirtualPortByte() {
  byte VirtualPortByte = 0;
  for(i = 2; i < 9; i++)
  {
    bitWrite(VirtualPortByte,i-2,digitalRead(i))
  }
}
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1419
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tend to go for the simplest option.
Assuming MSB is on pin 2:
Code:
mybyte = 0;
for(i =2;i>=10;i++){
mybyte = (mybyte*2) + digitalRead(i);
}
Reverse the for statement if the LSB is on pin 2.
Logged

Pages: [1]   Go Up
Jump to: