0
votes

I have a Mbed and a digital gyroscope sensor. I want to make sure two of them communications through SPI where Mbed (master) able to read the data from the gyroscope sensor (slave). Can anyone help out?

#include "mbed.h"

SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p8);
Serial pc(USBTX, USBRX);

int main() {

    // Slave select disabled //
    cs = 1;

    // Setup the spi for 8 bit data, high steady state clock,
    // Second edge capture, with a 1MHz clock rate
    spi.format(8,3);
    spi.frequency(500000);

    // Slave select enabled, it is active low // 
    cs = 0;

    // Send 0x8F, the command to read the WHO_AM_I register //
    // 0x8F 1000 1111, MSB = 0 means write and MSB = 1 means read // 
    spi.write(0x8F);

    // Send a dummy byte to receive the contents of the WHO_AM_I register //
    // The default response 1101 0011 // 
    int whoami = spi.write(0x00);
    pc.printf("WHOAMI register = 0x%X\n", whoami);

    // Slave select disabled //
    cs = 1;

    while(1) {

        cs = 0 ;
        // 0x28 is the register for OUT_X_L of the gyroscope //   
        spi.write(0x28);
        int data = spi.write(0x00);
        pc.printf("data output 0x%X\n", data);

        wait(0.5);

        cs = 1;

    }
}
1
What gyro part are you using? - Jan Jongboom

1 Answers

0
votes

A longshot answer. The gyro spec may specify a time between CS goes low and you can start sending data. Hence, try with a delay after cs = 0;

Secondly, why are you setting MSB when you intend to read WHO_AM_I register. MSB=1 is for writing and not reading.