/*
* Digital Volume Control with NJR MUSES72320
*
* The connections:
* DREF (MUSES72320 pin 21) - to Arduino's GND via a 10kOhm resistor (10k zit al op Muses bord)
* DVDD (MUSES72320 pin 17) - to +5V via a 10kOhm resistor (10k zit al op Muses bord)
* CLOCK (MUSES72320 pin 19) (CTRL pin 3) - to digital pin D13 (SCK pin Nano Every)
* DATA (MUSES72320 pin 18) (CTRL pin 4) - to digital pin D11 (MOSI pin Nano Every)
* LATCH (MUSES72320 pin 20) (CTRL pin 5) - to digital pin D8 (SS pin Nano Every)
* Z/C (MUSES72320 pin 32) (CTRL pin 8) - to digital pin D10 (can be any digital pin)
* 5V (power for the arduino) (CTRL pin 2) - to 5V pin (it's ok to power a Nano Every this way. When using a UNO, Mega, and others, you may need to cut the 5V wire in the USB cable to avoid problems when connecting to a powered muses board)
* GND (pwr gnd arduino) (CTRL pin 6) - to power GND pin
*
* Pot - 0-5V to A0
*
* created 19 Dec 2024
* by erwinp
Todo: ZC pin nog gebruiken? zero crossing
*/
// setup for MUSES 72320 control
#include <SPI.h>
#define SPIMAXATTNU 54784 // Take the binary from the datasheet and convert to ordinary number, SPI transfer do not care. -99db.
#define SPIMINATTNU 4096 // 0 db
#define SPIDEFAULTATTNU 29696 //50 db
#define SPIMUTE 65280
#define SPIINIT 32832 // Inital setup. Set L/R Cont1 = 1 for controlling both channels at the same time.
// Set SPI SS PIN 8 Arduino Nano Every. Other SPI pins are connected according to Arduino specification.
const int csPin = 8; //SS PIN
long Volume; //zelf toegevoegd
long oldVolume; //zelf toegevoegd
float VolumedB; //zelf toegevoegd
word VolBinary;
void setMusesVolume(word regVal)
{
digitalWrite(csPin, LOW); // MUSES72320 datasheet seems to like to go low before transmission
delayMicroseconds(1);
SPI.transfer(highByte (regVal)); // split the SPI transfer in high and lowbyte
SPI.transfer(lowByte (regVal));
delayMicroseconds(1);
digitalWrite(csPin, HIGH);
}
void startMuses(){
Serial.print(" startMuses: ") ;
Serial.println("check");
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE2);
// initialize SPI:
SPI.setClockDivider(SPI_CLOCK_DIV64); // Set correct speed for SPI muses chip
digitalWrite(csPin, LOW); // SPI SS PIN low - MUSES72320 datasheet seems like this go low before transmission
// SPI setup MUSES72320 l/r control
delayMicroseconds(1);
SPI.transfer(highByte (SPIINIT)); //
SPI.transfer(lowByte (SPIINIT));
delayMicroseconds(1);
digitalWrite(csPin, HIGH); // MUSES72320 datasheet seems like it go high after transmission
//setMusesVolume (SPIDEFAULTATTNU);
}
void setup() {
Serial.begin(9600); //voor debuggen
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
delay(1000);
startMuses();
}
void loop() {
Volume = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
Volume = map(Volume, 0, 1023, 223, 0); // scale it to the 223 steps between -111.5dB to 0dB
// volume to attenuation data conversion:
// #=====================================#
// | 0.0 dB | in: [ 0] -> 0b00010000 |
// | -111.5 dB | in: [223] -> 0b11101111 |
// #=====================================#
if (Volume != oldVolume) {
VolBinary = (min(Volume, 223) + 0x10);
setMusesVolume(VolBinary);
VolumedB = -Volume*0.5;
Serial.print(VolumedB) ;
Serial.print("dB ") ;
Serial.print(" VolBinary: ") ;
// Serial.println(VolBinary,BIN);
//Serial.println(VolBinary);
for (int a=7; a >=0; a--) {
Serial.print ( bitRead (VolBinary, a));
Serial.print ("");
}
Serial.println ("");
}
oldVolume = Volume;
delay(50);
}