Using multiple pins from different ports as 1 set

Hi,
I am trying to use 8 pins to do an increment as 8 bits outputs. I have used the following so far:

DDRD = 255; // set PORTD (Pins 0-7) to outputs
     PORTD = 0;  // Set all pins LOW
        delay(timer);
  }
   void loop()
  {
     // Increment Port D (Digital Pins 0-7)
    for (PORTD = 0; PORTD < 255; PORTD++){

This works for the 0 to 7 (8 bits) but I loose the TX and RX pins.

What I want is to use digital pin 2 to 7 (6bits) and add digital pin 8 and 9 (2 bits) to get all 8 bits. The increment should use all 8 bits as a 1 set.

Thanks for any help

No can do.

The closest you can do is to keep a separate 8-bit counter value, then use bit shifting and masking to split the bits out.

For example, if your counter was called "counter", to get the lower 8 bits into the upper 8-bits of PORTD, you might do something like:

PORTD &= 0b00000011; // Clear upper 6 bits
PORTD |= (counter << 2) & 0b11111100;

It's important to do it like that, and not like:

PORTD = (counter << 2) & 0b11111100;

as you might be tempted, as that will set bits 0 and 1 to 0 every time. The first way, although longer, preserves the rest of the port.
I can't remember off hand which port / bits the next two pins are, but for the sake of arguement, and to illustrate, let's assume they're port A pins 3 and 4. You may do something like:

PORTA &= 0b11100111; // Clear bits 3 and 4
PORTA |= (counter >> 3) & 0b00011000;

Again, that method preserves the contents of the rest of the port.

Or a 6-bit and a 2-bit counter.
When the 6-bit rolls over from 3F to 0, increment the 2-bit.
D7-2 are PORTD bits 7-2, D9-8 are PORTB bits 1-0. No problem having them come from PORTA tho.

Or, move up to a bigger chip with four 8 bit ports - ATmega1284P.
I just ordered parts for another 25 kits last night. Need to review the purchase prices and adjust the kit price if needed.
http://www.crossroadsfencing.com/BobuinoRev17/

Thanks majenko and Crossroads for your replies.
I have the Nano and pro mini arduino. I can't get another one variant for now so will have to try with these 2.
Both of the suggestions are not suitable (unless I dont really understand what you trying to explain...I a beginner :blush:).
Since I am counting from 0 to 255 decimal and all 8 pins need to be used in parallel (output at the same time) to drive an 8bit DAC. I will keep trying.

The only way you'll get true simultaneous output is to either use one single port a s a whole, or use some kind of external latching buffer to only allow the signals to enter the DAC once they have all been set up properly.

If you want to avoid race conditions when outputing multi-bit value that's a
counter you might be able to switch to Gray coding if the receiving end can
accept it - with a Gray code you only change one bit for each increment.

Alternatively provide an external parallel latch and use one more pin to
control it. For instance 74HC374 or 74HC574 octal DQ latches.

I would go simple, with one of the following. Finding the right IC is up to you:

  1. 8-bit synchronous binary counter
  2. 8-bit serial in, gated parallel out shift register

Yes, use SPI.transfer() to load your data into a shift register to drive the DAC.
D13 is SCK, connect to SRCLK
D11 is MOSI, connect to SER-IN
D10 is ssPin, connect to RCLK (data is moved output register)
Connect OE/ to Gnd, Connect SRCLR to +5:

Simplest:

digitalWrite(ssPin, LOW);  // D10 is PORTB-bit2, 
SPI.transfer(yourData); 
digitalWrite(ssPin, HIGH);

or
digitalWrite(ssPin, LOW); // faster method: PORTB = PORTB & 0b11111011;

SPI.transfer(yourArrayData[x]);

digitalWrite(ssPin, HIGH); faster method: PORTB = PORTB | 0b00000100;

If you're going to transfer the data to the parallel DAC using serial, why bother with a parallel DAC? Why not just use a serial DAC and reduce the component count?

... maybe serial is too slow, which is why the OP has a parallel DAC...?

Wouldn't simplest be something like a 54ALS590? (https://www.engineering.uiowa.edu/sites/default/files/ees/files/NI/pdfs/00/91/DS009167.pdf)

Three pins required. Reset, Count, and Register Clock

I don't think you can buy those anymore.

54 logic series... now there's a blast from the past :wink: (for those not in the know, the 54 series was the military rated versions of the 74 series. Obsolete now).

CrossRoads:
I don't think you can buy those anymore.

But surely there's an equivalent. I did say "Something like..."

Or, he could use shiftOut() to a shift register ( 74HC595 ? ). Three pins, I think; clock, data, and latch.
Simple.
SPI is overkill, IMO.

lar3ry:
SPI is overkill, IMO.

How can you say that when you don't have a clue what the end application is?

shiftOut() is dog slow. SPI is faster, parallel IO (which the OP wants to use) is faster still.

The maximum clock speed for SPI on a 328 is 8MHz. At 8 bits per value, that's an absolute maximum output speed of 1Msps, not including the time taken to set latches, change values, etc.

Using parallel access of a whole port you can get that up considerably higher. Maybe around 4Msps...?

So don't pass judgement on what is overkill and what isn't until you actually know what the requirements are.

hiftOut() is dog slow. SPI is faster, parallel IO (which the OP wants to use) is faster still.

Point taken, but since he wants a simple increment, surely my other suggestion of using a counter would be about the fastest method, or am I wrong in that?

lar3ry:

hiftOut() is dog slow. SPI is faster, parallel IO (which the OP wants to use) is faster still.

Point taken, but since he wants a simple increment, surely my other suggestion of using a counter would be about the fastest method, or am I wrong in that?

Yep, you're quite right about that. You can make it even faster and cheaper by replacing the whole Arduino with a small clock generator. You could increment at GHz frequencies then if you wanted...