SPI Interfacing two Arduino boards

HI... This Arduino world is wonderfull... Always GREAT help :) I have a problem with SPI! I would like to communicate between two Arduino Diecimila boards using the SPI standard. I have located the tutorial: "Interface with a serial EEPROM using SPI", so I know how to setup the master to send data... But HOW should I setup the other board to recieve this data... Abstract: I would like to send 200 (int) and recieve it and send it back devided by 2. How is this possible? And can I view it in the serial monitor? I hope for a small code example?

No one has admitted doing it with the Arduino yet. Take a look at this thread (same thing, but with I2C): http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1195968384

Here is an Atmel app note, complete with example code, for what you want: http://www.atmel.com/dyn/resources/prod_documents/doc2585.pdf . Note this isn’t exactly Arduino compatible, but it shouldn’t be impossible to port to Arduino.

-j

Okay... But NOW some one have done it! I Have manufactured a master and slave code!

Should I contact Arduino to post it on their site?

That's awesome. You should go ahead and post it on the Arduino playground wiki. You can sign up for an account here: http://www.arduino.cc/playground/Main/LoginForm

Wow, that was fast. :)

Yes, please post your code to the playground (or at the very least here, or in the "exhibition" section) so that others can benefit.

I have had this on my to-do list for a while, just haven't gotten around to it.

-j

This is the mastercode

#define DATAOUT 11 // MOSI - Master Output Slave Input
#define DATAIN 12 // MISO - Master Input Slave Output
#define SPICLOCK 13
#define SLAVESELECT 10
#define SW 6 // Sætter pin 6 til output

int val;
int tael=0;
int minmax=0;
int time1=0;
int time2=0;
int onoff=0;
int on=0;

char spi_transfer(volatile char data)
{
SPDR = data; // Start overførslen
while (!(SPSR & (1<<SPIF))) // Vent til overførslen er fuldført
{
};
return SPDR; // returnere de data der er blevet sendt, hvis de evt skulle bruges
}
void setup() // Sætter gøres klar til at lave SPI interface
{
byte clr;
pinMode(SW, INPUT); // Sætter pin 6 til input(Vores analoge input, i dette tilfælde vores trykknap)
pinMode(DATAOUT, OUTPUT); // Sætter pin 11 til output(Data out)
pinMode(DATAIN, INPUT); // Sætter pin 12 til input(Data in)
pinMode(SPICLOCK,OUTPUT); // Sætter 13 til output(clock)
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); // Sørger for at slaven er klar til at modtage data

SPCR = (0<<SPIE)|(1<<SPE)|(0<<DORD)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)
|(0<<SPR0); // Opsætter SPI control register(s.169)
clr=SPSR; // nulstiller SPSR
clr=SPDR; // nulstiller SPDR
delay(10); // Laver et delay på 10 ms, for at undgå fejl
}

byte SPI_write(int value) // Her sker processen med at sende data
{
digitalWrite(SLAVESELECT,LOW); // Gør klar til at sende
spi_transfer(value); // Sender data
digitalWrite(SLAVESELECT,HIGH); // Frigiv slaven
}

void loop() // Dette er selve funktionaliteten i systemet
{
delay(20); // Venter 20 ms
val = digitalRead(SW); // Læser på pin 6 og indsætter værdien i val
if(val == HIGH){
if(onoff<=10 && onoff!=0){
if(on == 0){
onoff=0;
tael=time1;
on=1;
}else{
onoff=0;
time1=tael;
tael=0;
on=0;
}
}else{
onoff=0;
}
}
if(val == LOW){
if(minmax == 0){
tael=tael++;
onoff=onoff++;
}
if(tael == 256 || minmax == 1){
tael=tael–;
minmax=1;
onoff=onoff++;
if(tael==0){
minmax=0;
}
}
}
SPI_write(tael); // Her bliver hastigheden af pulser sendt til slaven
}

This is the slave code

#define DATAIN 11//MOSI
#define DATAOUT 12//MISO - not used, but part of builtin SPI
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss

byte registerNr=0;
byte dataSend=0;
int ledPin = 2; // LED connected to digital pin 13
unsigned char spi_transfer()
{
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
unsigned char data;
data = SPDR; // Start the transmission
return data; // return the received byte
}

void setup()
{
byte i;
byte clr;

pinMode(ledPin, OUTPUT); // sets the digital pin as output

pinMode(DATAIN, INPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(SPICLOCK,INPUT);
pinMode(SLAVESELECT,INPUT);
//digitalWrite(SLAVESELECT,HIGH); //disable device
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 (fastest)
SPCR = (0<<SPIE)|(1<<SPE)|(0<<DORD)|(0<<MSTR)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(0<<SPR0);
clr=SPSR;
clr=SPDR;
delay(10);
/for (i=0;i<6;i++)
{
write_registerNr(i,255);
}
/
}

void loop()
{
//write_registerNr(registerNr,dataSend);
delay(10);
//dataSend=170;
if (spi_transfer()==170){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(500); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(500); // waits for a second
} else {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(50); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(50); // waits for a second
}

if (registerNr==6)
{
registerNr=0;
}
}