title: How to implement 8-bit DAC (Digital to Analog conversion) with Arduino - atmega 2560 microcontroller?
body: <p>I am working on a Recorder and Player Project. I want to implement both ADC (Analog to Digital) and DAC (Digital to Analog) Convertors. The code should be implemented in Arduino - Atmega 2560 (Atmel Microcontroller). I have tried to implement the ADC and I found the following code: </p>
  
  <pre><code>void setup() 
  {
      Serial.begin (9600);
      ADCSRA |= ((1&lt;&lt;ADPS2) | (1&lt;&lt;ADPS1) | (1&lt;&lt;ADPS0));
      ADMUX |=(1&lt;&lt;REFS0);
      ADCSRA |= (1&lt;&lt;ADEN);
      ADCSRA |= (1&lt;&lt;ADSC);
  }
  
  int read_adc( int channel )
  {
      ADMUX &amp;=0xE0;
      ADMUX |= channel &amp; 0x07l;
      ADCSRB = channel &amp; (1&lt;&lt;ADSC);
      ADCSRA |= (1&lt;&lt;ADSC);
      while (ADCSRA &amp; (1&lt;&lt;ADSC))
      {
          return ADCW;
      }
  }
  
  void loop() 
  {
      int w = read_adc(0);
      Serial.write(w);
  }
  </code></pre>
  
  <p>Here I am sending the digitized signal to the serial port. I have two Questions: 
  1- How can I modify this code to work as 8-bit ADC? 
  2- How to implement DAC the same way as before ? I mean how to read digital signal from the serial and then convert it to Analog signal like alorithm above?</p>
  
  <p>Thank you for your help.</p>