Arduino R2R DAC: Writing to Ports

A few days ago I wrote a post outlining the R2R Digital to Analog Converter. This project is in it’s early stages, but there were a few refinements I wanted to make prior to expanding it’s functionality. The first thing I want to modify is the way that the code handles writing values to the digital pins.

The digitalWrite function on Arduino is only capable of changing the state of one pin at a time. This means we are currently writing to PIN_8, then PIN_9, then 10 and so on. This becomes an issue at high speeds since I am setting my voltage levels based on the sum of these pin’s outputs. The time in between the first and final pin switching will not match the desired voltage and can cause the wave to break down.

The solution to this problem involves pulling back a layer of abstraction from the Arduino. By bypassing the digitalWrite function and writing directly to the ports of the Arduino we can manipulate these pins at the same time. This will provide us faster and more efficient code.

What’s a Port

In the architecture of ATMEGA chips (The chips that run Arduinos) as well as most microprocessors the pins are organized into a set of Ports. These ports are a way of organizing the i/o pins and simplify the chips internal logic.

In the case of the Arduino we have 3 ports. They are as follows:

  • Port B: Digital Pins 8 – 13
  • Port C: Analog Input Pins
  • Port D: Digital Pins 0-7

This project uses pins 8-11 so I am interested in Port B. But how do I access this port?

Port Registers

Each port functions with the aid of three registers. A register is a special section of volatile (non-permanent) memory. Registers hold parameters or values for the system. Each of these three registers has a different task relating to the port. Further each of these registers is made up of 8 bits with each bit corresponding to a pin in the port.

DDR – Port Data Direction Register – As it’s name suggests the DDR port controls the direction of data flow for each pin in the port. If a bit in this register is set to 1 the corresponding pin is an output. Meanwhile the pin is an input when the corresponding bit is set to zero.

PORT – Port Data Register – This port governs the data being written to the pins in the port. When working with output pins we can write zeros or ones to this register to output high or low on the corresponding pins.

PIN – Port Input Register – This register is less relevant to this project but is still good to be aware of. The input register is where you can read the values being received by any of the input pins.

Each of these registers is set up as a variable within the Arduino IDE making them very easy to access. To access one of these registers we just need to append the port name to the register name. For this project I will be using DDRB and PORTB.

Setting Pin States

void setup() {
  for (int k = 0; k < 4; k++){
    pinMode(DAC_bus[k], OUTPUT);
  }

In my original code I used the loop above to set pins 8-11 to Output. This is not very efficient. Instead we can use the register DDRB to set these pins. One important note is that the lowest pin in the port is tied to the last bit in the register. That means to set pins 8-11 to Output I need to write B00001111 into the register. The “B” here tells the compiler this is a binary number. Since the most significant bits here are all zeros we can even shorten this slightly to give us B1111. This gives us the following setup function.

void setup() {
  DDRB = B1111;      //Set Pins 8-11 as Inputs
}

This shortens our code and minimizes the amount of memory required on the Arduino. Additionally this new setup function will run much faster. As a housekeeping item, I also added a comment to clarify what is happening since it is no longer clear in the code.

Writing To A Port

void test_run2(){
  for (int i = 0; i < 15; i++){
    int value = i;
    for (int k = 0; k < 4; k++){
      digitalWrite(DAC_bus[k], value%2);
      value = value/2;
    }
    delay(1);
  }
}

Above I’ve included my original function to draw a sawtooth pattern. In addition to being inefficient this function is just plain ugly. Using the PORTB register we should be able to get rid of the nested loop and write the value directly to the register. Conveniently the variable i is stored in the Arduino in binary. This means we don’t have to do any kind of conversion, we can write the value directly into the port.

void draw_saw(){
  for (int v_level = 0; v_level < 15; v_level++){
    PORTB = v_level;         
    delayMicroseconds(500);
  }
}

That looks a lot cleaner! By making this change I’ve now assured all of the digital pins update simultaneously. This will allow the oscillator to run much more accurately at high speeds. Additionally I’ve reduced the size of the program freeing more memory in the Arduino.

Notice I also made a few other changes to the function. I have changed the function and variable names to be more descriptive. This makes it much clearer what is happening. Additionally I have swapped the Arduino delay function for delayMicroseconds. This allows me to use much smaller delay times and achieve significantly higher frequencies.

With these changes I can now create saw waves throughout the audio frequency range (20 – 20,000Hz) and well beyond it. I’ve been able to shrink the code while improving performance. Further I’ve honed what will be a valuable skill moving forward. That’s all for today but I hope you are all staying healthy. I’ll see you again soon!