Created Sun, 13 Jan 2013 23:23:29 +0000 by slink3r
Sun, 13 Jan 2013 23:23:29 +0000
I used an example for SPI from the mpide library, and stole some code to implement a simple triangle wave. When I scope my SCLK I don't get a signal, was wondering where I went wrong.
// 3 - LDAC
// 6 - CS
// 11 - SDI
// 13 - SCLK
// inslude the SPI library:
#include <SPI.h>
class MCPDAC {
public:
void DACSelect(int i);
void GainSelect(int i);
void OutputModeSelect(int i);
void setValue(int i);
void outputDAC();
private:
int value;
byte high;
byte low;
boolean DAC_A;
boolean DAC_B;
boolean GainMode1;
boolean GainMode2;
boolean Outputpowermode;
int DAC_Data;
};
void MCPDAC::DACSelect(int i) {
if(i == 0) {
DAC_A = 1;
DAC_B = 0;
} else {
DAC_B = 1;
DAC_A = 0;
}
}
void MCPDAC::GainSelect(int i) {
if(i == 0) {
GainMode1 = 1;
GainMode2 = 0;
} else {
GainMode2 = 1;
GainMode1 = 0;
}
}
void MCPDAC::OutputModeSelect(int i) {
if(i == 0) {
Outputpowermode = 0;
} else {
Outputpowermode = 1;
}
}
void MCPDAC::setValue(int i) {
value = i;
}
void MCPDAC::outputDAC() {
high = 0x00000000;
low = 0x00000000;
if(DAC_B) {
high = high + 0x10000000;
}
if(GainMode2) {
high = high + 0x00100000;
}
if(Outputpowermode) {
high = high + 0x00010000;
}
if(value/2048 >= 1) {
high = high + 0x00001000;
value = value-2048;
}
if(value/1024 >= 1) {
high = high + 0x00000100;
value = value-1024;
}
if(value/512 >= 1) {
high = high + 0x00000010;
value = value-512;
}
if(value/256 >= 1) {
high = high + 0x00000001;
value = value-256;
}
low = value;
// CS high
Serial.println(high);
Serial.println(low);
digitalWrite(6,HIGH);
// LDAC high
digitalWrite(3,HIGH);
delay(1);
// CS low
digitalWrite(6,LOW);
SPI.transfer(high);
SPI.transfer(low);
digitalWrite(6,HIGH);
delay(1);
digitalWrite(3,LOW);
delay(1);
digitalWrite(3,HIGH);
delay(1);
digitalWrite(6,LOW);
delay(1);
}
void setup() {
// CS
pinMode(6,OUTPUT);
// LDAC
pinMode(3,OUTPUT);
Serial.begin(9600);
SPI.begin();
}
void loop() {
MCPDAC myDAC;
myDAC.DACSelect(0);
myDAC.GainSelect(0);
myDAC.OutputModeSelect(0);
myDAC.setValue(4095);
myDAC.outputDAC();
myDAC.setValue(2047);
myDAC.outputDAC();
myDAC.setValue(0);
myDAC.outputDAC();
}
Sat, 26 Jan 2013 16:59:01 +0000
Grasping at straws here but I don't see you setting the pin mode of the clock here, but I don't think you should need too. Also, what about jumper config on your board? SPI pins can be re detected.
Jacob
Sat, 26 Jan 2013 21:26:33 +0000
That seems like an incredibly long winded way of setting the values...
Why not just use an "unsigned short" to store the 16 bit value to output, then output the two bytes of it using masking and bitshifting?
I wrote a library for the MCP DAC for the Arduino - you might want to take it and adapt it to your own ends.
[url]http://www.hacking.majenko.co.uk/MCPDAC[/url]