Hi all!
Im working on a volume controller, based on Texas Instrument`s PGA2310 Digital volume-controller chip, but im having some trouble making it work at all.
Here is how my code looks:
#include "SPI.h"
const int PGA_CS_PIN = 48;
const int PGA_SCLK_PIN = 52;
const int PGA_SDI_PIN = 51;int timedelay = 100;
int volumeLevel = 55;void setup() {
pinMode(PGA_CS_PIN,OUTPUT);
pinMode(PGA_SCLK_PIN,OUTPUT);
pinMode(PGA_SDI_PIN,OUTPUT);
digitalWrite(PGA_CS_PIN,HIGH);
digitalWrite(PGA_SCLK_PIN,HIGH);
digitalWrite(PGA_SDI_PIN,HIGH);
Serial.begin(9600);
}void PGA_set_volume(byte value)
{
byte shifted_val = (value << 1);
digitalWrite(PGA_CS_PIN, LOW); // assert CS
SPI_write(shifted_val); // right value (0..255)
SPI_write(shifted_val); // right value (0..255)
digitalWrite(PGA_CS_PIN, HIGH); // deassert CS
Serial.print(shifted_val);
}static inline void SPI_write(byte out_spi_byte)
{
byte i;
// loop thru each of the 8-bits in the byte
for (i=0; i < 8; i++) {
// strobe clock
digitalWrite(PGA_SCLK_PIN, LOW);
// send the bit (we look at the high order bit and 'print' that to the remtoe device)
if (0x80 & out_spi_byte) // MSB is set
digitalWrite(PGA_SDI_PIN, HIGH);
else
digitalWrite(PGA_SDI_PIN, LOW);
// unstrobe the clock
digitalWrite(PGA_SCLK_PIN, HIGH);
out_spi_byte <<= 1; // left-shift the byte by 1 bit
}
}void loop()
{
// Reset the volumeLevel if it goes over 128. I think it can actually go to 255.
//if (volumeLevel > 254) {
//volumeLevel = 1;
//}
// Set the volume
PGA_set_volume(volumeLevel);
delay(timedelay);
volumeLevel = volumeLevel + 1;
}
And using the serial communication too see what the arduino says to the chip, this is what comes:
"$&(*,.02468:<>@BDFHJLNPRTVXZ^`bdfhjlnprtvxz|~???????
I have no idea why this happens, Ive just started with arduino so im basically an extremely newbie... :o
Some of this code comes from what linux-works once posted, basically its the only thing i can find, except linux-works PGA23XX library witch i dont now how works.
Can some one help sort this out?
Thanks,
Stian