DAC
The following general DAC functions are available to set the Analog Output voltage. They take a DAC channel as an argument along with the DAC value.
Additionally, these channel-specific functions are available for each of the 4 DAC channels as well. The "X" in the function name must be replaced by the channel number (1,2,3 or 4). While the functionality is the same as the above functions, these run a bit faster as they do not have to calculate the channel to update. If you need to save a few clock cycles, use these functions. Otherwise (or if you are unsure) stick with writeDAC and writeDACRAW.
writeDAC
void writeDAC(int channel, float voltage);
void writeDAC(int channel, double voltage);
This function writes a voltage to the DAC channel channel
. It takes either a float or a double as an input. (Technically, writeDAC is overcast with two functions, one that takes a double and one that takes a float as an argument, but this detail can be ignored.) The function takes the following arguments:
channel
DAC channel to update. Valid values are 1, 2, 3 or 4.voltage
The voltage to output on the DAC channelchannel
. Values above +10V or below -10V will be coerced to +10V or -10V respectively. Type float or double is accepted.
Example
IntervalTimer Timer; // instantiate a timer for setting the DAC update rate
double voltage_pattern[] = {1.25,1.3,1.4534,2.453,2.6534,3,4,-2,-3.423} ; // array of voltages
void setup() {
Timer.begin(ddsUpdate, 10); // update every 10 microseconds
}
void ddsUpdate() {
static unsigned int position = 0; //store position in array
writeDAC(1,voltage_pattern[position]); // set DAC channel 1 to next value array
writeDAC(2,-voltage_pattern[position]); // set DAC channel 2 to negative of next value in array
position++; // Increment to next value in array
if ( position >= ( sizeof(voltage_pattern)/sizeof(voltage_pattern[0]) ) ) {
//reset positition when position equal to number of elements in voltage_pattern
position = 0;
}
}
writeDACRAW
void writeDACRAW(int channel, int16_t data);
This function writes a signed 16-bit integer to the DAC on channel channel
. The function takes the following arguments:
channel
DAC channel to update. Valid values are 1, 2, 3 or 4.data
A signed 16-bit integer that is the raw data sent to the DAC on channelchannel
. The voltage on the DAC is equal to the integer value times 10.24 / 32768 = 3.125e-4. An integer value of 16,384 will output +5.12V. And value of -16,384 will output -5.12V. Values above +10V or below -10V will be coerced to +10V or -10V respectively.
Example
IntervalTimer Timer; // instantiate a timer for setting the DAC update rate
int16_t voltage_pattern[] = {-23432,-10342,-5432,-43,543,1532,15342}; // Raw DAC values
void setup() {
Timer.begin(ddsUpdate, 10); // update every 10 microseconds
}
void ddsUpdate() {
static unsigned int position = 0; //store position in array
writeDACRAW(1,voltage_pattern[position]); // set DAC channel 1 to next value array
writeDACRAW(2,-voltage_pattern[position]); // set DAC channel 2 to negative of next value in array
position++; // Increment to next value in array
if ( position >= ( sizeof(voltage_pattern)/sizeof(voltage_pattern[0]) ) ) {
//reset positition when position equal to number of elements in voltage_pattern
position = 0;
}
}
writeDACX
void writeDACX(double voltage);
void writeDACX(float voltage);
This function is identical to writeDAC, except it does not take a channel argument, as the channel is set by the function called. It takes either a float or a double as an input. (Technically, writeDACX is overcast with two functions, one that takes a double and one that takes a float as an argument, but this detail can be ignored.)
The function takes the following argument:
voltage
The voltage to output on the DAC channel X. Values above +10V or below -10V will be coerced to +10V or -10V respectively.
Example
writeDAC2(-4.562); //Write -4.562 volts to DAC channel 2
writeDAC4(3.14); //Write 3.14 volts to DAC channel 4
writeDACXRAW
void writeDACRAWX(int16_t data);
This function writes a signed 16-bit integer to the DAC on channel X.
The function takes the following argument:
data
a signed 16-bit integer that is the raw data sent to the DAC. The voltage on the DAC is equal to the integer value times 10.24 / 32768 = 3.125e-4. An integer value of 16,384 will output +5.12V. And value of -16,384 will output -5.12V.
Example
int16_t data = 16384;
writeDACRAW(2,data); //Write 16,384 or 5.12V volts to DAC channel 2
data -= 24576;
writeDACRAW(4,data); //Write -8192 or -2.56 volts to DAC channel 4
zeroDACs
void zeroDACs(void);
This function sets all four DAC's output to 0V. The function takes no arguments.
Example
float output = 0;
if (running) {
output += 0.25;
writeDAC(1,output);
writeDAC(2,-output);
} else {
zeroDACs();
}