/* Sample program to generate waveforms using DAC on Wytec EduBase board * The Wytec EduBase board has an MCP4922 two channel DAC on SPI. * A sawtooth waveform is generated on channel 0 and a sine wave channel 1. */ #include /* Tiva TM4C123 */ //const int DAC = PD_6; const int DAC = 33; void setup() { // configure DAC slave select pin pinMode (DAC, OUTPUT); // initialize SPI SPI.begin(); } void loop() { int i = 0; float f; while(1) { // create a sawtooth waveform on channel 0 DACwrite(i++, 0); // create a sine wave on channel 1 f = sinf(0.00314159 * i) * 0x07FF + 0x800; DACwrite((int)f, 1); } } // write to MCP4922 DAC through SPI void DACwrite(int value, int channel) { const int BUFFERED = 0x4000; const int UNITYGAIN = 0x2000; const int NOTSHUTDOWN = 0x1000; digitalWrite(DAC, LOW); // assert DAC slave select // DAC commands are 16 bit long value = (value & 0x0FFF) | (channel ? 0x8000 : 0) | BUFFERED | UNITYGAIN | NOTSHUTDOWN; SPI.transfer(value >> 8); // send high byte SPI.transfer(value & 0xFF); // send low byte digitalWrite(DAC, HIGH); // deassert slave select }