I am trying to get a simple sin wave to go through my arduino and output it with a DAC. (Just as a test) I have a MCP4922 DAC on a miniDAC board. The code we are using calls the CS, SCK, and SDI pins. I'm not sure if the LDAC pin is needed for the DAC to work, but i don't have it in the code as of now. Here is a part of my code, any comments would be appreciated!
// SPI interface setup
pinMode(9, OUTPUT); //sets up CS pin to pin 9. When CS pin is LOW, SPI signaled to write
// for SPI: pin 10 = SS (ignore)
pinMode(11, OUTPUT); // pin 11: MOSI (SDI)
pinMode(13, OUTPUT); // pin 13: SCK
// pin 12: MISO (ignore)
SPI.setBitOrder(MSBFIRST); //Reads most significant bit first
SPI.setClockDivider(SPI_CLOCK_DIV16); //Sets clock speed. Want lowest "div__" without fluctuation
SPI.setDataMode(SPI_MODE0); //Mode 0 (CPOL0 and CHPHA0)
SPI.begin(); // Begins SPI
}
void loop (){
}
//Create the message for DAC
//Initialize the message for right
int outputRupper;
int outputR=analogRead(A0);
//Create the message commands for the right side
int A=0; //Write to channel DAC-A
const int BUF=0; //V-refinput is unbuffered
const int nGA=1; //No output gain selected (1x)
const int nSHDN = 1; //Output power down control bit
//Put the commands into a 16 bit code
//msg1 and msg2 is needed because we need a 16 bit message, and only have 12 bits to work with
int HDR = (A<<3)||(BUF<<2)||(nGA<<1)||(nSHDN);
outputRupper=outputR>>8;
int msg1R = (HDR<<4)||outputRupper;
int msg2R = 0xFF&outputR;
digitalWrite(9, LOW); //CS pin to low - Begin SPI communication
SPI.transfer(msg1R);
SPI.transfer(msg2R); // Communicate the 16 bit message for right
delayMicroseconds(20); // delay for writing
digitalWrite(9, HIGH); //CS pin to high -- stop SPI communication
Here is the full code for testing the DAC, although the full project is more complex, and has a lot more code (just manipulating the signal digitally)... let me know if that's the one you are referring to
#include "SPI.h" // introduces SPI library
void setup()
{
pinMode(13, OUTPUT);
//Set up interrupts and timers
cli(); //Disable interrupts while setting registers
TCCR1A = 0; // Make sure it is zero
TCCR1B = (1 << WGM12); // Configure for CTC mode (Set it; don't OR stuff into it)
TCCR1B |= ((1 << CS10) | (1 << CS12)); // Prescaler @ 1024
TIMSK1 = (1 << OCIE1A); // Enable interrupt
OCR1A = 16; // compare value = 1kHz (16MHz AVR)
sei(); // Enable global interrupts
// SPI interface setup
pinMode(9, OUTPUT); //sets up CS pin to pin 9. When CS pin is LOW, SPI signaled to write
// for SPI: pin 10 = SS (ignore)
pinMode(11, OUTPUT); // pin 11: MOSI (SDI)
pinMode(13, OUTPUT); // pin 13: SCK
// pin 12: MISO (ignore)
SPI.setBitOrder(MSBFIRST); //Reads most significant bit first
SPI.setClockDivider(SPI_CLOCK_DIV16); //Sets clock speed. Want lowest "div__" without fluctuation
SPI.setDataMode(SPI_MODE0); //Mode 0 (CPOL0 and CHPHA0)
SPI.begin(); // Begins SPI
}
void loop (){
}
ISR(TIMER1_COMPA_vect) { //Timer commands
//Begin SPI interface writing
//int msg1R = 24; // initializes number for oscilliscope verfication
//int msg2R = 24; // initializes number for oscilliscope verfication
//int msg1L = 36; // initializes number for oscilliscope verfication
//int msg2L = 36; // initializes number for oscilliscope verfication
//Create the message for DAC
//Initialize the message for right
int outputRupper;
int outputR=analogRead(A0);
//Create the message commands for the right side
int A=0; //Write to channel DAC-A
const int BUF=0; //V-refinput is unbuffered
const int nGA=1; //No output gain selected (1x)
const int nSHDN = 1; //Output power down control bit
//Put the commands into a 16 bit code
//msg1 and msg2 is needed because we need a 16 bit message, and only have 12 bits to work with
int HDR = (A<<3)||(BUF<<2)||(nGA<<1)||(nSHDN);
outputRupper=outputR>>8;
int msg1R = (HDR<<4)||outputRupper;
int msg2R = 0xFF&outputR;
digitalWrite(9, LOW); //CS pin to low - Begin SPI communication
SPI.transfer(msg1R);
SPI.transfer(msg2R); // Communicate the 16 bit message for right
delayMicroseconds(20); // delay for writing
digitalWrite(9, HIGH); //CS pin to high -- stop SPI communication