First i got 2 arduino nano to test for SPI(master and slave)
Then i copy the code from this post
it is fine when transferring "Hello World" only
so i edited some of its code to directly transfer byte instead
and i can see what is the actual data transferring with a software
but i can't get the right value from the slave received
here is my code
For master side:
#include <SPI.h>
uint8_t p[4];
void setup (void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
//SPI.setClockDivider(SPI_CLOCK_DIV2);//divide the clock by 8
p[0]=0x12;
p[1]=0x32;
p[2]=0x53;
p[3]=0xAB;
}
void loop (void) {
char c;
digitalWrite(SS, LOW); // enable Slave Select
// send test string
SPI.beginTransaction(SPISettings(8000000,MSBFIRST,SPI_MODE0));
{
SPI.transfer (0x12);
SPI.transfer (0x32);
SPI.transfer (0x53);
SPI.transfer (0xAB);
}
SPI.endTransaction();
digitalWrite(SS, HIGH); // disable Slave Select
Serial.print("SEnt");
delay(2000);
}
=========================================================
For Slave side:
#include <SPI.h>
byte buff [50];
volatile byte indx;
volatile boolean process;
void setup (void) {
Serial.begin (115200);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect){ // SPI interrupt routine {
byte c = SPDR; // read byte from SPI Data Register
//
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == 0xAB) //check for the end of the word
process = true;
}
Serial.print("Output:");
Serial.println(c,HEX);
}
void loop (void) {
if (process) {
process = false; //reset the process
Serial.print (buff[0],HEX); //print the array on serial monitor
Serial.print (buff[1],HEX); //print the array on serial monitor
Serial.print (buff[2],HEX); //print the array on serial monitor
Serial.println (buff[3],HEX); //print the array on serial monitor
indx= 0; //reset button to zero
}
}
=================================================
Cause i can't get the last byte with 0xAB ,so i just print out instantly to see what it got.
so my master side sent 0x12 0x32 0x53 0xAB
but my slave constantly get 0x57 and 0x65
when i alter my softer to see the data with cpha = 1
it somehow can read the byte as 0x25 0x65 0xA7 0x57
which contains the wrong data that slave side receive.
i don't know if it is my code wrong or i should alter the setting on slave side
Please help