How to Use PORT B and PORT D Simultaneously to make 10-bit Digital Outputs

Hello, Could you help me to review my sketch or have another option to make it simple?

I'd like to make 10 bit digital output using pin 0 until pin 9 in Arduino Uno. This output will be used to represented decimal number from 0 to 1023. This my sketch

int analogPin=0;
int val = 0;
int Ts=2*6;
byte highbyte;
byte lowbyte;

void setup() {

DDRD = B11111111;
DDRB = B00000011;
}

void loop() {

for (int i=0;i<1023;i++)
{
delayMicroseconds(Ts);
PORTD = i;
// in this part I want to make PORT B and PORT D work simultaneously to count until 1023
}
delayMicroseconds(1*Ts);
}

but this sketch can not be used to dec number > 255. I hope you can help me, Guyz. Thank you

DDRD = B11111111;
The above makes RX an output.

Do realize D0 and D1 are used for serial communication?
Is there a reason why you don't use A0-5 as digital outputs?

What are you outputting to?

Mark

Thank you Mark and Larry, actually I want to make 10 bit digital output simultaneously for DAC purposes. This sketch I use to make 8 bit for the first try. If I can use pin in port B and port D in the same time, I will use pin 3 until 12.

What DAC?

Mark

Have you thought about using two output serial shift registers, 74HC595?

.

If I can use pin in port B and port D in the same time

That depends on your definition of "at the same time" The fact is that you cannot change the state of 2 ports at the same time. You can change one then the other very soon afterwards.

Sorry Mark, I thought DAC (digital to analog converter) is common word in this forum. Thank you Larry for your suggestion, I will keep in my mind. I am just curious whether the UNO could be used as DAC 10 bit without any additional peripheral devices.

Mark asked "What DAC ?" not "What is a DAC ?"

I would use PORTC and PORTD and leave D0 and D1 for serial.

DDRD = DDRD | B11111100; // this is safer as it sets pins 2 to 7 as outputs
// without changing the value of pins 0 & 1, which are RX & TX

Look into masking and shifting to manage breaking up a number of 0-1023

.

That depends on your definition of "at the same time" The fact is that you cannot change the state of 2 ports at the same time. You can change one then the other very soon afterwards.

Mark asked "What DAC ?" not "What is a DAC ?"

It is relatively simple with bit shifting and masking to move a 10 bit value into PORTD 2:7 and PORTB 0:3.

What people have been asking, is how will you write those values into the DAC? Or, conversely, how will the DAC read the values?

void setup() {
  Serial.begin(9600);
  int ten_bit_number = 789;//test number
  Serial.print("Value = ");
  Serial.println(ten_bit_number);
  Serial.print ("Binary = ");
  Serial.println(ten_bit_number, BIN);

  byte lo_6 = ten_bit_number & B00111111;
  Serial.print("lo_6  ");
  print_bits(lo_6, 6);
  byte hi_4 = (ten_bit_number >> 6) & B00001111;
  Serial.print("hi_4  ");
  print_bits(hi_4, 4);
 

  PORTD = (lo_6 << 2) & B11111100;
  Serial.print("PORTD = ");
  print_bits(PORTD, 8);
  PORTB = (hi_4) & B00001111;
  Serial.print("PORTD = ");
  print_bits(PORTB, 8);

  int value = (PORTB << 6 | PORTD >> 2);
  Serial.print("Combined 10 bit value ");
  Serial.println(value, BIN);
  Serial.println(value);
}

void loop() {}

void print_bits (byte val, byte bits)
{
  for (int i = bits - 1; i >= 0; i-- )
  {
    Serial.print((val >> i) & 0X01);//shift and select first bit
  }
  Serial.println();
}

It is relatively simple with bit shifting and masking to move a 10 bit value into PORTD 2:7 and PORTB 0:3.

At the same time ?

DoZu:
Sorry Mark, I thought DAC (digital to analog converter) is common word in this forum. Thank you Larry for your suggestion, I will keep in my mind. I am just curious whether the UNO could be used as DAC 10 bit without any additional peripheral devices.

I am also curious about what DAC. For 10 bit resolution, you need some very specialized circuitry. A resistor ladder made from off the shelf components, driven from the digital outputs, would not be accurate enough.

My logic in asking my first question was based on the fact that there are few, if any, devices which require all the pins to be written to at the same time.

A If you are talking to another chip using a parallel interface then the interface would consist of the data/address lines plus control lines. In this case we setup the data/address taking as long as we like and then hit the control lines. The device ignores the data/address until we hit the control lines - so no need to write to all the bits of two ports at the same time.

OR

B the output is something like a set of LEDs to be read bu a human - in which case who cares about speed humans are so sloooooow.

When told it was a DAC, then what DAC is the obvious question.

I'll be interested to see if the OP knows what a DAC does and therefore find out why he thinks it needs 10 pins.

Mark