Hi again to this wonderful forum who helped me the last time.
I built a previous preamp using another AD chip which is still in constant use today.
I have adapted the the software from that build to this new project but am stuck with the communication for the volume control of the DS1882.
Here is the full code code as you can see in the photo it is driving an OLED display and also controls the selection of the relays. I'm using encoders for both volume and input selection and those work perfectly.
I have checked the out put from the Uno SDA and SCL with a scope and the voltages and frames are there.
I think my problem is how to send the bytes to the DS1882 in the data sheet it gives me the impression that one should send 4 bytes of data one to address the chip, second byte channel 1 volume, third byte channel 2 volume and 4th byte command to tell it what to do. I have tried having the stop transmission in various places but still no volume control of the DS1882.
Can some kind person please look over my code and advise where my error could be?
#include <Encoder.h>
#include <Wire.h>
#include <U8glib.h>
U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, DC = 9, RST = 8
Encoder Volume(3,2);
Encoder Selector(A0, A1);
int ledpin1 = 5; //led 1
int ledpin2 = 6; //led 2
int ledpin3 = 7; //led 3
int finalVolume=0; //values can now be negative which makes things easier
int maxVolume=63; //change limits as you wish; I expect minimal value is 0
int finalSelector=0;// this ensures the unit always starts at the first chosen input
int maxSelector=2;
const byte potaddress = B01010000;
const byte potconfig = B10000110;
void setup() {
Serial.begin(9600);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(ledpin3, OUTPUT);
digitalWrite(ledpin1,LOW);//to make sure that ledpin is on the first channel on switch on
Wire.begin(); // join i2c bus (address optional for master)
}
void titlepage() {
u8g.setFont(u8g_font_fub11);
u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.
u8g.drawStr( 30, 13, "Willow II");
u8g.drawStr( 0, 33, "Volume");
u8g.drawStr( 0, 58, "Input");
enum {BufSize=5}; // If a is short use a smaller number, eg 5 or 6
char buf[BufSize];
snprintf (buf, BufSize, "%d", finalVolume);
u8g.drawStr(65, 33, buf);
switch (finalSelector) {
case 0:
u8g.drawStr(65, 58, "COMP");
break;
case 1:
u8g.drawStr(65, 58, "TUNER");
break;
case 2:
u8g.drawStr(65, 58, "PHONO");
break;
}
}
void draw(){
titlepage(); }
long oldVolume = -999;
void loop() {
u8g.firstPage();
do { draw();
}while(u8g.nextPage());
//set new value depending on encoder settings
finalVolume = Volume.read();
//now check if finalVolume is not out of range
if (finalVolume < 0) finalVolume=0;
else if (finalVolume > maxVolume) finalVolume=maxVolume;
////the next is the selector
finalSelector = Selector.read()/4;
//finalSelector = constrain(finalSelector,0 ,2);
if (finalSelector < 0) finalSelector =0;
else if (finalSelector > maxSelector) finalSelector=maxSelector;
if (finalSelector == 0) { digitalWrite(5,LOW);
} else {digitalWrite (5,HIGH);}
if (finalSelector == 1) { digitalWrite(6,LOW);
} else {digitalWrite(6,HIGH);}
if (finalSelector == 2) { digitalWrite(7,LOW);
} else {digitalWrite (7,HIGH);}
long finalVolume = Volume.read();
if (finalVolume != oldVolume) {
oldVolume = finalVolume;
if(finalVolume >= 63) {
finalVolume = 63;
oldVolume = 63;
Volume.write(63);
}
if(finalVolume <= 0) {
finalVolume = 0;
oldVolume = 0;
Volume.write(0);
}
//next piece of code controls the digital pot
Wire.beginTransmission(potaddress); // transmit to device
// device address is specified in datasheet
Wire.write(finalVolume); // sends instruction byte to first channel sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
Wire.beginTransmission(potaddress); // transmit to device
Wire.write(finalVolume | B01000000); // sends instruction byte to second channel
Wire.write(potconfig);
Wire.endTransmission(); // stop transmitting
}
}
DS1882.pdf (218 KB)