MCP4921 DAC, sine wave and variable frequency..?

Hello from Peckham in London UK. :slight_smile:
Aside from the Android controlled arduino synth I am working on I am also looking into ways of generating waveforms for synthesis using our friend. :0)
I am currently playing around with the MCP4921 12 bit DAC and have got a sine wave on my oscillloscope from this code:

//
// Example for the MCP49x1 *single* DACs
// For the dual MCP49x2 series, see the other bundled example sketch.
//

#include <SPI.h>         // Remember this line!
#include <DAC_MCP49xx.h>
#define STEPS 256	
// The Arduino pin used for the slave select / chip select
#define SS_PIN 10
byte wavetable[STEPS] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2,
2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 17,
18, 19, 20, 22, 23, 24, 26, 27, 29, 30, 32, 34, 36, 37, 39, 41
, 43, 45, 47, 49, 51, 54, 56, 58, 61, 63, 66, 68, 71, 74, 76, 79,
82, 85, 88, 91, 94, 97, 100, 103, 107, 110, 113, 116, 120, 123,
127, 130, 134, 137, 141, 144, 148, 152, 155, 159, 162, 166, 170, 
173, 177, 180, 184, 187, 191, 194, 197, 201, 204, 207, 210, 213,
216, 219, 222, 225, 228, 230, 233, 235, 237, 239, 241, 243, 245,
246, 248, 249, 251, 252, 253, 253, 254, 255, 255, 255, 255, 255,
255, 255, 254, 253, 253, 252, 251, 249, 248, 246, 245, 243, 241,
239, 237, 235, 233, 230, 228, 225, 222, 219, 216, 213, 210, 207,
204, 201, 197, 194, 191, 187, 184, 180, 177, 173, 170, 166, 162,
159, 155, 152, 148, 144, 141, 137, 134, 130, 127, 123, 120, 116,
113, 110, 107, 103, 100, 97, 94, 91, 88, 85, 82, 79, 76, 74, 71,
68, 66, 63, 61, 58, 56, 54, 51, 49, 47, 45, 43, 41, 39, 37, 36, 34,
32, 30, 29, 27, 26, 24, 23, 22, 20, 19, 18, 17, 15, 14, 13, 12, 11,
10, 9, 9, 8, 7, 6, 6, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0};

volatile int indx=0;
volatile int mi=1;
// Set up the DAC. 
// First argument: DAC model (MCP4901, MCP4911, MCP4921)
// Second argument: SS pin (10 is preferred)
// (The third argument, the LDAC pin, can be left out if not used)
DAC_MCP49xx dac(DAC_MCP49xx::MCP4921, SS_PIN);

void setup() {
  // Set the SPI frequency to 1 MHz (on 16 MHz Arduinos), to be safe.
  // DIV2 = 8 MHz works for me, though, even on a breadboard.
  // This is not strictly required, as there is a default setting.
  dac.setSPIDivider(SPI_CLOCK_DIV8         );
  
  // Use "port writes", see the manual page. In short, if you use pin 10 for
  // SS (and pin 7 for LDAC, if used), this is much faster.
  // Also not strictly required (no setup() code is needed at all).
  dac.setPortWrite(true);
}

// Output something slow enough that a multimeter can pick it up.
// For MCP4901, use values below (but including) 255.
// For MCP4911, use values below (but including) 1023.
// For MCP4921, use values below (but including) 4095.
void loop() {
 
 
 indx++;

  dac.output(wavetable[indx]);
 
 
  if (indx>=STEPS)
 indx=0;

 

  
}

Could anyone help me with how to change the frequency of the wave using either sensor or variable changes?

1 Like

Google "phase accumulator"

Look in to PLL's and VCO as the source of the sine wave. You can get a PLL for about 40p +VAT.

Mark

What frequency range do you want to generate?

What frequency range do you want to generate?

About 50Hz-12KHz...

Google "phase accumulator"

I'm looking at a tutorial for the DUE here:

Some good stuff on the role of the phaseAccumulator. I'll see what I can glean from it..
Speaking of the DUE... Has anyone seen and/or ordered the DigiX board from Digistump?? :astonished:

$59... I have!! XD

If I send instead of the variable indx one PWM it should work?