/*************************************************************************************************** * HIFIDUINO v. B021 * * October 15, 2010 * Arduino code for Buffalo II DAC and other DACs based on the Sabre32 DAC chip. This code is adapted * from an earlier version developed for the Wolfson 8741 DAC chip. For more information visit * www.hifiduino.wordpress.com or www.hifiduino.blogspot.com * * Although this code is based on a confidential data sheet for which I've signed an NDA, I've also * requested and have received permission to publish this code. * * Change log: * v. B01 10/11/10: Volume control, LCD, Rotary Encoder * v. B02 10/15/10: Added reading of sample rate * v. B021 10/18/10: No new functionality, but cleaned up the code and comments ***************************************************************************************************/ // LIBRARIES #include <LiquidCrystal.h> // For LCD // Initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 10, 9, 8, 7); #include <Wire.h> // For I2C // CONSTANT DEFINITION // (The digital volume for Sabre32 is 0 to -127 db in .5 db steps) #define DEFAULTVOL 0x64 //-50 dB this is 50x2=100 or 0x64 #define MINVOL 0xC6 //-99dB this is 99X2=198 or 0xC6 #define MAXVOL 0x00 //-0 dB #define DIMVOL 0x3C //-60dB The volume level when dimming the volume #define VOLUPPIN 4 // Button to increase volume or RotEnc A terminal #define VOLDOWNPIN 2 // Button to decrease volume or RotEnc B terminal #define INTERVAL 2 // Time interval in seconds for doing something like reading the sample rate // GLOBAL VARIABLE DECLARATION byte regVal; // Variable to pass register value byte regAddr; // Variable to pass register value byte currVol=DEFAULTVOL; // Variable to hold the current volume value unsigned long previousMillis = 0; // Stores last recorded time /* LCD For the LCD, I am using a "standard" HD44780 20x4 display, and I am using the official Arduino LiquidCrystal library that comes with the standard installation. This is as standard as it can be and this type of LCD is available everywhere The pin assignment is different from the example code and it is as follows: * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 10 * LCD D5 pin to digital pin 9 * LCD D6 pin to digital pin 8 * LCD D7 pin to digital pin 7 */ /* ROTARY ENCODER The rotary encoder is connected to pins 2 (A) and pin 4 (B). It does not matter which terminal is connected to which pin. The third terminal is connected to GND. At each cycle, the rotary encoder will pull the pins LOW and the transition is detected (an interrupt is generated). It is then compared with the level in the other pin. The interrupt service routine is specified below. Debouncing For this code to work, the rotary encoder is "debounced" by installing capacitors between the signal pins an GND in order to minimize the noise generated by the mechanical switches of the rotary encoder. Without this "debouncing", spurious readings will make the volume jump several clicks. If an optical rotary encoder or a high quality encoder is used, this modification may not be needed. The rotary encoder used here is a low cost model ($1.00) and requires the installation of the capacitors. INTERRUPT SERVICE ROUTINE It is used to determines direction. The code displays volume attenuation, so if the value increased, there is more attenuation and the volume level goes down. This is counter clockwise motion in the rotary encoder. Likewise if the value decreases, there is less attenuation and the volume level goes up. This is clockwise direction */ volatile boolean attenuUp=false; // flag to indicate more attenuation or counter clockwise motion volatile boolean attenuDown=false; // flag to indicate less attenuation or clockwise motion void rotEncoder() { if (digitalRead(2) == digitalRead(4)) { attenuUp = true; //if on interrupt the encoder channels are the same, direction is CCW } else { attenuDown = true; //if they are not the same, direction is CW } } /* READING THE SAMPLE RATE The sample rate can be calculated by reading the registers of the 32 bit DPLL value. For SPDIF this value is divided by (2^32/Crystal-Frequency). In Buffalo II, the Crystal frequency is 80,000,000 Hz. In Arduino (and other small microprocessors) it is NOT advisable to do floating point math because "it is very slow"; therefore we will use interger math to calculate the sample rate. In order to increase the accuracy of the integer calculation, I did some evaluation of the DPLL register values for sample rates ranging from 44.1K to 192K. I noticed that I could multiply the value of the DPLL number by up to 400 without overflowing the 32-bits. The value of 2^32/80000000 is 53.687091 (which is a floating point number). If we use the integer part (53 or 54) we will have an error in the calculation. For example, if we divide the DPLL number for a 44.1K sample rate signal by 53, the resultant values would be 44.677K and 43.849K if divided by 54. If we divide by the actual number 53.687091, then we obtain 44.105K (The 4 Hz deviation from ideal is within the SPDIF specification and crystal tolerances of the source signal). Clearly the integer math is not very accurate. However, since we have 32 bit number to work with, we can multiply the DPLL number by 400 and then divide by 400X53.687091=21475. If we do this, we obtain 44.105K which is within rounding off error of the exact value (the one using floating math) */ // Sample rate reading routines volatile unsigned long DPLLNum; // Variable to hold DPLL value byte readDPLL(byte regAddr) { Wire.beginTransmission(0x48); // Hard coded the Sabre/Buffalo device address Wire.send(regAddr); // Specify the DPLL register from which to read value Wire.endTransmission(); Wire.requestFrom(0x48,1); // Hard coded to Buffalo, request from address specified with Wire.send() while(!Wire.available()) { } // Do nothing and wait for the data on the wire -if needed... return Wire.receive(); } unsigned long sampleRate() { DPLLNum=0; // Reading the 4 registers of DPLL one byte at a time // and stuffing into a single 32-bit number DPLLNum|=readDPLL(31); DPLLNum<<=8; DPLLNum|=readDPLL(30); DPLLNum<<=8; DPLLNum|=readDPLL(29); DPLLNum<<=8; DPLLNum|=readDPLL(28); // Calculating the sample rate for SPDIF DPLLNum*=400; DPLLNum/=21475; return DPLLNum; } /* CONTROLLING THE DIGITAL ATTENUATION (VOLUME) IN THE DAC The device address of Sabre DAC Datasheet specifies the address as 0x90 which is an 8-bit value. The wire library in Arduino uses 7-bit device addresses and the 8th R/W bit is added automatically depending on whether you use the write call [beginTransmission()] or the read call [requestFrom()]. Therefore, you will use the 7 most significant bits of the 8-bit address. In our example, 0x90 becomes 0x48 as follows: 0x90: 0101000 (we eliminate the rightmost bit) 0x48: 0010100 */ void writeVolRegister(byte regAddr, byte regVal) { Wire.beginTransmission(0x48); //Hard coded to the the Sabre/Buffalo device address Wire.send(regAddr); // Specifying the address of volume register Wire.send(regVal); // Writing the volume value into the register Wire.endTransmission(); } void setSabreVolume(byte regVal) { writeVolRegister(0, regVal); // set up volume in DAC1 writeVolRegister(1, regVal); // set up volume in DAC2 writeVolRegister(2, regVal); // set up volume in DAC3 writeVolRegister(3, regVal); // set up volume in DAC4 writeVolRegister(4, regVal); // set up volume in DAC5 writeVolRegister(5, regVal); // set up volume in DAC6 writeVolRegister(6, regVal); // set up volume in DAC7 writeVolRegister(7, regVal); // set up volume in DAC8 } /* PRINTING (DISPLAYING) THE VAVLUE OF VOLUME "NICELY FORMATTED" For now, it is hardcoded to print on line 3 (4th line) of the LCD. This routine takes in the actual value of the volume level and displays it with proper format in the LDC. */ void printVol(byte regVal) { if (regVal==9) // transition between two digit and one digit display { lcd.setCursor(12,3); lcd.print("0 "); // Add a leading zero } if (regVal<10) // two digit to one digit transistion, right justification { lcd.setCursor(13,3); lcd.print(regVal, DEC); } else { lcd.setCursor(12,3); lcd.print(regVal, DEC); } } /*************************MAIN PROGRAM*************************************************************/ void setup() { // Set up the LCD's number of columns and rows: lcd.begin(20, 4); // Join the I2C bus as a master Wire.begin(); // Attach Interrupts attachInterrupt(0, rotEncoder, CHANGE); // ISR for rotary encoder // Set up the pin modes pinMode(VOLUPPIN, INPUT); // Button or Encoder pin for volume up digitalWrite(VOLUPPIN, HIGH); // Enable pull-up resistor pinMode(VOLDOWNPIN, INPUT); // Button or Encoder pin for volume down digitalWrite(VOLDOWNPIN, HIGH); // Enable pull-down resistor // Print the welcome message and other labels to the LCD lcd.setCursor(0,1); lcd.print("BUFFALO CONTROL v0.2"); lcd.setCursor(0,2); lcd.print("Sample Rate: "); lcd.setCursor(0,3); lcd.print(" Volume: - dB"); // Set the default volume at power up, otherwise the DAC defaults at full volume setSabreVolume(DEFAULTVOL); printVol(DEFAULTVOL/2); } void loop() { // Print the sample rate (once every "INTERVAL" time) if(millis() - previousMillis > INTERVAL*1000) { previousMillis = millis(); // Saving last time we display sample rate lcd.setCursor(13,2); lcd.print(sampleRate(), DEC); } // Asjusting the volume down (larger numbers since we are displaying attenuation) while(attenuUp==true) // While there is CCW motion in the rotary encoder { attenuUp=false; // Reset the flag if (currVol// Check if already at maximum attenuation (minimum Volume) { currVol=currVol+2; // Increase 1 dB setSabreVolume(currVol); // Write value into registers printVol(currVol/2); // Divide by 2 to print in whole dBs } } // Adjusting the volume up (smaller numbers since we are displaying attenuation) while(attenuDown==true) // While there is CW motion in rotary encoder { attenuDown=false; // reset the flag if (currVol>MAXVOL) // Check if already at max Volume { currVol=currVol-2; // Increase 1 dB setSabreVolume(currVol); // Write value into registers printVol(currVol/2); // Divide by 2 to print in whole dBs } } }
Advertisement