Here's the code -
Code: Select all
#include <Wire.h>
#include "Adafruit_TPA2016.h"
Adafruit_TPA2016 audioamp = Adafruit_TPA2016();
int vol;
void setup() {
Serial.begin(9600);
Serial.println("TPA2016 Audio Test");
audioamp.begin();
// Dump register map, for debugging purposes.
/*
for (uint8_t i=1; i<8; i++) {
Serial.print("Register #"); Serial.print(i);
Serial.print(": 0x");
Serial.println(audioamp.read8(i), HEX);
}
*/
// Turn off AGC for the gain test
audioamp.setAGCCompression(TPA2016_AGC_OFF);
// we also have to turn off the release to really turn off AGC
audioamp.setReleaseControl(0);
// We can update the gain, from -28dB up to 30dB
for (int8_t i=-28; i<=30; i++) {
Serial.print("Gain = "); Serial.println(i);
audioamp.setGain(i);
delay(500);
}
// Each channel can be individually controlled
Serial.println("Left off");
audioamp.enableChannel(true, false);
delay(1000);
Serial.println("Left On, Right off");
audioamp.enableChannel(false, true);
delay(1000);
Serial.println("Left On, Right On");
audioamp.enableChannel(true, true);
delay(1000);
// OK now we'll turn the AGC back on and mess with the settings :)
// AGC can be TPA2016_AGC_OFF (no AGC) or
// TPA2016_AGC_2 (1:2 compression)
// TPA2016_AGC_4 (1:4 compression)
// TPA2016_AGC_8 (1:8 compression)
Serial.println("Setting AGC Compression");
audioamp.setAGCCompression(TPA2016_AGC_2);
// See Datasheet page 22 for value -> dBV conversion table
Serial.println("Setting Limit Level");
audioamp.setLimitLevelOn();
// or turn off with setLimitLevelOff()
audioamp.setLimitLevel(25); // range from 0 (-6.5dBv) to 31 (9dBV)
// See Datasheet page 23 for value -> ms conversion table
Serial.println("Setting AGC Attack");
audioamp.setAttackControl(5);
// See Datasheet page 24 for value -> ms conversion table
Serial.println("Setting AGC Hold");
audioamp.setHoldControl(0);
// See Datasheet page 24 for value -> ms conversion table
Serial.println("Setting AGC Release");
audioamp.setReleaseControl(11);
// Turn off AGC for the gain test
audioamp.setAGCCompression(TPA2016_AGC_OFF);
// we also have to turn off the release to really turn off AGC
audioamp.setReleaseControl(0);
}
void loop() {
vol=map(analogRead(A0),0,1023,0,30);
Serial.print("Gain = "); Serial.println(vol);
audioamp.setGain(vol);
delay(400);
}