MCP4921 DAC with Metro M4

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
brentmaxwell
 
Posts: 2
Joined: Thu Nov 17, 2022 12:39 pm

MCP4921 DAC with Metro M4

Post by brentmaxwell »

I have a Metro M4 board, and I'm using an AudioHacker shield (https://nootropicdesign.com/audio-hacker/) which contains an ADC (MCP3201), DAC (MCP4921), and two SRAM chips all accessible via SPI.

I have gotten the shield to work fine with an Arduino Uno, using the code supplied by the manufacturer of the shield, but I need more space for my program, The manufacturer only supports the Uno and bitbangs the SPI interface, so I decided to port it to the SAMD51 platform and use the SPI library on the Metro M4.

Using SPI, I am able to read data via the ADC with no problem, but writing to the DAC produces no output on my oscilloscope. Anyone see what I could be doing wrong?

Setup code:

Code: Select all

union SpiData {
  uint16_t value; /**< value */
  struct {
    uint8_t loByte; /**< low byte */
    uint8_t hiByte; /**< high byte */
  };
};

void setup(){
  //ACD_CS = 7 per manufacturer
  //DAC_CS = 8 per manufacturer
  pinMode(ADC_CS, OUTPUT);
  digitalWrite(ADC_CS, HIGH);
  pinMode(DAC_CS, OUTPUT);
  digitalWrite(DAC_CS, HIGH);
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.begin();
}
Reading the ADC

Code: Select all

int read(){
  SpiData adc;
  digitalWrite(ADC_CS, LOW);
  adc.hiByte = SPI.transfer(0x00) & 0x1F;
  adc.loByte = SPI.transfer(0x00);
  digitalWrite(ADC_CS, HIGH);
  return (adc.value >> 1);
 }
 
Writing to the DAC

Code: Select all

void write(int value) {
  // DAC_CONFIG = 0x30
  byte firstByte = DAC_CONFIG | ((value >> 8) & 0xF);
  byte secondByte = value;
  digitalWrite(DAC_CS, LOW);
  SPI.transfer(firstByte);
  SPI.transfer(secondByte);
  digitalWrite(DAC_CS, HIGH);
}

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP4921 DAC with Metro M4

Post by adafruit_support_mike »

We don't use the MCP4921 ourselves, so we don't have any special knowledge of how it works.

If you have code that works with it on the Uno, compare the SPI messages it sends to the ones your SAMD51 version uses. There has to be some difference between them, and you can find the difference mechanically.

User avatar
brentmaxwell
 
Posts: 2
Joined: Thu Nov 17, 2022 12:39 pm

Re: MCP4921 DAC with Metro M4

Post by brentmaxwell »

There's a volume control on the shield to control the output. I had it turned down. 🤦‍♂️

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP4921 DAC with Metro M4

Post by adafruit_support_mike »

That would do it. Glad to hear you got things to work!

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”