Arduino due SPI communication problem

Hello ,

I am shruthi ,i am very new to arduino . i am working on SPI communication between two arduino due's. I am not getting any positive result . i am reading only 255 as my Spi slave output.

Please some one help me out.

Regards, Shruthi

code ? schematic ?

Hi ,
I am connecting two arduino due’s

Due Master Due Slave
SS(10) SS(10)
SCK SCK
MOSI MOSI
MISO MISO
GND GND

SCK,MISO and MOSI pins from SPI header of arduino due board .

Slave Code:
#include <SPI.h>
#include “pins_arduino.h”

void setup (void)
{
Serial.begin(9600);
pinMode(SS,INPUT);
SPI.begin ();

Serial.println(“SPI slave initialed”);

}

void loop (void)
{

char c;
byte in,out;
// enable Slave Select
digitalWrite(SS, LOW); // SS is pin 10
delay(1000);

// send test string

out= SPI.transfer (0x00);
// disable Slave Select
digitalWrite(SS, HIGH);
Serial.print("TDRE value: ");
Serial.println(SPI_SR_TDRE);
Serial.print("TDR value: ");
Serial.println(REG_SPI0_TDR);
Serial.print("RDRF value: ");
Serial.println( SPI_SR_RDRF);
Serial.print("RDR value: ");
Serial.println(REG_SPI0_RDR);
Serial.print("Received data : ");
Serial.print(out);
delay (1000); // 1 seconds delay

} // end of loop[/table]

Master:

#include <SPI.h>
#include “pins_arduino.h”

void setup (void)
{
Serial.begin(9600);
pinMode(SS,OUTPUT);
digitalWrite(SS,HIGH);
SPI.begin ();

Serial.println(“SPI slave initialed”);

}

void loop (void)
{

char c=‘a’;
digitalWrite(SS, LOW); // SS is pin 10
SPI.transfer (c);
digitalWrite(SS, HIGH);

} // end of loop[/table]

if i try to use “SPI.begin(SS)” and “SPI.transfer(SS,data)”
Program wont execute.

Please do help me. guide me how to solve this issue.

Thanks & Regards,
Shruthi

Do you know what MOSI stands for ?

MOSI stands for Master out Slave in.Data from master transfer to slave through this pin.

I have attached connection diagram which i got from Google.

Capture.PNG

Post the Master code copied from the Master DUE IDE window and pasted in the forum window using the “#” CODE TAGS toolbutton.

Post the SLAVE code copied from the SLAVE DUE IDE window and pasted in the forum window using the “#” CODE TAGS toolbutton.

When you are finished there should be TWO SEPARATE WINDOWS , NOT ONE LONG BUNCH OF CODE.

Master:

#include <SPI.h>
#include "pins_arduino.h" 

void setup (void)
{
  Serial.begin(9600);
  pinMode(SS,OUTPUT);
digitalWrite(SS,HIGH);
   SPI.begin ();
  
   Serial.println("SPI MAster initialed");

} 


void loop (void)
{

  char c='a';
  digitalWrite(SS, LOW);    // SS is pin 10
   SPI.transfer (c);
  digitalWrite(SS, HIGH);
 
}

Slave:

#include <SPI.h>
#include "pins_arduino.h" 

void setup (void)
{
  Serial.begin(9600);
  pinMode(SS,INPUT);
   SPI.begin ();
  
   Serial.println("SPI slave initialed");

} 


void loop (void)
{

  char c;
  byte in,out;

  digitalWrite(SS, LOW);    // SS is pin 10
  delay(1000);
  out= SPI.transfer (0x00);
 
  digitalWrite(SS, HIGH);
Serial.print("TDRE value: ");
  Serial.println(SPI_SR_TDRE);
  Serial.print("TDR value: ");
  Serial.println(REG_SPI0_TDR);
  Serial.print("RDRF value: ");
 Serial.println( SPI_SR_RDRF);
 Serial.print("RDR value: ");
  Serial.println(REG_SPI0_RDR);
  Serial.print("Received data : ");
  Serial.print(out);
  delay (1000);  
}  // end of loop

I'll try it with a couple of UNOs when I get home tonight. Were did you get the code ? I didn't see any link in your original post.

The Arduino SPI library does not support slave mode. You will need to consult the datasheet for your microcontroller and program the hardware directly.

In the slave sketch, If SS is an INPUT, why are you writing to it?

That's clearly an error. I can see how you would think it should be an INPUT on the slave unit , and for that reason I could see why the MASTER would write to the SLAVE SS , but there is no explanation for why the slave unit would try to write to an SS pin so that is clearly and error. The issue of the SPI library not supporting SLAVE mode sort of makes it a moot point and puts and end to this project as it stands but the digitalWrites in the SLAVE code clearly don't belong there.

[u]SLAVE CODE[/u]

void setup (void)
{
  Serial.begin(9600);
  pinMode(SS,INPUT);
   SPI.begin ();
  
   Serial.println("SPI slave initialed");

} 


void loop (void)
{

  char c;
  byte in,out;

  digitalWrite(SS, LOW);    // SS is pin 10
  delay(1000);
  out= SPI.transfer (0x00);

  digitalWrite(SS, HIGH);

In arduino Spi explanation they have given before write or read data we shouls make SS pin low then spi.transfer and again ss pin high.I know SPi library support only works in Master Mode . if we want to make that as slave means we should make SS pin as INPUT.So I write code like this.

if we want to make that as slave means we should make SS pin as INPUT.So I write code like this.

There is no such thing as a transvestite uC. It is either a MASTER or a SLAVE and SLAVES are not allowed to talk to each other. Only a Master can speak and a SLAVE can only speak when spoken to. Writing to you own chip select does not make you a slave. So in other words, you need to find another protocol to use, like I2C.

I used I2c, in that protocol i can transmit data between two micro controllers. But specifically i need Spi communication. If u have worked on it.Give me schematic and code. i will try out with my board.

Give me schematic and code

Excuse me ? You're not even going to say "Please" ?

I didn't try it because the other post said that SPI doesn't support SLAVE mode.

Note about Slave Select (SS) pin on AVR based boards

All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master.

[u]Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative.[/u]

It is, however, possible to use any pin as the Slave Select (SS) for the devices. For example, the Arduino Ethernet shield uses pin 4 to control the SPI connection to the on-board SD card, and pin 10 to control the connection to the Ethernet controller.

http://arduino.cc/en/Reference/SPI

READ THE ABOVE

Read this...

http://www.gammon.com.au/forum/?id=10892

It's written for the UNO so you can't use it directly, but it should get you started.

[quote ] SPI is used to send serial data from a microprocessor to another one, or a peripheral, [/quote]

I haven't had a chance to read it yet but is it a way to do it WITHOUT using the Library with it's No-Slave support limitation ?

raschemmel: [quote ] SPI is used to send serial data from a microprocessor to another one, or a peripheral,

I haven't had a chance to read it yet but is it a way to do it WITHOUT using the Library with it's No-Slave support limitation ? [/quote]

Yes, it is a great article. Also describes how to use the USART to do SPI.

I'll try to read it tonight.