문제

Im trying to send floats from one arduino to another over SPI using a union. I know my wiring is correct because I can send integers. The code looks correct to me so I must be missing something. When I run the code the serial monitor just prints out 0.0000.

Here is my master code

#include <SPI.h>
#include "pins_arduino.h"

void setup ()
{
  Serial.begin (115200);

  digitalWrite(SS, HIGH);  // ensure SS stays high for now

  // Put SCK, MOSI, SS pins into output mode
  // also put SCK, MOSI into LOW state, and SS into HIGH state.
  // Then put SPI hardware into Master mode and turn SPI on
  SPI.begin ();

  // Slow down the master a bit
  SPI.setClockDivider(SPI_CLOCK_DIV8);
}

byte transferAndWait (const byte what)
{
  byte a = SPI.transfer (what);
  delayMicroseconds (10);
  return a;
} 

union first_union
{
  float f;
  byte b[4];
}data;

float yaw, pitch, roll, alt, temp;   

void loop (void)
{
  digitalWrite(SS, LOW);

  //YAW
  transferAndWait ('Y');
  data.b[0] = transferAndWait ('1');
  data.b[1] = transferAndWait ('2');
  data.b[2] = transferAndWait ('3');
  data.b[3] = transferAndWait ('Z');
  yaw = data.f;

  //PITCH
  transferAndWait ('P');  
  data.b[0] = transferAndWait ('1');
  data.b[1] = transferAndWait ('2');
  data.b[2] = transferAndWait ('3');
  data.b[3] = transferAndWait ('Z');
  pitch = data.f;

  //ROLL
  transferAndWait ('R');  
  data.b[0] = transferAndWait ('1');
  data.b[1] = transferAndWait ('2');
  data.b[2] = transferAndWait ('3');
  data.b[3] = transferAndWait ('Z');
  roll = data.f;

  //TEMP
  transferAndWait ('T');  
  data.b[0] = transferAndWait ('1');
  data.b[1] = transferAndWait ('2');
  data.b[2] = transferAndWait ('3');
  data.b[3] = transferAndWait ('Z');
  temp = data.f;

  //ALT
  transferAndWait ('A');  
  data.b[0] = transferAndWait ('1');
  data.b[1] = transferAndWait ('2');
  data.b[2] = transferAndWait ('3');
  data.b[3] = transferAndWait ('Z');
  alt = data.f;

  digitalWrite(SS, HIGH);

  Serial.print("yaw:");
  Serial.println(yaw, 4);
  Serial.print("pitch: ");
  Serial.println(pitch,4);
  Serial.print("roll:");
  Serial.println(roll,4);
  Serial.print("alt: ");
  Serial.println(alt,4);
  Serial.print("temp: ");
  Serial.println(temp,4);

  delay(100);
}

and my slave code

#include <EEPROM.h>
#include <FreeIMU.h>
#include <Wire.h>
#include <SPI.h>
#include <HMC58X3.h>
#include <MS561101BA.h>
#include <I2Cdev.h>
#include <MPU60X0.h>
#include "pins_arduino.h"

//Set the default object
FreeIMU my3IMU = FreeIMU();
float ypr[3];
float alt = 0;
float temp = 0;

void setup()
{
  //Setup IMU
  Wire.begin();
  delay(500);
  my3IMU.init(true);
  delay(500);

  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

  // turn on interrupts
  SPCR |= _BV(SPIE);
}

union first_union
{
    float f;
    byte b[4];
}data;

ISR (SPI_STC_vect)
{
  byte command = SPDR;

  switch (command)
  {
  case 'Z':
    data.f = 0;
    SPDR = 0;
  case 'Y':
    data.f = ypr[0];
    SPDR = data.b[0]; 
    break;
  case 'P':
    data.f = ypr[2];
    SPDR = data.b[0]; 
    break;
  case 'R':
    data.f = ypr[1];
    SPDR = data.b[0]; 
    break;
  case 'A':
    data.f = alt;
    SPDR = data.b[0]; 
    break;
  case 'T':
    data.f = temp;
    SPDR = data.b[0]; 
    break;
  case 1:
    SPDR = data.b[1];
    break;
  case 2:
    SPDR = data.b[2];
    break;
  case 3:
    SPDR = data.b[3];
    break;
  }
}

void loop() 
{
  //my3IMU.getYawPitchRoll(ypr);
  //alt = my3IMU.getBaroAlt();
  //temp = my3IMU.getBaroTemp();

  ypr[0] = 1.1111;
  ypr[1] = 2.2222;
  ypr[2] = 3.3333;

  alt = 4.4444;
  temp = 5.5555;
}

Any ideas?

Thanks

도움이 되었습니까?

해결책

You are sending '1', '2' and '3' (i.e. the ASCII chars) but the switch looks for 1, 2 and 3 (i.e. the numbers).

Also, you're missing a break in the handler for 'Z'.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top