SPI transfer with BMA222 accelerometer

Hi,

I'm trying to communicate with an onboard BMA222 accelerometer. This is my first time doing SPI communication, so could anyone tell me what my code is missing? I simply get no responses.

#include "SPI.h"

void setup()
{
  pinMode(17, OUTPUT); //pin 17 is Chip select
  digitalWrite(17,HIGH);
  SPI.begin();
  SerialUSB.begin(9600);
}

void loop()
{
byte c;

digitalWrite(17, LOW);
c = SPI.transfer(0x08);  //0x08 is the temperature sensor
digitalWrite(17, HIGH);

SerialUSB.println("Temp:"+c);
}

Printing "c" results in no output. I realize I am missing SPI_MODE stuff, but I'm not sure which mode corresponds to the BMA222, and I have seen example code online that does not use it. Could that be my problem? Thanks!

You need to read up on how to use SPI, e.g. start here: SPI - Arduino Reference

Not sure what this is supposed to do: Does it even compile?

SerialUSB.println("Temp:"+c);

Interesting!

Using Arduino version 1.0.5-r2, the following program compiles, uploads and prints nothing between the ><. Anyone care to comment?

void setup(){
  byte c=48;
  Serial.begin(9600);
  Serial.print("test>");
  Serial.print("Temp"+c);
  Serial.println("<done");
}
void loop(){}

Thanks for the reply, That print statement is there to check what the reply from the BMA222E device is, should I be using int c instead of byte c?

I changed my main loop to:

void loop()
{
byte c;
SPI.beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE0));
digitalWrite(17, LOW);
c = SPI.transfer(0x08);
digitalWrite(17, HIGH);
SPI.endTransaction();
SerialUSB.println("Temp:"+c);
}

But it still doesn't seem to work. Am I still missing something?

Yes, at the very least

Serial.print("Temp:"+c)

is not properly defined and does nothing.

Try

Serial.print("Temp: ");
Serial.println(c);

I still haven't managed to get this chip to work. Could there be any significance of getting a 0 as a return from SPI.transfer?