// MCP4922 Demo code sinewave at 16 res top to bot.
// For comparison to MCP4725 operation (DAC_RESOLUTION=-5).
#include <SPI.h>



SPISettings settingsA(16000000, MSBFIRST, SPI_MODE0);  // At 16 = SPI Clock = 8MHz.

int RCLKPin  = 53;   // pin 12 on the 74hc595 latch - nSS
int SRCLKPin = 52;  // pin 11 on the 74hc595 shift register clock - SCK
int SERPin   = 51;    //  MOSI

//////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200);        // Start serial port (debug).

  pinMode(RCLKPin,  OUTPUT);   // Set SPI control PINs to output.
  pinMode(SRCLKPin, OUTPUT);
  pinMode(SERPin,   OUTPUT);

  SPI.begin();

  Serial.println("MCP4922 SPI Dual DAC SPI hardware mode");
  noInterrupts();
}

//////////////////////////////////////////////////////////////////////////////
// 0 - A, 1 - B
//
void writeMCP4922_AB(byte AB, uint16_t v) {

    v |=0xf000;             // B15(A/B)=1 B, B14(BUF)=1 on, B13(GAn) 1=x1  B12(SHDNn) 1=off
    if (!AB)  v &= ~0x8000; // When zero clear B15 for A.

    SPI.beginTransaction(settingsA);
    digitalWrite(RCLKPin, LOW);
    SPI.transfer( (0xff00 & v)>>8 );
    SPI.transfer(      0x00ff & v );
    digitalWrite(RCLKPin, HIGH);
    SPI.endTransaction;
}

void loop() {
  writeMCP4922_AB( 0, 4092 );
  writeMCP4922_AB( 1, 3000 );

}
