Pages: [1]   Go Down
Author Topic: DS1881 Volume Control  (Read 1274 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

I seem to be having a problem using the DS1881. After trying to get it to work for hours, I am turning to everyone here. smiley I am trying to use the DS1881 as a volume control and I can't seem to figure out how to get it working properly. I was setting the VOL variable and it would turn on the speakers at -1 but that was the only volume change that would happen otherwise it was always muted. I have read the datasheet and it seems that it is me that is not working correct. smiley-wink There is code that is commented out as I was trying different ideas to see if it would work.

Does anyone have any suggestions for me?

Code:
#include <Wire.h>

int CEVolume = 6;
int volButton = 8;
int val = 0;     // variable for reading the pin status
int vol = 0;
int stringOne;

void setup()
{
  Wire.begin();

  Serial.begin(9600);
  pinMode(CEVolume, OUTPUT);
  digitalWrite(CEVolume, HIGH);
 
  pinMode(volButton, INPUT);
  vol = 34;

}

void loop()
{

  val = digitalRead(volButton);
  if (vol == 63)
  {
    vol = 34;
  }
  if (val == HIGH)
  {
    vol--;
    changeVolume();
  }
  Serial.println(vol);
  delay(1000);
}



void changeVolume()
{
  digitalWrite(CEVolume, LOW);
  Wire.beginTransmission(0x28);
    Wire.write(B01010000);
    //stringOne = String(vol, HEX);
    Wire.write(B00001111); //Command Byte - Pot 0
    //Wire.write(B01111111);
    Wire.write(B01001111); //Command Byte - Pot 1
    //Wire.write(B10000111);
    Wire.write(0x87); //Command Byte - Config Reg
  //int x = Wire.endTransmission();
  //Serial.println(vol, DEC);
  Wire.endTransmission();
  digitalWrite(CEVolume, HIGH);
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13718
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


do you have a link to the datasheet of the DS1881?

Wire.beginTransmission(0x28); has a return value that you do not check ...

Code:
void loop()
{

  if (digitalRead(volButton) == HIGH) vol--;
  if (vol >= 63) vol = 34;
  if vol <= 0) vol = 34;

  changeVolume();

  Serial.println(vol);
  delay(1000);
}

Can you explain how changeVolume() works as it nowhere uses the vol parameter?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Whoops, meant to include the link to the datasheet.
http://datasheets.maximintegrated.com/en/ds/DS1881.pdf

I had the changeVolume looking like this

Code:
void changeVolume()
{
  digitalWrite(CEVolume, LOW);
  Wire.beginTransmission(0x28);
    Wire.write(B01010000); //slave address byte
    Wire.write(vol); //Command Byte - Pot 0
    Wire.write(vol); //Command Byte - Pot 1
    Wire.write(0x87); //Command Byte - Config Reg
  Wire.endTransmission();
  digitalWrite(CEVolume, HIGH);
}

I was letting the vol variable go from 34 to negative numbers and it wasn't until vol reached -1 that any sound would come out of the speaker and it was loud. I couldn't get different levels out of it and once I reset vol back to 34, would it be silent again.

So with that not working, I was manually adjusting the volume. From what I understand from the datasheet, there were 4 commands to adjust the pots.

The first one is the slave address, then pot0, pot1, and then the command bytes. I left the command byte to the default setting which should be Configuration Option 2 table so the steps are from 0-33 and 34 is mute. The first two bytes are for the pot address (00 for pot0, 01 for pot1).

That is where I am at right now.

I was manually adjusting the pot0 to see if it was the code that was the issue, I was trying B00100001, which should be mute (34). Hardly any sound came out of the speaker at this point, so I tried B00001111 (15) and still hardly any sound came out of the speaker.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I attached the schematic that I am using. I am using a OPA134UA.

This is my first time using fritzing, sorry if it is hard to read. smiley-wink  The purple wire comes from an input from the computer speaker output and the speaker is driven by a TDA7297. A analog pot does work on the stereo amp but I am trying to get it digitally controlled so I know it is not the amplifier that is the problem.


* ds1881.png (149.13 KB, 886x852 - viewed 43 times.)
Logged

Raleigh, NC
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Big Iron
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure whether the OP ever got this working or not, but here's what I've found.  Posting here to save others the trouble.

You don't need to write the config register (defaults to 0x87) unless you want to change it.  Writing this stores it in the DS1881 EEPROM, so at most you might need to do it on boot.  You likewise don't have to write both pots in the package at the same time unless you want to.

This works:

Code:
digitalWrite(POT_CS, LOW);  // chip select pin for the digital pot
Wire.beginTransmission(0x28);  // if you have only one pot, just ground all three address lines and use this address
Wire.write(B00000000 | pot0Val);  // technically not necessary to or these together, just use pot0Val
Wire.write(B01000000 | pot1Val);  // MSBs should be "01" or you can't write the second pot.
Wire.endTransmission();
digitalWrite(POT_CS, HIGH);

Obviously if you want to write the config register to do stuff like disable zero crossing or make the pot value store in NVRAM, you'll need to do Wire.write(0x80) OR'd with the three config bits.  The default is 0x87, which means the three config bits are 0x0111.  That should work for most audio that you want to run through this thing.  Also be sure to read the data sheet regarding the 1/2 supply voltage bias on the audio signal.  If you just RC couple an AC signal in here it's not going to sound good.
Logged

Pages: [1]   Go Up
Jump to: