我已经在这个话题上挣扎了一段时间,因此我决定在这里提问。我浏览了几个站点,以获取清晰答案,不幸的是,该主题非常混乱,答案通常仅涵盖其中的特定部分。而且看来,有多种方法可以使这种起作用。
我想拥有一个可以驱动一些传感器,收集所有值并可以通过SPI将这些值提供给连接的Arduino Mega2560的Arduino Nano。
我这样给他们接线:
Mega2560 Nano
MOSI 51 11
MISO 50 12
SCK 52 13
SS (Slave) 53 10
在Mega2560(主设备)上,我正在运行以下代码:
#include <SPI.h>
int i = 0;
byte myData[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
union
{
long x;
byte rxData[8];
} data;
void setup()
{
Serial.begin(115200);
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV128);
digitalWrite(SS, HIGH);
delay(100);
}
void loop()
{
digitalWrite(SS, LOW);
SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV128, MSBFIRST, SPI_MODE0));
SPI.transfer(myData, sizeof(myData));
data.rxData[0] = myData[1];
data.rxData[1] = myData[2];
data.rxData[2] = myData[3];
data.rxData[3] = myData[4];
data.rxData[4] = myData[5];
data.rxData[5] = myData[6];
data.rxData[6] = myData[7];
data.rxData[7] = myData[0];
SPI.endTransaction();
digitalWrite(SS, HIGH);
int number1 = (int)(data.rxData[1] << 8) | (int)data.rxData[0];
int number2 = (int)(data.rxData[3] << 8) | (int)data.rxData[2];
int number3 = (int)(data.rxData[5] << 8) | (int)data.rxData[4];
int number4 = (int)(data.rxData[7] << 8) | (int)data.rxData[6];
Serial.print("Received data from Slave: ");
Serial.println(data.x, HEX);
Serial.print("Number-1: ");
Serial.println(number1);
Serial.print("Number-2: ");
Serial.println(number2);
Serial.print("Number-3: ");
Serial.println(number3);
Serial.print("Number-4: ");
Serial.println(number4);
Serial.println("================================");
delay(2000);
}
对于Nano(奴隶),我使用以下草图:
#include <SPI.h>
volatile uint16_t sensorId;
uint16_t distancePrimaryCM = 200;
uint16_t distanceSecondaryCM = 300;
uint16_t distanceTertiaryCM = 400;
uint16_t distanceQuaternaryCM = 500;
volatile bool flag1 = false;
byte myData[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int i = 0;
void setup()
{
Serial.begin(115200);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT_PULLUP);
SPCR |= _BV(SPE);
bitClear(SPCR, 8);
SPCR |= _BV(SPIE);
}
void loop()
{
if(flag1 == true)
{
SPDR = myData[i];
flag1 = false;
i++;
if (i == 8)
{
i = 0;
}
}
myData[0] = lowByte(distancePrimaryCM);
myData[1] = highByte(distancePrimaryCM);
myData[2] = lowByte(distanceSecondaryCM);
myData[3] = highByte(distanceSecondaryCM);
myData[4] = lowByte(distanceTertiaryCM);
myData[5] = highByte(distanceTertiaryCM);
myData[6] = lowByte(distanceQuaternaryCM);
myData[7] = highByte(distanceQuaternaryCM);
}
ISR(SPI_STC_vect)
{
flag1 = true;
}
通信似乎正在起作用... 一种。我希望在母版上读取的数字是200、300、400和500。但是,母版上的输出看起来像这样:
11:59:01.765 -> Received data from Slave: 12C00C8
11:59:01.765 -> Number-1: 200
11:59:01.765 -> Number-2: 300
11:59:01.765 -> Number-3: 400
11:59:01.800 -> Number-4: 400
11:59:01.800 -> ================================
11:59:03.796 -> Received data from Slave: 2C0000C8
11:59:03.796 -> Number-1: 200
11:59:03.796 -> Number-2: 11264
11:59:03.796 -> Number-3: -32511
11:59:03.796 -> Number-4: 500
11:59:03.796 -> ================================
11:59:05.801 -> Received data from Slave: 12C00C8
11:59:05.801 -> Number-1: 200
11:59:05.801 -> Number-2: 300
11:59:05.801 -> Number-3: 300
11:59:05.801 -> Number-4: 500
11:59:05.801 -> ================================
11:59:07.802 -> Received data from Slave: 12C0064
11:59:07.802 -> Number-1: 100
11:59:07.802 -> Number-2: 300
11:59:07.802 -> Number-3: 11408
11:59:07.802 -> Number-4: 257
11:59:07.802 -> ================================
11:59:09.813 -> Received data from Slave: 12C00C8
11:59:09.813 -> Number-1: 200
11:59:09.813 -> Number-2: 300
11:59:09.813 -> Number-3: 400
11:59:09.813 -> Number-4: 300
11:59:09.813 -> ================================
11:59:11.815 -> Received data from Slave: 2C00C8C8
11:59:11.815 -> Number-1: -14136
11:59:11.815 -> Number-2: 11264
11:59:11.815 -> Number-3: 257
11:59:11.815 -> Number-4: 500
11:59:11.815 -> ================================
11:59:13.825 -> Received data from Slave: C800C8
11:59:13.825 -> Number-1: 200
11:59:13.825 -> Number-2: 200
11:59:13.825 -> Number-3: 400
11:59:13.825 -> Number-4: 500
11:59:13.825 -> ================================
11:59:15.799 -> Received data from Slave: C82C00C8
11:59:15.799 -> Number-1: 200
11:59:15.835 -> Number-2: -14292
11:59:15.835 -> Number-3: -28672
11:59:15.835 -> Number-4: 372
11:59:15.835 -> ================================
11:59:17.836 -> Received data from Slave: 12CC801
11:59:17.836 -> Number-1: -14335
11:59:17.836 -> Number-2: 300
11:59:17.836 -> Number-3: 400
11:59:17.836 -> Number-4: 500
11:59:17.836 -> ================================
11:59:19.839 -> Received data from Slave: 2C2C00C8
11:59:19.839 -> Number-1: 200
11:59:19.839 -> Number-2: 11308
11:59:19.839 -> Number-3: -32767
11:59:19.839 -> Number-4: 500
11:59:19.839 -> ================================
11:59:21.846 -> Received data from Slave: 2C2C00C8
11:59:21.846 -> Number-1: 200
11:59:21.846 -> Number-2: 11308
11:59:21.846 -> Number-3: 300
11:59:21.846 -> Number-4: 500
11:59:21.846 -> ================================
11:59:23.855 -> Received data from Slave: 2C2C00C8
11:59:23.855 -> Number-1: 200
11:59:23.855 -> Number-2: 11308
11:59:23.855 -> Number-3: 11308
11:59:23.855 -> Number-4: 500
11:59:23.855 -> ================================
11:59:25.856 -> Received data from Slave: 100C8C8
11:59:25.856 -> Number-1: -14136
11:59:25.856 -> Number-2: 256
11:59:25.856 -> Number-3: 400
11:59:25.856 -> Number-4: 500
11:59:25.856 -> ================================
11:59:27.848 -> Received data from Slave: 12C00C8
11:59:27.848 -> Number-1: 200
11:59:27.848 -> Number-2: 300
11:59:27.848 -> Number-3: -28528
11:59:27.848 -> Number-4: 257
11:59:27.848 -> ================================
11:59:29.870 -> Received data from Slave: 100C8C8
11:59:29.870 -> Number-1: -14136
11:59:29.870 -> Number-2: 256
11:59:29.870 -> Number-3: 400
11:59:29.870 -> Number-4: 500
11:59:29.870 -> ================================
11:59:31.862 -> Received data from Slave: 12C0001
11:59:31.862 -> Number-1: 1
11:59:31.862 -> Number-2: 300
11:59:31.862 -> Number-3: 400
11:59:31.862 -> Number-4: 500
11:59:31.862 -> ================================
11:59:33.876 -> Received data from Slave: 2C0000C8
11:59:33.876 -> Number-1: 200
11:59:33.876 -> Number-2: 11264
11:59:33.876 -> Number-3: 400
11:59:33.876 -> Number-4: 500
11:59:33.876 -> ================================
11:59:35.861 -> Received data from Slave: C8
11:59:35.861 -> Number-1: 200
11:59:35.861 -> Number-2: 0
11:59:35.861 -> Number-3: -32468
11:59:35.861 -> Number-4: 500
11:59:35.861 -> ================================
11:59:37.870 -> Received data from Slave: 116C801
11:59:37.870 -> Number-1: -14335
11:59:37.870 -> Number-2: 278
11:59:37.870 -> Number-3: 400
11:59:37.870 -> Number-4: 385
11:59:37.870 -> ================================
...
我想了解那里发生了什么以及如何改进我的代码,以解决主机读取随机数的问题。我的猜测是,这与两个组件之间的时钟/同步有关,但是到目前为止,为了理解实际问题,我对此主题的概述还不够。
非常感谢任何提示!预先感谢。