Hi...!!!
I want to read a data from another board using SPI(MOSI,MISO,SCK and SS) wires.
First I want to read some data as an acknowledgement then I want to send some data.Sending data part code is working fine but reading is happening. Please help me.
Sending data Code:-
#include <SPI.h>// include the SPI library:
const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int mm =0;
void setup() {
pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
SPI.begin();// initialize SPI:
Serial.begin(4800);
}
void loop() {
int arry[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
digitalWrite(spidata,LOW);
Serial.println("If condition-1 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[0], BIN);
SPI.transfer(arry[0]); // send in the address and value via SPI:
delay(1500);
Serial.println("If condition-2 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[1], BIN);
SPI.transfer(arry[1]);
delay(1500);
Serial.println("If condition-3 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[2],BIN);
SPI.transfer(arry[2]);
delay(1500);
Serial.println("If condition-4 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[3], BIN);
SPI.transfer(arry[3]);
delay(1500);
Serial.println("If condition-5 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[4], BIN);
SPI.transfer(arry[4]);
delay(1500);
Serial.println("If condition-6 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[5], BIN);
SPI.transfer(arry[5]);
delay(1500);
Serial.println("If condition-7 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[6], BIN);
SPI.transfer(arry[6]);
delay(1500);
Serial.println("If condition-8 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[7], BIN);
SPI.transfer(arry[7]);
Serial.println("If condition-9 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[8], BIN);
SPI.transfer(arry[8]);
delay(1500);
Serial.println("If condition-10 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[9], BIN);
SPI.transfer(arry[9]);
delay(1500);
Serial.println("If condition-11 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[10], BIN);
SPI.transfer(arry[10]);
delay(1500);
Serial.println("If condition-12 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[11], BIN);
SPI.transfer(arry[11]);
delay(1500);
Serial.println("If condition-13 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[12], BIN);
SPI.transfer(arry[12]);
delay(1500);
Serial.println("If condition-14 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[13], BIN);
SPI.transfer(arry[13]);
delay(1500);
Serial.println("If condition-15 Matches then:");
Serial.println("I am writting on SPI");
Serial.println(arry[14], BIN);
SPI.transfer(arry[14]);
delay(1500);
Serial.println("ALL THE CONDITIONS are over now::");
Serial.println("\n");
Serial.println("\n");
digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
delay(1500);
}
Recieving data code:-
// Slave
#define SCK_PIN 13
#define MISO_PIN 12
#define MOSI_PIN 11
#define SS_PIN 10
void SlaveInit(void) {
// Set MISO output, all others input
pinMode(SCK_PIN, INPUT);
pinMode(MOSI_PIN, INPUT);
pinMode(MISO_PIN, OUTPUT);
pinMode(SS_PIN, INPUT);
// Enable SPI
SPCR = B00000000;
SPCR = (1<<SPE);
}
byte ReadByte(void) {
while(!(SPSR & (1<<SPIF))) ;
return SPDR;
}
void WriteByte(byte value) {
SPDR = value;
while (!(SPSR & (1<<SPIF))) ;
return;
}
unsigned long lastSent;
void setup() {
Serial.begin(57600);
digitalWrite(5, HIGH);
SlaveInit();
lastSent = millis();
pinMode(5, OUTPUT);
}
void loop() {
if (digitalRead(10) == LOW) {
Serial.println("Pin 10 low...");
byte rxData;
rxData = ReadByte();
Serial.print("Command: ");
Serial.println(rxData, DEC);
if (rxData == 17) {
Serial.println("Sending data to master...");
WriteByte(19);
Serial.println("Done Sending data...");
}
}
if (millis() > lastSent + 2000) {
Serial.println("Pin 5 low...");
digitalWrite(5, LOW);
delay(10);
digitalWrite(5, HIGH);
lastSent = millis();
}
}