/* SPI Test Code MOSI pin 51 SCLK pin 52 Load DACs pin 10 Scope Trigger pin 11 Loads all four DACs in 83.2 uS with 4MHz clock Loads all four DACs in 75.2 uS with 8MHz clock dac_load pulse is 6.8 uS Compares to ComputerVoltageSource with 16MHz AtomPro 0.5 mS execution time for PSIM ~6X faster 1 mS execution time for CVS4 (additional I2C addressing time) 2 mS execution time for CVS (8 channels with 2X I2C addressing time) */ #include // declare pins const int dac_load = 10; const int trigger = 11; // declare variables unsigned int out_j1 = 0; unsigned int out_j2 = 0; unsigned int out_j3 = 0; unsigned int out_j4 = 0; // setup runs once when you press reset void setup() { pinMode (dac_load, OUTPUT); pinMode (trigger, OUTPUT); SPI.begin(); SPI.setDataMode(SPI_MODE3); SPI.setBitOrder(MSBFIRST); // SPI.setClockDivider(SPI_CLOCK_DIV2); pinMode(dac_load,OUTPUT); digitalWrite(dac_load,HIGH); out_j1 = 245; out_j2 = 454; out_j3 = 155; out_j4 = 823; } // the loop routine runs over and over again forever: void loop() { digitalWrite(trigger,LOW); // Indicate start of cycle SPI.transfer(out_j1 >> 8 | 0xC0 ); // Output the MSB first with address 3 SPI.transfer(out_j1 & 0xFF); // Followed by the LSB digitalWrite(dac_load,LOW); digitalWrite(dac_load,HIGH); SPI.transfer(out_j2 >> 8 | 0x80 ); // Output the MSB first with address 2 SPI.transfer(out_j2 & 0xFF); // Followed by the LSB digitalWrite(dac_load,LOW); digitalWrite(dac_load,HIGH); SPI.transfer(out_j3 >> 8 | 0x40 ); // Output the MSB first with address 1 SPI.transfer(out_j3 & 0xFF); // Followed by the LSB digitalWrite(dac_load,LOW); digitalWrite(dac_load,HIGH); SPI.transfer(out_j4 >> 8); // Output the MSB first with address 0 SPI.transfer(out_j4 & 0xFF); // Followed by the LSB digitalWrite(dac_load,LOW); digitalWrite(dac_load,HIGH); digitalWrite(trigger,HIGH); // indicate end of cycle delay(1); }