SPI connection not working

Hi Guys,

Im trying to use a Mega to simply send another (non arduino) Board an SPI signal. However with my current programm does not seem to function.
The signal Im trying to send is: 00011001 11101011 10000100

Im now wondering if the problem is my arduino code not working or the other Hardware not receiving properly.
Is there something wrong with my code:

#include <SPI.h>


const int slave_select = 41;


// Example: Write to DAC register 2.4 V
byte front_byte= 0b00011001;
byte middle_byte= 0b11101011;
byte end_byte= 0b10000100;
byte first_answer;
byte second_answer;
byte third_answer;


void setup() {
  // put your setup code here, to run once:
 pinMode(slave_select, OUTPUT);



 digitalWrite(slave_select,HIGH);
SPI.begin();
Serial.begin(9600);



Serial.println("START OF PROGRAM");
SPI.beginTransaction(SPISettings(500, MSBFIRST, SPI_MODE0));
digitalWrite(slave_select, LOW);


SPI.transfer(front_byte);
SPI.transfer(middle_byte);
SPI.transfer(end_byte);
Serial.println("Bytes gesendet.");

SPI.transfer(0);         
//Antwort empfangen            
first_answer= SPI.transfer(0);
second_answer= SPI.transfer(0);
third_answer= SPI.transfer(0);
digitalWrite(slave_select, HIGH);

SPI.endTransaction();
Serial.print("empfangene Antwort: ");
Serial.print(first_answer);
Serial.print(" ");
Serial.print(second_answer);
Serial.print(" ");
Serial.println(third_answer);
}

void loop() {
  // put your main code here, to run repeatedly:

}

What is the other hardware? Perhaps an SPI DAC based on your code comments. Do you have a link to its datasheet that you can share?

What doesn't work as expected?

Your code clocks out your 3 bytes (front, middle & end), then sends out a further byte (set to zero), before reading back 3 response bytes.

Are you using the correct pins?
For a Mega you need to use:-
SPI: 50 (MISO), 51 (MOSI), 52 (SCK), 53 (SS).
If you would have included a schematic like you are supposed to we would know, but you didn't.

I see from your code you are using pin 41 as a slave select.

Yes exactly an SPI DAC. Heres the datasheet:

Perfect. This is what I try to acomplish.

Im trying to Output a certain Voltage over the DAC Board. For that i want to send a 24 bit information to the Input Shift register of the DAC (page 19 in the documentation).
Then (after SPI communication) in theorie I simply should take the LDAC Pin of the board low to output the voltage (I excluded this code for readability).
However if I run this code over it, it doesnt work.

Sorry I didnt know I had to include one.
I did use the correct pins apart from SS. However even after putting SS on 53 it doesnt work

I would try removing the 4 SPI transfers of zero and take SS high after your 3 bytes have been sent. Then toggle LDAC to load the value.

How have you wired the DAC board up?

Heres the documentation for the whole DAC Board:

The board is powered with 5 V VCC and 5V IOVCC. Over the circuitry on the board it outputs -10V for REFN to the chip and +10V for REFP.
On the Board there are pins for connecting SPI (called SDO, SDIN, SYNC, SCLK) and for the rest of the chips pins to control DAC functions (LDAC, CLR, RESET)

I connected:
SDO on DAC to MISO on arduino (50)
SDIN to MOSI (51)
SCLK to CLK (52)
SYNC to SS (53)
LDAC, CLR, RESET I put on random digital Outputs on arduino (47-49)

I just tried it and it also didnt work

Hi,
Have you Googled;

AD5791 arduino

I think there may even be a library.

Tom.. :smiley: :+1: :coffee: :australia:

You save the schematic drawing as a jpeg file on your desktop.

Then when writing your reply you drag this file into your answer.

I forgot! If it's an Analog Devices unit, then there is a good chance that Analog Devices have a library for it that is compatible with Arduino. They have a board called Linduino ONE that is compatible with an Arduino UNO. They have all sorts of libraries for their chips. Might be worth digging around the AD website and see if you can find a library for your device.

Okay Thanks guys. That is a good approach actually. I will try to look for something!

Connect LDAC/ to LOW; CLR/ and RESET/ to HIGH; use SYNC/ to latch data. Read Table 6 of data sheets.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.