11-29-2019 12:58 PM - edited 11-29-2019 01:01 PM
Hi! Ive been working with the BMX055 and been trying to implement with SPI and havent been sucssefull with the reading in the magnetometer. I received the information that using the Mag in I2C and trying SPI after, something should be done to change the pin. I honestly didnt understand and would like to know if somoene could help me. I leave my program for arduino below hoping that somoene could help me or know what the problem is, since i can only reado 0,0,0 from the mag in SPI. And since i've used it in I2C and everything worked fine, i think is probably something that im missing.
Thank you very much for your attention and help!
#include <SPI.h>
int CSB1 = A1;
int CSB2 = A2;
int CSB3 = A3;
unsigned int x_lsb = 0x02;
unsigned int y_lsb = 0x04;
unsigned int z_lsb = 0x06;
const byte READ = 0b10000000;
unsigned long elp_time = micros();
unsigned long read_time = micros();
void setup() {
pinMode(CSB1, OUTPUT);
pinMode(CSB2, OUTPUT);
pinMode(CSB3, OUTPUT);
Serial.begin(115200);
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);
aux = SPI.transfer(0x00);
digitalWrite(CSB1, HIGH);
if (aux & 1) {
aux = aux >> 4;
digitalWrite(CSB1, LOW);
SPI.transfer(0x80 + coor + 1);
aux += (SPI.transfer(0x00) << 4);
if (aux & 0x800)
aux &= ~(0x800);
digitalWrite(CSB1, HIGH);
return aux;
}
else
return 0;
//delay(50);
}
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;
}
return aux;
}
int readMag(unsigned int coor) {
int aux;
SPI.transfer(0xC0 + coor);
aux = (SPI.transfer(0x00) >> 3);
digitalWrite(CSB3, HIGH);
digitalWrite(CSB3, LOW);
SPI.transfer(0xC0 + coor + 1);
aux += (SPI.transfer(0x00) << 5);
digitalWrite(CSB3, HIGH);
return aux;
}
void loop() {
// if (micros()-read_time > 5){
// read_time = micros();
int x, y, z;
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);
x = readGyr(x_lsb);
y = readGyr(y_lsb);
z = readGyr(z_lsb);
// Serial.print(x);
// Serial.print(",");
// Serial.print(y);
// Serial.print(",");
// Serial.println(z);
x = readMag(x_lsb);
y = readMag(y_lsb);
z = readMag(z_lsb);
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
}
12-11-2019 10:30 AM
If you have logical analyzer waveform, please provide it to us. BMM150 need initialize by setting register 0x4B, 0x4C.