share Arduino to Arduino SPI_MISO code..

share Arduino to Arduino SPI_MISO code…

//==============MASTER==================
#include <SPI.h>
byte t=0; //Master->Slaver value
byte r=0; //Master<-Slaver value
void setup() {
SPI_MasterInit();
Serial.begin(9600);
}

void loop() {

digitalWrite(SS, LOW);
SPI_MasterTransmit(t);
r=SPI_MasterReceive();
digitalWrite(SS, HIGH);

Serial.print(“Slaver input = “); //print Master<-Slaver value
Serial.print(r);
Serial.print(”\n”);
delay(200);

}

void SPI_MasterInit(void)
{
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(SS, OUTPUT);

digitalWrite(SCK, LOW);
digitalWrite(MOSI, LOW);
digitalWrite(MISO, LOW);
digitalWrite(SS, HIGH);

SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);

setClockDivider(SPI_CLOCK_DIV32);

}

void SPI_MasterTransmit(byte cData)
{
/* Start transmission /
SPDR = cData;
/
Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
}

byte SPI_MasterReceive(void)
{
/* Wait for reception complete /
while(!(SPSR & (1<<SPIF)))
;
/
Return Data Register */
return SPDR;
}

void setClockDivider(byte rate)
{
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | (rate & SPI_2XCLOCK_MASK);
}

//====================END==================

//===============Slaver=======================

byte t=0; //Master<-Slaver value
byte r=0; //Master->Slaver value
void setup() {
SPI_SlaveInit();
}
void loop() {

t = SPI_SlaveReceive();
SPI_SlaveTransmit(r);
}

void SPI_SlaveInit(void)
{
pinMode(SCK, INPUT);
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT);

digitalWrite(SCK, LOW);
digitalWrite(MOSI, LOW);
digitalWrite(MISO, LOW);
digitalWrite(SS, HIGH);

SPCR |= _BV(SPE);
}

void SPI_SlaveTransmit(byte cData)
{
/* Start transmission /
SPDR = cData;
/
Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
}

char SPI_SlaveReceive(void)
{
/* Wait for reception complete /
while(!(SPSR & (1<<SPIF)))
;
/
Return Data Register */
return SPDR;
}

//====================END==================

SPI_Slaver_MISO.ino (1.51 KB)

SPI_Master_MISO.ino (1.16 KB)