0

I have a buffer which contains 16000 PCM samples of 8Khz 8-bit mono. I am trying to play it using 12 bit MCP4725 DAC. I have tried using micros() to control the write interval for the DAC. Here's my code -

uint8_t soundData[16000] = { 234,206,79,255,249,....,210,222 }; // 8Khz 8-bit mono

void setup() {

Serial.begin(115200);
Wire.begin(D2, D1);
delay(100); // delay 100 ms
Serial.flush();
delay(1000);

} 

void value_write(uint16_t temp){

  Wire.beginTransmission(0x62);
  Wire.write(64); 
  Wire.write(temp >> 4);        // the 8 most significant bits...
  Wire.write((temp & 15) << 4); // the 4 least significant bits...
  Wire.endTransmission();

}

void loop() {

  unsigned long currentMicros = micros();

// 125 uS sampling for 8KHz signal    
if(currentMicros - previousMicros > 125) {
  Serial.print("Index: ");
  Serial.println(indx);
  uint16_t temp = map(soundData[indx++], 0, 255, 0, 4095);

  value_write(temp);
}


}

The write should logically be completed within 2 seconds but takes way more time. Any help regarding successfully writing the PCM values to the DAC at 8000Hz is greatly appreciated.

Ashish K
  • 905
  • 10
  • 27
  • Trying to generate (proper) sound with a constant data rate over an asynchronous serial interface with no buffers is most probably futile - Your transmission speed is very probably simply not fast enough. – tofro Jan 11 '18 at 21:35
  • @tofro what do you suggest? – Ashish K Jan 11 '18 at 21:37
  • #1 do some math: Calculate worst case throughput on I2C in your setup - In case you can transmit much more than 4 * 8000 = 32000 bytes per second (you need 4 bytes per sample), your approach may be feasible (better measure than only calculate) , although I have seen nothing in the datasheet that says how long the chip needs from transmission of the last command byte to analogue output. If not, get a better DA. – tofro Jan 11 '18 at 21:49

0 Answers0