Hi guys, I'am trying to read out the Bytes of the G-Sensor BMA020 by SPI (4-Wire).
First, I want to keep all defaults and read out the acceleration.
So I tried to keep the code as short as possible by working with the datasheet:
http://stefanfrings.de/mindstorms/BMA020.pdf
As a result the three axis returns just zeros.. so I think the communication is not working.
Take a look at page 26 (or the pic: appendix) it is said, that a multiple reading out of all axis is possible.
(For a complete readout of 10 bit acceleration data from all axes the sensor IC provides the option to use an automatic incremented read command to read more than one byte (multiple read). This is activated when the serial enable pin CSB (chip select) stays active low after the read out of a data register. Thus, read out of data LSB will also cause read out of MSB if the CSB stays low for further 8 cycles of system clock.)
//Circuit
//BMA020 - Arduino Uno
//Uin -- 5V
//CSB ----- 10
//SCK ------ 13
//SDI ----- 11
//SDO ---- 12
//GND------GND
include <SPI.h>
const int chipSelectPin = 10; //ss
const byte READ =0B10000010; //page 26
void setup() {
Serial.begin(9600);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setDataMode(SPI_MODE3);
SPI.setBitOrder(MSBFIRST);
SPI.begin();
pinMode(chipSelectPin,OUTPUT);
digitalWrite(chipSelectPin, HIGH);
delay(100);
}
void loop() {
digitalWrite(chipSelectPin, LOW);
SPI.transfer(READ); // Command to Read //(0x80) Register 2 (0x02) multiple reading
byte xl = SPI.transfer(0); // Read register 2
byte xh = SPI.transfer(0); // Read register 3
byte yl = SPI.transfer(0); // Read register 4
byte yh = SPI.transfer(0); // Read register 5
byte zl = SPI.transfer(0); // Read register 6
byte zh = SPI.transfer(0); // Read register 7
digitalWrite(chipSelectPin,HIGH);
unsigned short x = int (xh << 2) | (xl >> 6);
unsigned short y = int (yh << 2) | (yl >> 6);
unsigned short z = int (zh << 2) | (zl >> 6);
Serial.print("x = ");
Serial.println(x,DEC);
Serial.print("y = ");
Serial.println(y,DEC);
Serial.print("z = ");
Serial.println(z,DEC);
delay(80);
}
Is my Code wrong? I think all settings were choosen right. I'am very unsure, that's possible to encode multiple reading, like I try it in the void loop, which starts with the byte Spi.transfer(READ)
thank you.