尝试在2个Arduinos之间通过SPI发送浮点值

时间:2018-03-19 04:29:28

标签: arduino spi

我目前正试图通过SPI在两个Arduinos上发送一个浮点值。目前我正在发送静态值2.25,然后通过Serial.println()命令读取它。然后我想从线性位移传感器传递浮点值。我的最终目标是能够让主人请求信息,奴隶收集适当的数据并将其打包,然后主人接收所述数据并用它做所需的事情。

目前我收到错误"调用过载' println(byte [7])'暧昧"而且我不确定为什么我会收到此错误。我目前是一名机械工程专业的学生,​​我正在通过C / C ++解决问题。我对自己所做的事情并不完全乐观。我知道浮点数是4个字节,我试图创建一个7字节的缓冲区来存储浮点数和' \ n'有空余的char。我目前的代码如下。

站长:

#include <SPI.h>
void setup() {
  pinMode(SS,OUTPUT);
  digitalWrite(SS,HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV4);
}

void loop() {
  digitalWrite(SS,LOW);
  float a = 2.25;
  SPI.transfer(a);
  SPI.transfer('\n');
  digitalWrite(SS,HIGH);
}

我的奴隶代码如下:

#include <SPI.h>

byte buf[7];
volatile byte pos = 0;
volatile boolean process_it = false;

void setup() {
  Serial.begin(9600);
  pinMode(MISO,OUTPUT);
  digitalWrite(MISO,LOW);
  SPCR |= _BV(SPE); // SPI Enable, sets this Arduino to Slave
  SPCR |= _BV(SPIE); // SPI interrupt enabled
}

ISR(SPI_STC_vect) {
  // Interrupt Service Routine(SPI_(SPI Transfer Complete)_vector)
  byte c = SPDR;
  // SPDR = SPI Data Register, so you are saving the byte of information in that register to byte c
  if (pos < sizeof buf) {
    buf[pos++] = c;
    if (c == '\n') {
      process_it = true;
    }
  }
}

void loop() {
  if (process_it = true) {
    Serial.println(buf);
    pos = 0;
    process_it = false;
  }
}

2 个答案:

答案 0 :(得分:0)

当前的问题是Serial.print不知道如何处理字节数组。将它声明为char数组或将其转换为print语句:

char buf[7];

OR

Serial.print((char*) buf);

无论哪种方式,它都不会像你想要的那样显示出来。

更简单的方法是使用memcpy或union在float和bytes之间来回切换。在主端:

uint8_t buf[4];
memcpy(buf, &a, 4);

然后使用SPI发送4个字节。在外围端反转它。

请注意发送&#39; \ n&#39;因为终止字节是一个坏主意,因为它可能导致奇怪的行为,因为浮点数中的一个字节很容易为0x0a,十六进制等效于&#39; \ n&#39;。

答案 1 :(得分:0)

我想出了我需要做什么,我想发布完成的代码。我还添加了一个传递多个浮点值的功能。

站长:

#include <SPI.h>

float a = 3.14;
float b = 2.25;
uint8_t storage [12];
float buff[2] = {a, b};
void setup()
{
  digitalWrite(SS, HIGH);
  SPI.begin();
  Serial.begin(9600);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void loop()
{

  digitalWrite(SS, LOW);
  memcpy(storage, &buff, 8);
  Serial.print("storage[0] = "); Serial.println(storage[0]); // the 
  following serial prints were to check i was getting the right decimal 
  numbers for the floats.
  Serial.print("storage[1] = "); Serial.println(storage[1]);
  Serial.print("storage[2] = "); Serial.println(storage[2]);
  Serial.print("storage[3] = "); Serial.println(storage[3]);
  Serial.print("storage[4] = "); Serial.println(storage[4]);
  Serial.print("storage[5] = "); Serial.println(storage[5]);
  Serial.print("storage[6] = "); Serial.println(storage[6]);
  Serial.print("storage[7] = "); Serial.println(storage[7]);
  SPI.transfer(storage, sizeof storage ); //SPI library allows a user to 
  transfer a whole array of bytes and you need to include the size of the 
  array.
  digitalWrite(SS, HIGH);
  delay(1000);
}

对于我的奴隶代码:

#include <SPI.h>

byte storage [8];
volatile byte pos;
volatile boolean process;
float buff[2];

void setup()
{
  pinMode(MISO,OUTPUT);
  SPCR |= _BV(SPE);
  SPCR |= _BV(SPIE);
  pos = 0;
  process = false;
  Serial.begin(9600);

}

ISR(SPI_STC_vect)
{
  byte gathered = SPDR;
  if( pos < sizeof storage)
    {
      storage[pos++] = gathered;
    }
    else 
    process = true;
}

void loop()
{
  if( process )
  {
    Serial.print("storage[0] = "); Serial.println(storage[0]);
    Serial.print("storage[1] = "); Serial.println(storage[1]);
    Serial.print("storage[2] = "); Serial.println(storage[2]);
    Serial.print("storage[3] = "); Serial.println(storage[3]);
    Serial.print("storage[4] = "); Serial.println(storage[4]);
    Serial.print("storage[5] = "); Serial.println(storage[5]);
    Serial.print("storage[6] = "); Serial.println(storage[6]);
    Serial.print("storage[7] = "); Serial.println(storage[7]);
    memcpy(buff,&storage,8);
    Serial.print("This is buff[0]");Serial.println(buff[0]);
    Serial.print("This is buff[1]");Serial.println(buff[1]);
    storage[pos] = 0;
    pos = 0;
    process = false;


  }
}