Hi!
I have been working with the BMX055 IMU module and i had a problem with reading the Magnetometer in SPI. I've working with an Arduino Mega and have tried I2C wich worked just fine but when i turn it to SPI,Accelarometerand Gyroscope i can read again, I can't just read the Mag. I have been following the datasheet but nothing quite work.
I would like to ask if anyone have a tip or an example for arduino to help me. Below i leave the program i have for ACC and Gyro.
Thank you.
#include <SPI.h>
int CSB1 = A2;
int CSB3 = A0;
int CSB2 = A1;
unsigned int x_lsb = 0x02;
unsigned int y_lsb = 0x04;
unsigned int z_lsb = 0x06;
const byte READ = 0b10000000; // SCP1000's read command
void setup() {
pinMode(CSB1, OUTPUT);
pinMode(CSB2, OUTPUT);
pinMode(CSB3, OUTPUT);
Serial.begin(9600);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV64);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
digitalWrite(CSB1, HIGH);
digitalWrite(CSB2, HIGH);
digitalWrite(CSB3, HIGH);
}
int readAcc(unsigned int coor) {
int aux;
digitalWrite(CSB1, LOW);
SPI.transfer(0x80 + coor); // prepare for read operation register coor where are LSB.
aux = SPI.transfer(0x00); // reads LSB (4 bits)
digitalWrite(CSB1, HIGH);
//check if acceleration value has been updated since last it has been read out last
if (aux & 1) {
aux = aux >> 4;
digitalWrite(CSB1, LOW);
SPI.transfer(0x80 + coor + 1); // MSB is on the next register
aux += (SPI.transfer(0x00) << 4); // sums MSB and LSB
if (aux & 0x800)
aux &= ~(0x800);
digitalWrite(CSB1, HIGH);
return aux;
}
else
return 0;
}
int readGyr(unsigned int coor) {
int lsb;
int msb;
int aux;
digitalWrite(CSB2, LOW);
SPI.transfer(0x80 + coor);
lsb = SPI.transfer(0x00);
digitalWrite(CSB2, HIGH);
digitalWrite(CSB2, LOW);
SPI.transfer(0x80 + coor + 1);
msb = SPI.transfer(0x00);
digitalWrite(CSB2, HIGH);
aux = msb;
if (lsb & 0x80) {
lsb = ~lsb;
lsb &= 0x7F;
lsb++;
}
if (msb & 0x80) {
msb = ~msb;
msb &= 0x7F;
msb++;
}
if (aux & 0x80) {
aux = (msb << 8 ) + lsb;
aux = ~aux;
aux++;
}
else {
aux = (msb << 8 )+ lsb;
}
// Serial.print(" msb:");
// Serial.println(msb);
// Serial.print(" lsb:");
// Serial.println(lsb);
return aux;
}
void loop() {
int x, y, z;
////////////////////////////////////////////////////////////////////////ACC READ / PRINT
x = readAcc(x_lsb);
y = readAcc(y_lsb);
z = readAcc(z_lsb);
//Serial.print("Acc x:");
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
//////////////////////////////////////////////////////////////////////////GYRO READ / PRINT
x = readGyr(x_lsb);
y = readGyr(y_lsb);
z = readGyr(z_lsb);
//Serial.print("Gyr x:");
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
}