Hi everyone, I'm trying to use the Arduino Due to read sensor data from the BMA280. This is a 3-axis accelerometer (datasheet: https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMA280-DS000-11_published.pdf). Protocol that I use is SPI.
Acceleration data for each axis has a width of 14-bit. Problem is, for each axis I always get 65532, which is (1111111111111100)2. I don't understand why it keeps returning this value. I bit-masked the last two bits because these are useless data as specified in the datasheet. The codes are as shown:
#include <SPI.h>
// https://www.arduino.cc/en/Tutorial/BarometricPressureSensorhttps://www.sparkfun.com/tutorials/240
// http://forum.arduino.cc/index.php/topic,159313.0.html
const int CS = 52;
byte PMU_Range = 0x0F;
byte PMU_BW = 0x10;
byte PMU_SELF_TEST = 0x32;
byte DATAX0 = 0x02; // x-axis LSB
byte DATAX1 = 0x03; // x-axis MSB
void setup(){
SPI.begin();
SPI.setDataMode(CS, 0); // set SPI mode to '00'
SPI.setClockDivider(CS, 84);
Serial.begin(115200);
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
writeRegister(PMU_Range, 0x03); // set ge-range to +/- 2G
writeRegister(PMU_BW, 0x0E); // set bandwidth to 500Hz
writeRegister(PMU_SELF_TEST, 0x00); // disable self-test
delay(100);
}
void loop(){
// read 2 bytes of data (LSB and MSB of x-axis) starting at register DATAX0
unsigned int xData = readRegister(DATAX0, 2);
Serial.println(xData);
delay(10);
}
void writeRegister(byte registerAddress, byte value){
// Set Chip Select pin low to signal the beginning of an SPI packet.
digitalWrite(CS, LOW);
// Transfer the register address over SPI.
SPI.transfer(registerAddress);
// Transfer the desired register value over SPI.
SPI.transfer(value);
// Set the Chip Select pin high to signal the end of an SPI packet.
digitalWrite(CS, HIGH);
}
unsigned int readRegister(byte thisRegister, int bytesToRead) {
byte lsb = 0; // least significant byte
unsigned int msb = 0; // most significant byte
// read process requires setting the first bit to 1
thisRegister = 0x80 | thisRegister;
digitalWrite(CS, LOW);
SPI.transfer(thisRegister);
// read least significant byte returned
lsb = SPI.transfer(0x00);
// decrement the number of bytes left to read:
bytesToRead--;
if (bytesToRead > 0) {
// read most significant byte returned
msb = SPI.transfer(0x00);
// shift most significant byte left
msb = msb << 8;
// combine the byte you just got with the previous one:
msb = msb | (lsb & 0xFC);
// decrement the number of bytes left to read:
bytesToRead--;
}
// take the chip select high to de-select:
digitalWrite(CS, HIGH);
// return the result:
return (msb);
}
I'm using the BMA280 shuttle board (schematic in attachment) which is easy to wire, so I suppose the problem lies in the software. I'd be grateful for any advice. Thanks! ![]()
