MIDI-to-CV Converter Programming Question

You have to send 3 bytes (24 bits). The first byte consists of the 4 bits for the operation (0011 = write & update), the next 4 bits consists of the channel (0000 or 0001), The next byte are the upper 8 bits of the value you want to write. The last byte are the 4 remaining bits of the value and then 4 0's

Something like this:

// MCP4822 Version
void setVoltage_MCP4822(int dacpin, bool channel, bool gain, unsigned int mV)
{
  unsigned int command = channel ? 0x9000 : 0x1000;

  command |= gain ? 0x0000 : 0x2000;
  command |= (mV & 0x0FFF);

  SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
  digitalWrite(dacpin, LOW);
  SPI.transfer(command >> 8);
  SPI.transfer(command & 0xFF);
  digitalWrite(dacpin, HIGH);
  SPI.endTransaction();
}

// LTC2632 Version
// Note:  There is no "gain" for this chip so that parameter is ignored
//        It could also be removed
void setVoltage_LTC2632(int dacpin, bool channel, bool gain, unsigned int mV)
{
  // 0b0011 == 0x03 == Write and Update DAC
  uint8_t command = channel ? 0x31 : 0x30;  // write and update shifted 4 bits + DAC channel

  uint16_t data = (mV << 4) & 0xFFF0;   // shift value 4 bits, lower 4 bits 0

  SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
  digitalWrite(dacpin, LOW);
  SPI.transfer(command);
  SPI.transfer(data >> 8);
  SPI.transfer(data & 0xFF);
  digitalWrite(dacpin, HIGH);
  SPI.endTransaction();
}