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:
}
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.
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.
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 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.