Monday, November 21, 2016

Multiple MIDI-Controlled 12-Bit DACs via Teensy / Arduino

Overview
The MCP4922 is a 12-bit, two channel, SPI-based DAC - and can be easily connect to a studio setup using a Teensy via MIDI!This is an easy way of adding a 0 - 5V or 0 - 3.3V 4096-step DAC to a MIDI sequencing environment. Using one Teensy, multiple MCP4922 chips can be easily controlled, and mapped via MIDI data.

The cost of these chips is relatively low - around AUD$3 per chip or $1.50 per DAC channel - and they don't require any additional components for basic implementation. 



Hardware
Hardware connection is very similar to the previous post on the MCP4922. To connect multiple MCP4922 to a Teensy microcontroller, the following connections should be made:

  • Teensy 5V or 3.3V (depending on model) to MCP4922 VDD (Pin 1) - all MCP4922 have this connection in common
  • Teensy SPI Chip Select pin (user-designated) to MCP4922 CS (Pin 3) - each individual MCP4922 chip has a corresponding pin on the Teensy microcontroller. In this example, I am using digital pin 4 for the CS pin of MCP4922 #1 and then digital pin 5 for the CS pin of MCP4922 #2. 
  • Teensy SPI Serial Clock pin (as defined in the SPI library) to MCP4922 SCK (Pin 4) - all MCP4922 have this connection in common
  • Teensy SPI Digital Out (as defined in the SPI library) to MCP4922 SDI (Pin 5) - all MCP4922 have this connection in common
  • Teensy 5V or 3.3V (depending on model) to MCP4922 V REF A (Pin 13) - all MCP4922 have this connection in common
  • Teensy ground to MCP4922 VSS (Pin 12) - all MCP4922 have this connection in common
  • Teensy 5V or 3.3V (depending on model)to MCP4922 V REF B (Pin 11) - all MCP4922 have this connection in common
  • Teensy 5V or 3.3V (depending on model) to MCP4922 SHDN - all MCP4922 have this connection in common
  • Teensy ground to MCP4922 LDAC - all MCP4922 have this connection in common
  • MCP4922 V OUT A (Pin 14) is the output for DAC A - all MCP4922 have this connection in common
  • MCP4922 V OUT B (Pin 10) is the output for DACB - all MCP4922 have this connection in common
Almost all connections are duplicated for each MCP4922 - with the exception of the CS (Chip Select) pin, which requires a unique digital pin on the Teensy for each MCP4922.

This way, only a particular MCP4922 is selected when data is written to the SPI bus.





Software
The SPI library can be used to easily write data to the DAC. In the example below,  a function is written that has a chip select pin value, a DAC channel value and a DAC output value between 0 - 4095. This function writes to the SPI bus and updates the DAC output.

This function is then connected to MIDI via MIDI control messages.

This particular sketch supports both high res MIDI and normal res MIDI. The controller number that is used determines the DAC channel number as well as the MCP4922 chip.

In this example, the mapping is as follows:
  • CC 0 paired with CC 32 will send a high resolution value to MCP4922 chip 1 DAC 0
  • CC 1 paired with CC 33 will send a high resolution value to MCP4922 chip 1 DAC 1
  • CC 2 paired with CC 34 will send a high resolution value to MCP4922 chip 2 DAC 0
  • CC 3 paired with CC 35 will send a high resolution value to MCP4922 chip 2 DAC 1
  • CC 64 will send a normal resolution value to MCP4922 chip 1 DAC 0
  • CC 65 will send a normal resolution value to MCP4922 chip 1 DAC1
  • CC 66 will send a normal resolution value to MCP4922 chip 2 DAC 0
  • CC 67 will send a normal resolution value to MCP4922 chip 2 DAC 1
The mapping is arbitrary, to some degree - so adjust as desired.

Download here: http://milkcrate.com.au/_other/downloads/arduino/MCP4922_DAC_MULTI_MIDI/





0 comments: