0% found this document useful (0 votes)
6 views

Arduino-DAC8812_encoder-sketch

This document contains an Arduino sketch for controlling a Digital-to-Analog Converter (DAC) using SPI communication. It sets up pins for a DAC and an encoder to adjust the volume level, allowing for volume increase and decrease based on encoder input. The code includes functions to write the DAC level and print the current decibel level to the serial monitor.

Uploaded by

Apsara Dilakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Arduino-DAC8812_encoder-sketch

This document contains an Arduino sketch for controlling a Digital-to-Analog Converter (DAC) using SPI communication. It sets up pins for a DAC and an encoder to adjust the volume level, allowing for volume increase and decrease based on encoder input. The code includes functions to write the DAC level and print the current decibel level to the serial monitor.

Uploaded by

Apsara Dilakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include "SPI.

h"

// Pin 10 = CS
// Pin 11 = Serial data out
// Pin 13 = CLK
// Pin 8 = DACResetPin

const int slaveSelectPin = 10;


const int DACResetPin = 8;
int timedelay = 500;
double dbLevel = -30;
double dbIncrement = 0.5;
unsigned int dac_level;
// Address code 3 = Both A and B DACs
int addr_decode = 3;
// Encoder stuff
const int encoderPowerPin = 4;
const int encoderGroundPin = 5;
int encoder0PinA = 6;
int encoder0PinB = 7;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() {
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin, OUTPUT);
// Set DAC reset pin high to disable that feature
pinMode (DACResetPin, OUTPUT);
digitalWrite(DACResetPin, HIGH);
// Set up pins for encoder:
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
pinMode (encoderPowerPin, OUTPUT);
pinMode (encoderGroundPin, OUTPUT);
digitalWrite(encoderPowerPin,HIGH);
digitalWrite(encoderGroundPin,LOW);
// Serial
Serial.begin (9600);
// Sleep
delay(1000);
Serial.println ("Starting SPI..");
SPI.begin();
Serial.println ("Setting volume to 0..");
DACWrite(addr_decode, 0);
}
///////////////////////////////////////////////////////////////////////////////////
///////////
int WriteAndPrintLevel(){
Serial.print ("dB: ");
Serial.print (dbLevel);
Serial.print (" / DAC Level: ");
Serial.println (dac_level);
DACWrite(addr_decode, dac_level);
}
int VolumeUp() {
if (dbLevel < (0-dbIncrement)) {
dbLevel = dbLevel + dbIncrement;
dac_level = 65536*(pow(10, (dbLevel/20)));
WriteAndPrintLevel();
}
}
int VolumeDown() {
if (dbLevel > -102.5) {
dbLevel = dbLevel - dbIncrement;
dac_level = 65536*(pow(10, (dbLevel/20)));
WriteAndPrintLevel();
}
}

///////////////////////////////
// Main loop
////////////////////////////////
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
VolumeUp();
} else {
VolumeDown();
}
}
encoder0PinALast = n;
}

///////////////////////////////
// Function to set DAC level
////////////////////////////////
void DACWrite(int addr_decode, unsigned int level) {
digitalWrite(slaveSelectPin,LOW);
SPI.transfer(addr_decode);
byte highByte = level >> 8;
unsigned int lowBytetemp = level << 8;
byte lowByte = lowBytetemp >> 8;
SPI.transfer(highByte);
SPI.transfer(lowByte);
digitalWrite(slaveSelectPin,HIGH);
}

You might also like