#include /* -------------------------------------------------------------------- */ /* Setup Function */ /* -------------------------------------------------------------------- */ void setup() { Serial.begin(9600); // set SPI and a user LED to be outputs pinMode(SS, OUTPUT); pinMode(MOSI, OUTPUT); pinMode(SCK, OUTPUT); pinMode(43, OUTPUT); SPI.begin(); // starts the SPI library object SPI.setBitOrder(MSBFIRST); // during the transfer of data, send the most significant bit first SPI.setDataMode(SPI_MODE0); // Clock starts at a LOW value and transmits data on the falling edge of the clock signal delay(5); digitalWrite(SS, LOW); // selecting the DAC setUpDAC(); // user defined function digitalWrite(SS, HIGH); // deselecting the DAC } /* -------------------------------------------------------------------- */ /* Loop Function */ /* -------------------------------------------------------------------- */ void loop() { digitalWrite(SS, LOW); sendAddressAndValue(15, 4095); // full analog signal digitalWrite(SS, HIGH); delay(500); digitalWrite(SS, LOW); sendAddressAndValue(15, 3500); // slightly less than full analog signal digitalWrite(SS, HIGH); delay(500); digitalWrite(SS, LOW); sendAddressAndValue(15, 3250); // slightly less again digitalWrite(SS, HIGH); delay(500); digitalWrite(SS, LOW); sendAddressAndValue(15, 3000); // and again digitalWrite(SS, HIGH); delay(500); }//end of loop /* -------------------------------------------------------------------- */ /* User Defined Functions */ /* -------------------------------------------------------------------- */ void sendAddressAndValue(int address, int value){ //valid address values are between 0 and 7; 0 for A, 1 for B, and so on. //An address value of 15 will provide power to all of the outputs simultaneously if(((address>=0 && address<=7) || address==15) && (value>0 && value<=4095)){ SPI.transfer(0b00000011); // 4 bits of junk and a command to update DAC register delay(1); // small time delay to ensure the DAC processes the data int value1 = highByte(value); // collecting the upper half of the value int value2 = lowByte(value); // collecting the lower half of the value int addressAndValue1 = (address<<4) + value1; // combining the address and value into one piece SPI.transfer(addressAndValue1); // the 4 bits of address and the upper half of the value delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(value2); // the lower half of the value delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(0);//junk data delay(1); // small time delay to ensure the DAC processes the data } else{ SPI.transfer(0b00000011); // 4 bits of junk and a command to update DAC register delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(0b11110000); // all addresses and a value of zero delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(0); // a value of zero delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(0);//junk data delay(1); // small time delay to ensure the DAC processes the data for(int i=0; i<8; i++){ digitalWrite(43, HIGH); delay(25); digitalWrite(43, LOW); delay(25); } } } //end of sendAddressAndValue void setUpDAC(){ SPI.transfer(0b00001000);//4 bits of junk and a command to change reference voltage delay(3); // small time delay to ensure DAC processes the data SPI.transfer(0);//sending more values since it doesn't care about these (thanks to the Command) and we need 32 bits total delay(3); SPI.transfer(0);//same reason delay(3); SPI.transfer(0b00000001);//7 bits of ignored data and a final "1" to indicate to use the internal reference delay(3); digitalWrite(SS, HIGH); delay(1); digitalWrite(SS, LOW); //turning all the outputs off to begin with SPI.transfer(0b00000011); // 4 bits of junk and a command to update DAC register delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(0b11110000); // all addresses and a value of zero delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(0); // a value of zero delay(1); // small time delay to ensure the DAC processes the data SPI.transfer(0);//junk data delay(1); // small time delay to ensure the DAC processes the data }