More on the R-2R ladder as a DAC. This time some work on the firmware.


What I'm doing here is the reverse of what happens when we digitize a signal; at regular intervals I have to come up with

the value for the waveform at that moment in time rather than read the value. When filtered, that will then reconstruct the



To do that I need an accurate time interval. The only sensible way to do that on the Arduino is with interrupts, so I had

to have a quick read on how that works. It's not too complicated - just a short set-up of the timer and an interrupt

routine. Here's my test sketch modified to use timer 2 to generate an interrupt every 10uS and put out a value to the DAC.


/* AD Test */
unsigned int tableOffset = 0;             // output count
unsigned int tableStep = 5120;
//  --- Sine table - generated by sineTable.exe 
unsigned int sineTable[512] = {
void setup() {
  // set the digital pins as outputs:
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  digitalWrite(A0, HIGH);   
  digitalWrite(A1, HIGH);   
  digitalWrite(A2, HIGH);   
  digitalWrite(A3, HIGH);   
  // timer 2 set up
  cli();                  // disable interrupts
  TCCR2A = 0;             // control register all 0
  TCCR2B = 0;             // control register all 0
  TCNT2 = 0;              // set count to 0
  OCR2A = 159;            // period = 160 x 1/16MHz = 10uS
  TCCR2A |= (1 << WGM21); // mode is clear on match
  TCCR2B |= (1 << CS20);  // no prescaler   
  TIMSK2 |= (1 << OCIE2A);  // enable interrupt on match
  sei();                  // enable interrupts
  tableOffset = tableOffset + tableStep;
  PORTD = (sineTable[tableOffset >> 7]) >> 8;
void loop() {


Why 10uS? It needs to be fast enough to give several samples for each cycle at the highest frequency I want to generate

(20kHz) but if I run it too fast the processor won't keep up. This is far from perfect - even a third-order filter can't

take out the sample frequency to the extent that we would like - but remember this is cheap and cheerful and there's a

limit to how good a £2 [plus an Arduino] piece of test equipment can be.


I changed the sine table to be 512 entries so that it now spans a full cycle (before it was just one quadrant). I did that

because of the limited time that there is to do arithmetic in the interrupt routine.


To generate different frequencies, I step along the sine table at a rate that's appropriate for the particular frequency

we're trying to achive. The variable that holds the table index is a 16-bit unsigned int. The top 9 bits are the index into

the table. The lower 7 bit are a fraction of an entry - I'm doing arithmetic here with an implied fixed point as that

allows better resolution of frequency. When a particular frequency is asked for via a SCPI message there will need to be

some arithmetic done to work out the step value, but once that's done the process of reading out the table values is simply

by adding the step to the current offset which is nice and fast within the interrupt routine. There isn't even a need to

test for the end of table as the natural overflow from working 16 bits brings the index round to the start again.

Here's the DAC output on the 'scope. You can see the samples coming out at 10uS intervals, so I must have got the timer

set-up code right.



Below is another view where you can see several cycles - because the step value in this case doesn't have a simple

relationship to the cycle, the samples are different for each cycle, but when filtered they'll give the frequency that we




Finally, here's an interesting trace - this is the previous one accumulated. This one shows that, though the step isn't a

simple divion of the table size, it does repeat after a few cycles. (The filtered waveform is blurred because the trigger

is on the 10uS steps of the yellow trace which don't have a simple relationship to the frequency generated.)



Next part will be combining that with the SCPI parser and seeing what happens.


Part one: Arduino: R-2R Experiment

Part two: Arduino: R-2R: Sine On You Crazy Diamond

Part three: Arduino: R-2R: Buffer, Attenuate, and Filter

Part four: Arduino: R-2R: "We Interrupt This Programme..."

Part five: Arduino: R-2R: "Resistance is..."?

Part six: Arduino: R-2R: Setting the Output Frequency

Part seven: Arduino: R-2R; "A Sweep is as Lucky, as Lucky Can Be..."

Part eight: Arduino: R-2R: Setting the Signal Amplitude