Go Down

Topic: PCM Audio With R2R Dac (Read 764 times) previous topic - next topic

It's late and I've been trying allday to play a WAV/PCM file but can't get it to work.  I succesfully played the WAVE/PCM using one of the PWM/PCM libraries.  I then wanted to try and play the same WAV/PCM data with the r2r dac.  So I modified the timer interupts to use CTC mode instead of fast PWM and just copy the PCM data to PORTD inside the interupt.  I built an R2R with 10k and 20k resistor from a design from an intstructable.com and got the sine wave sample code to work.  Then I modifed the code to read from progmem and copy the PCM data to PORTD but I get nothing. 

My understand is an 8K 8-bit PCM data is the sampled audio wave data so I should be about to send it to the DAC at 8K rate and reproduce the audio wave. I know very little about audio.  I've been reading about audio and PCM all day.

Any help would be very appreciated. 

Here is my sktech:

Code: [Select]

#define uint8_t byte
#define SAMPLE_RATE 8000

#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "sounddata.h"

volatile uint16_t sample = 0;

// This is called at 8000 Hz to load the next sample.
  if (sample >= sounddata_length) {
  else {
    PORTD = pgm_read_byte(&sounddata_data[sounddata_length-1]);


void startPlayback()
  //set digital pins 0-7 as outputs
  for (int i=0;i<8;i++){

//set timer2 interrupt at 8kHz
  TCCR2A = 0;// set entire TCCR2A register to 0
  TCCR2B = 0;// same for TCCR2B
  TCNT2  = 0;//initialize counter value to 0
  // set compare match register for 8khz increments
  OCR2A = 249;// = (16*10^6) / (8000*8) - 1 (must be <256)
  // turn on CTC mode
  TCCR2A |= (1 << WGM21);
  // Set CS21 bit for 8 prescaler
  TCCR2B |= (1 << CS21);   
  // enable timer compare interrupt
  TIMSK2 |= (1 << OCIE2A);


void setup()
  pinMode(13, HIGH);

void loop()


Before even checking your code for possible errors, what exactly does your audio output sound like?
Have you tried listening to it?
If so, how?  (you will need to drive a small audio amp as with those R2R DAC values, you can't drive a speaker directly).

Go Up