Hello, I play around SPI and trying to send floats from Mega (slave) to Nano (master). I use union, it looks like elegant and readable way for me. Everything works, however, I noticed one issue. When slave increases the number that is sent, there is wrong received number showed on the side of master, and only in case of 8, 16, 128. Not for 32, 64, nor 256. I didn't check higher values. I cannot figure out what could cause this problem. I went through many discussions and didn't find this. Any experience, idea? Thank you.
Master code:
#include <SPI.h>
union myUserData {
float x;
byte myBytes[4];
};
myUserData myUD;
byte myData[] = {0x00, 0x00, 0x00, 0x00};
void setup(){
Serial.begin(19200);
SPI.begin();
delay(100);
SPI.setClockDivider(SPI_CLOCK_DIV16);
digitalWrite(SS,LOW);
}
void loop(){
for(int i = 0; i < 4; i++){
myData[i] = SPI.transfer(0);
delayMicroseconds(100); // allow Slave to process the received data byte
Serial.print(myData[i], HEX); // S0>M3, S1>M0, S2>M1, S3>M2
Serial.print(" ");
}
myUD.myBytes[3] = myData[0];
myUD.myBytes[0] = myData[1];
myUD.myBytes[1] = myData[2];
myUD.myBytes[2] = myData[3];
Serial.print(" float: ");
Serial.print(myUD.x, 2);
Serial.println();
delay(3000); //test interval
}
Slave code:
#include <SPI.h>
union myUserData {
float x;
byte myBytes[4];
};
myUserData myUD;
volatile bool flag = false;
volatile int i = 0;
void setup(){
Serial.begin(19200);
pinMode(SS, INPUT_PULLUP);// ensure SS stays high for now
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
SPCR |= !(_BV(MSTR)); // defining Slave
//bitSet(SPCR, SPE); //enable SPI Port, nechat?
SPI.attachInterrupt();
myUD.x = 1.11;
}
void loop(){
if (flag == true){
flag = false;
myUD.x++;
Serial.print (myUD.myBytes[0], HEX);
Serial.print (" ");
Serial.print (myUD.myBytes[1], HEX);
Serial.print (" ");
Serial.print (myUD.myBytes[2], HEX);
Serial.print (" ");
Serial.print (myUD.myBytes[3], HEX);
Serial.print (" float: ");
Serial.println (myUD.x, 2);
}
}
ISR (SPI_STC_vect){
SPDR = myUD.myBytes[i];
i++;
if (i == 4) {
i = 0; // 4-byte data are sent
flag = true;
}
}
Mega Slave sent data:
19:24:22.888 -> 1F 85 C3 40 float: 6.11
19:24:27.879 -> 1F 85 E3 40 float: 7.11
19:24:32.901 -> 90 C2 1 41 float: 8.11
19:24:37.924 -> 90 C2 11 41 float: 9.11
19:24:42.916 -> 90 C2 21 41 float: 10.11
Nano Master received data:
19:24:27.880 -> 40 1F 85 C3 float: 6.11
19:24:32.903 -> 40 1F 85 E3 float: 7.11
19:24:37.925 -> 40 90 C2 1 float: 2.03
19:24:42.918 -> 41 90 C2 11 float: 9.11
19:24:47.939 -> 41 90 C2 21 float: 10.11
Mega Slave sent data:
19:26:23.054 -> 48 E1 F0 41 float: 30.11
19:26:28.063 -> 48 E1 F8 41 float: 31.11
19:26:33.052 -> A4 70 0 42 float: 32.11
19:26:38.075 -> A4 70 4 42 float: 33.11
19:26:43.098 -> A4 70 8 42 float: 34.11
Nano Master received data:
19:26:28.063 -> 41 48 E1 F0 float: 30.11
19:26:33.052 -> 41 48 E1 F8 float: 31.11
19:26:38.075 -> 41 A4 70 0 float: 8.03
19:26:43.098 -> 42 A4 70 4 float: 33.11
19:26:48.073 -> 42 A4 70 8 float: 34.11
Mega Slave sent data:
19:34:23.723 -> 52 38 FC 42 float: 126.11
19:34:28.713 -> 52 38 FE 42 float: 127.11
19:34:33.743 -> 29 1C 0 43 float: 128.11
19:34:38.760 -> 29 1C 1 43 float: 129.11
19:34:43.743 -> 29 1C 2 43 float: 130.11
Nano Master received data:
19:34:28.712 -> 42 52 38 FC float: 126.11
19:34:33.742 -> 42 52 38 FE float: 127.11
19:34:38.759 -> 42 29 1C 0 float: 32.03
19:34:43.743 -> 43 29 1C 1 float: 129.11
19:34:48.765 -> 43 29 1C 2 float: 130.11