<CODE/> is a button in the composer window on the forum, not a text that you type in.
Code tags are three back ticks (```) on their own line at the beginning and the end of the code block; that is basically what the <CODE/> button does. I've fixed the code tags for you.
2. Upload the following Master (SPI-MEGA) Sketch to receive two bytes (0xABCD) data from Slave.
#include<SPI.h>
int y = 0x1234;
void setup()
{
Serial.begin(9600);
SPI.begin(); //default speed: 4 Mbits/sec
digitalWrite(SS, LOW); //Slave is enabled
}
void loop()
{
byte y1 = SPI.transfer(highByte(y));
delayMicroseconds(10); //must be
byte y2 = SPI.transfer(lowByte(y));
delayMicroseconds(10); //must be
//--------------------------------
int recInt = y2 << 8 | y1;
Serial.println((uint16_t)recInt, HEX);
delay(1000);
}
3. Upload the following Slave (SPI-NANO) Sketch to receive 2-byte (0x1234) data from Master.
#include<SPI.h>
byte txData[] = {0xAB, 0xCD};
byte rxData[2];
volatile bool flag = false;
int i = 0;
void setup()
{
Serial.begin(9600);
pinMode(SS, INPUT_PULLUP);
pinMode(MISO, OUTPUT);
bitClear(SPCR, MSTR); //UNO-2 is Slave
bitSet(SPCR, SPE); //SPI Port is created
SPI.attachInterrupt(); //LIE, GIE are active
}
void loop()
{
if (flag == true)
{
Serial.print("Received Data: ");
Serial.print(rxData[0], HEX);
Serial.println(rxData[1], HEX);
//------------------------------
flag = false;
}
}
ISR(SPI_STC_vect)
{
rxData[i] = SPDR;
SPDR = txData[i];
i++;
if (i == 2)
{
i = 0;
flag = true;
}
}
4. Open Serial Monitor (SM) of SPI-Master. 5. Open Serial Monitor of SPI-Slave. 6. Press and Hold REST uttons of both Arduino. 7. Release RESET utton of Slave. 8. Release RESET Button of Master. 9. Check that SM of Master shows ABCD at 1-sec interval.
ABCD
ABCD
ABCD
10. Check that SM of Slave shows 1234 at 1-sec interval.
Received Data: 1234
Received Data: 1234
Received Data: 1234
11. Now modify Slave sketch to acquire 2-byte data from the Pot of Slave and send them to SPI-Master. Hints: