OldNo7
0
- Joined
- Nov 13, 2012
- Messages
- 79
- Points
- 8
It looks like this implementation has the LDAC pin tied to ground therefore it'll write the value as soon as it's recieved. What you want to do it tie the LDAC pin to one of the arduino pins (probably should have a pull up resistor) write the x value, write the y value then set LDAC low. If you have more than 1 DAC (analog modulation) you could tie both LDAC pins to the same arduino pin.
Code:
/*
MCP4922 test
outputs steadily rising voltages through both DACs
Steve Woodward, 2010
most code borrowed from
http://mrbook.org/blog/2008/11/22/controlling-a-gakken-sx-150-synth-with-arduino/
connections
====================================================
+5v > 4922 pin 1
Ard pin 10 > 4922 pin 3 (SS - slave select)
Ard pin 13 > 4922 pin 4 (SCK - clock)
Ard pin 11 > 4922 pin 5 (MOSI - data out)
Ard pin 9 > 4922 pin 8 (LDAC)
+5v > 4922 pin 11 (voltage ref DAC B)
Ground > 4922 pin 12
+5v > 4922 pin 13 (voltage ref DAC A)
4922 pin 14 DAC A > 1k resistor > synth CV in
*/
// MCP4922 DAC out
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK 13//sck
#define SLAVESELECT0 10//ss
#define LDAC 09
int i = 0;
void setup() {
SPI_setup();
Serial.begin(9600);
}
void loop() {
i++;
Serial.println(i);
write_note(i);
if(i>=4096) {
i=0;
}
}
void write_note(int i) {
write_valueY(i);
write_valueX(i);
digitalWrite(LDAC,LOW); // move values from buffer to output
int i=100; // LDAC needs to be held low for 20ns. Using a
while(i--); // scope and adjust the value of i
digitalWrite(LDAC,HIGH);
}
// **************************************************
// SPI for DAC
void SPI_setup(){
byte clr;
pinMode(DATAOUT, OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT0,OUTPUT);
pinMode(LDAC,OUTPUT);
digitalWrite(SLAVESELECT0,HIGH); //disable device
digitalWrite(LDAC,HIGH); //disable device
SPCR = (1<<SPE)|(1<<MSTR) | (0<<SPR1) | (0<<SPR0);
clr=SPSR;
clr=SPDR;
delay(10);
}
// write out through DAC A
void write_valueX(int sample)
{
// splits int sample in to two bytes
byte dacSPI0 = 0;
byte dacSPI1 = 0;
dacSPI0 = (sample >> 8) & 0x00FF; //byte0 = takes bit 15 - 12
dacSPI0 |= (1 << 7); // A/B: DACa or DACb - Forces 7th bit of x to be 1. all other bits left alone.
dacSPI0 |= 0x10;
dacSPI1 = sample & 0x00FF; //byte1 = takes bit 11 - 0
dacSPI0 |= (1<<5); // set gain of 1
digitalWrite(SLAVESELECT0,LOW);
SPDR = dacSPI0; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
SPDR = dacSPI1;
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
digitalWrite(SLAVESELECT0,HIGH);
//delay(2);
}
// write out through DAC B
void write_valueY(int sample)
{
// splits int sample in to two bytes
byte dacSPI0 = 0;
byte dacSPI1 = 0;
dacSPI0 = (sample >> 8) & 0x00FF; //byte0 = takes bit 15 - 12
dacSPI0 |= 0x10;
dacSPI1 = sample & 0x00FF; //byte1 = takes bit 11 - 0
dacSPI0 |= (1<<5); // set gain of 1
digitalWrite(SLAVESELECT0,LOW);
SPDR = dacSPI0; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
SPDR = dacSPI1;
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
digitalWrite(SLAVESELECT0,HIGH);
//delay(2);
}
Last edited: