Hi....!!!
Master is reading wrongly from Slave, what could be the wrong here. In Slave if I use only one if() condition then it works fine otherwise this problem happens.
Master code:
#include <SPI.h>// include the SPI library:
#define SCK_PIN 13
#define MISO_PIN 12
#define MOSI_PIN 11
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:
void setup() {
pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
SPI.begin();// initialize SPI:
Serial.begin(9600);
SPI.setClockDivider(SPI_CLOCK_DIV64) ;
}
void loop() {
int data[10];
digitalWrite(spidata,LOW);
SPI.transfer(0x04); // send in the address and value via SPI:
delay(700);
digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
delay(200);
data[0] = SPDR;
Serial.println("Data received for 0x04");
Serial.println(data[0], HEX);
digitalWrite(spidata,LOW);
SPI.transfer(0x06); // send in the address and value via SPI:
delay(700);
digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
delay(200);
data[1] = SPDR;
Serial.println("Data received for 0x05");
Serial.println(data[1], HEX);
digitalWrite(spidata,LOW);
SPI.transfer(0x08); // send in the address and value via SPI:
delay(700);
digitalWrite(spidata,HIGH);// take the SS pin high to de-select the chip:
delay(200);
data[2] = SPDR;
Serial.println("Data received for 0x05");
Serial.println(data[2], HEX);}
Slave Code:
#include <SPI.h>
#define SCK_PIN 13
#define MISO_PIN 12
#define MOSI_PIN 11
#define SS_PIN 10
unsigned char data;
int array[15] = {0x0C,0x0c,0x0c, 0x0B,0x0B,0x0A,0x0D,0x0D,0x1D, 0xFF, 0xFF,0x1F, 0x07, 0x14, 0x09 }, ii;
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 = B00101100;
SPCR = (1<<SPE);
}
void setup(){ Serial.begin(9600);
SPI.begin();
}
void loop()
{
unsigned char hg = 0;
Serial.println("Data Received from Master Board");
SlaveInit();
data = SPI_SlaveReceive();
Serial.println(data, HEX);
// Serial.println(data, BIN);
hg = data;
data = 0;
delay(1300);
if(hg == 0x04)
{
SPDR = 0;
Serial.println("Writting Data on SPI bus");
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, LOW);
delay(700);
for(ii=0;ii<3;ii++){
SPI.transfer(array[ii]);
Serial.println(array[ii], HEX);
delay(300);
}
digitalWrite(SS_PIN, HIGH);
Serial.println("Sending Done");
}
if(hg == 0x06)
{
SPDR = 0;
Serial.println("Writting Data on SPI bus");
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, LOW);
delay(700);
for(ii=2;ii<5;ii++){
SPI.transfer(array[ii]);
Serial.println(array[ii], HEX);
delay(300);
}
digitalWrite(SS_PIN, HIGH);
Serial.println("Sending Done");
}
if(hg == 0x08)
{
SPDR = 0;
Serial.println("Writting Data on SPI bus");
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, LOW);
delay(700);
for(ii=5;ii<8;ii++){
SPI.transfer(array[ii]);
Serial.println(array[ii], HEX);
delay(300);
}
digitalWrite(SS_PIN, HIGH);
Serial.println("Sending Done");
}
}
unsigned char SPI_SlaveReceive()
{
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
Output for master:
Data received for 0x04
8
Data received for 0x05
4
Data received for 0x05
0
Data received for 0x04
C
Data received for 0x05
C
Data received for 0x05
6
Data received for 0x04
8
Data received for 0x05
0
Data received for 0x05
D
Data received for 0x04
D
Data received for 0x05
4
Data received for 0x05
6
Data received for 0x04
0
Data received for 0x05
B
Data received for 0x05
B
Data received for 0x04
8
Data received for 0x05
4
Data received for 0x05
0
Data received for 0x04
C
Output for Slave:
Data Received from Master Board
4
Writting Data on SPI bus
C
C
C
Sending Done
Data Received from Master Board
4
Writting Data on SPI bus
C
C
C
Sending Done
Data Received from Master Board
8
Writting Data on SPI bus
A
D
D
Sending Done
Data Received from Master Board
6
Writting Data on SPI bus
C
B
B
Sending Done
Data Received from Master Board
4
Writting Data on SPI bus
C
C
C
Sending Done
Data Received from Master Board
8
Writting Data on SPI bus
A
D
D
Sending Done
Data Received from Master Board
6
Writting Data on SPI bus
C
B
B
Sending Done
Data Received from Master Board
4
Writting Data on SPI bus
C
C
C
Sending Done
Data Received from Master Board
8
Writting Data on SPI bus
A