Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BMX055 SPI Mag values

    Highlighted
    Member

    BMX055 SPI Mag values

    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);


    }

    Tags (3)
    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist