Control volum via pc

Hi, I like to replace the potentiometer of my PC speakers with a digital one ds1802
I like to control the volume via arduino duemilanova over PC. I saw that arduino support SPI communication, ds1820 require some kind 3-wire serial interface , I don’t find some good example too understand how could I use arduino for this purpose, if is possible.
Any help is welcome
Thx

Hi,

you might want to try this sketch:

// Port bit numbers on PORT D
#define SELECT_DS1802 (10) // cannot be changed?; connect to “/RST”
#define MOSI (11) // cannot be changed; connect to “D”
#define MISO (12) // cannot be changed; don’t connect
#define SCK (13) // cannot be changed; connect to “CLK”

void setup()
{
digitalWrite(MISO, HIGH);
digitalWrite(MOSI, LOW);
digitalWrite(SCK, LOW);
digitalWrite(SELECT_DS1802, LOW);
pinMode(MISO, INPUT);
pinMode(MOSI, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(SELECT_DS1802, OUTPUT);

/* initialize SPI /
SPCR = (0 << SPIE) /
SPI Interrupt Enable /
| (1 << SPE) /
SPI Enable /
| (1 << DORD) /
Data Order: LSB first /
| (1 << MSTR) /
Master mode /
| (0 << CPOL) /
Clock Polarity: SCK low when idle /
| (0 << CPHA) /
Clock Phase: sample on rising SCK edge /
| (1 << SPR1) /
Clock Frequency: f_OSC / 64 /
| (0 << SPR0);
SPSR &= ~(1 << SPI2X); /
doubled clock frequency */
delay(300); // wait until POR done
}

void SetVolume(uint8_t vol0, bool mute0, uint8_t vol1, bool mute1)
{
SPITrans((mute1 << 14) | ((vol1 & 0x3f) << 8) | (mute0 << 6) | (vol0 & 0x3f));
}

// pause between two calls must be greater than 200us (tRLT)
void SPITrans(uint16_t writeVal)
{
digitalWrite(SELECT_DS1802, HIGH);
delayMicroseconds(30); //tCC
uint16_t timeout = 0;
//unsigned short readVal;

SPDR = writeVal >> 8;
while(!(SPSR & _BV(SPIF)) && ++timeout != 0);
//readVal = SPDR << 8;

SPDR = writeVal & 0xff;
while(!(SPSR & _BV(SPIF)) && ++timeout != 0);
//readVal |= SPDR;

digitalWrite(SELECT_DS1802, LOW);
//delayMicroseconds(200); //tRLT
//return readVal;
}

void loop()
{
static int volume0 = 0;
static int volume1 = 0;

if(++volume0 >= 64)
volume0 = 0;

if(++volume1 >= 64)
volume1 = 0;

SetVolume(volume0, false, volume1, false);
delay(100);
}

I can’t test it, because I don’t have such a chip.

MikeT