Hello forum,
I am opening a new post about SPI communication with DAC since I am not able to solve the following problem since two weeks.
I am trying to communicate with a DAC MAX 532 from Teency 3.5 https://datasheets.maximintegrated.com/en/ds/MAX532.pdf
Here is my code:
The problem was originally posted here: https://forum.pjrc.com/threads/49799-DAC-on-Teency-3-5
But at that time I was thinking the signal was noisy due to some problems in the circuit or with the grounding.
Currently I have tryed to change the "delay" in the void loop and those are the signals (please see attached pictures) I get from the scope with different values of the delays. I can not understand why the signal does not mantain a shape of a sin wave but oscillate so much.
By increasing the delay the signal doesn't look noisy so I am quite sure the problem is not in the hardware connections but in the software.
What is there wrong in the SPI communication?
Is there a timing problem?
I would really appreciate if you could look into this matter.
Thank you for your help.
I am opening a new post about SPI communication with DAC since I am not able to solve the following problem since two weeks.
I am trying to communicate with a DAC MAX 532 from Teency 3.5 https://datasheets.maximintegrated.com/en/ds/MAX532.pdf
Here is my code:
Code:
#include <SPI.h>
int channel_A = 0;
int channel_B = 0;
float pi=3.14;
float freq=1.0f;
int cycles=0;
// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
void setup() {
Serial.begin(115200);
pinMode(slaveSelectPin, OUTPUT);
SPI.setMOSI(11);
//SPI.setMISO(12);
SPI.setSCK(13);
SPI.begin();
}
void loop() {
float t=millis()/1000.0f;
channel_A=(int) (2000.0f*sin(t*2*pi*freq)+2000.0f);
channel_B=(int) (2000.0f*sin(t*2*pi*freq+pi/2)+2000.0f);
SPI_write(channel_A, channel_B);
delay(1000); // THIS IS THE DELAY I MODIFIED
}
void SPI_write(int channel_A, int channel_B) {
byte byte_0 = channel_B >> 4;
byte byte_1 = ( channel_B << 4 ) | ( channel_A >> 8 );
byte byte_2 = channel_A & 255;
digitalWrite(slaveSelectPin, LOW);
delayMicroseconds(1);
SPI.transfer(byte_0);
SPI.transfer(byte_1);
SPI.transfer(byte_2);
delayMicroseconds(1);
digitalWrite(slaveSelectPin, HIGH);
delayMicroseconds(1);
// printByte(byte_0);
// Serial.print("; ");
// printByte(byte_1);
// Serial.print("; ");
// printByte(byte_2);
// Serial.println();
}
void printByte (byte val)
{
for (byte mask = 0x80; mask; mask >>= 1)
{
Serial.print (val & mask ? '1' : '0');
}
}
The problem was originally posted here: https://forum.pjrc.com/threads/49799-DAC-on-Teency-3-5
But at that time I was thinking the signal was noisy due to some problems in the circuit or with the grounding.
Currently I have tryed to change the "delay" in the void loop and those are the signals (please see attached pictures) I get from the scope with different values of the delays. I can not understand why the signal does not mantain a shape of a sin wave but oscillate so much.
By increasing the delay the signal doesn't look noisy so I am quite sure the problem is not in the hardware connections but in the software.
What is there wrong in the SPI communication?
Is there a timing problem?
I would really appreciate if you could look into this matter.
Thank you for your help.