Example for MCP4921 (or MCP4922) 12-bit DAC chips

Post your cool example code here.
Post Reply
User avatar
Vassilis
Posts: 336
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

Example for MCP4921 (or MCP4922) 12-bit DAC chips

Post by Vassilis » Sat Feb 03, 2018 3:33 pm

I wrote a simple example sketch for using the 12-bit Microchip MCP4921 (single DAC channel) or MCP4922 (dual DAC channels) DAC.
There is no need to use a library for that.

Code: Select all

/**
 *  Example for using the MCP4921 (single DAC channel) or MCP4922 (dual DAC channels) external DAC chip with 
 *  the STM32F103 micro-controller. 
 *  
 *  (c) 3 January 2018 by Vassilis Serasidis 
 *   Home: http://www.serasidis.gr
 */

#define DAC_CS    PB12
#define DAC_SCK   PB13
#define DAC_MOSI  PB15

#define DAC_FLAGS_A 0b00110000 //Select DAC channel A (MCP 4921 or (MCP 4922)
#define DAC_FLAGS_B 0b10110000 //Select DAC channel B (only on MCP 4922)

#define waveformLength 120


// Sine wave
static int sineWave[waveformLength] {
    0x7ff, 0x86a, 0x8d5, 0x93f, 0x9a9, 0xa11, 0xa78, 0xadd, 0xb40, 0xba1,
    0xbff, 0xc5a, 0xcb2, 0xd08, 0xd59, 0xda7, 0xdf1, 0xe36, 0xe77, 0xeb4,
    0xeec, 0xf1f, 0xf4d, 0xf77, 0xf9a, 0xfb9, 0xfd2, 0xfe5, 0xff3, 0xffc,
    0xfff, 0xffc, 0xff3, 0xfe5, 0xfd2, 0xfb9, 0xf9a, 0xf77, 0xf4d, 0xf1f,
    0xeec, 0xeb4, 0xe77, 0xe36, 0xdf1, 0xda7, 0xd59, 0xd08, 0xcb2, 0xc5a,
    0xbff, 0xba1, 0xb40, 0xadd, 0xa78, 0xa11, 0x9a9, 0x93f, 0x8d5, 0x86a,
    0x7ff, 0x794, 0x729, 0x6bf, 0x655, 0x5ed, 0x586, 0x521, 0x4be, 0x45d,
    0x3ff, 0x3a4, 0x34c, 0x2f6, 0x2a5, 0x257, 0x20d, 0x1c8, 0x187, 0x14a,
    0x112, 0x0df, 0x0b1, 0x087, 0x064, 0x045, 0x02c, 0x019, 0x00b, 0x002,
    0x000, 0x002, 0x00b, 0x019, 0x02c, 0x045, 0x064, 0x087, 0x0b1, 0x0df,
    0x112, 0x14a, 0x187, 0x1c8, 0x20d, 0x257, 0x2a5, 0x2f6, 0x34c, 0x3a4,
    0x3ff, 0x45d, 0x4be, 0x521, 0x586, 0x5ed, 0x655, 0x6bf, 0x729, 0x794
};

#include <SPI.h>

int i;

SPIClass DAC_SPI(2); // use SPI-2 port

void setup() {
  pinMode(DAC_CS, OUTPUT);
  digitalWrite(DAC_CS, HIGH);
  DAC_SPI.begin();
}

void loop() {
  for(i=0;i<waveformLength;i++){
    sendToDAC('A',sineWave[i]);
  }
}


void sendToDAC(char channel, int val){
  byte lVal = val;
  byte hVal = (val >> 8);
  digitalWrite(DAC_CS, LOW);
  if(channel == 'A'){ 
    DAC_SPI.transfer(hVal | DAC_FLAGS_A); //Send data to DAC output 'A' (MCP4921 pin 8 or MCP4922 pin 14)
  }else{
    DAC_SPI.transfer(hVal | DAC_FLAGS_B); //Send data to DAC output 'B' (MCP4922 pin 10)
  }
  DAC_SPI.transfer(lVal);
  digitalWrite(DAC_CS, HIGH);
}
mcp4921.jpg
mcp4921.jpg (51.2 KiB) Viewed 176 times

dannyf
Posts: 231
Joined: Wed May 11, 2016 4:29 pm

Re: Example for MCP4921 (or MCP4922) 12-bit DAC chips

Post by dannyf » Sun Feb 11, 2018 12:42 am

Teo suggestions.

1. See if you can put the code in a module so it is easier for others to incorporate it into their project.

2. Google dds. It offers more ability to fine tune the output.

User avatar
Vassilis
Posts: 336
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

Re: Example for MCP4921 (or MCP4922) 12-bit DAC chips

Post by Vassilis » Sun Feb 11, 2018 2:10 am

Thanks dannyf for your suggestions. Personally, I am not really interested in DDS. I wrote that sample code because @stm32user asked me how to use the MCP4921 with STM32F103 MCU.

Post Reply