簡體   English   中英

與數字傳感器的Mbed SPI通信

[英]Mbed SPI Communication with Digital Sensor

我有一個Mbed和一個數字陀螺儀傳感器。 我想確保其中兩個通過SPI進行通信,其中Mbed(主機)能夠從陀螺儀傳感器(從機)讀取數據。 有人可以幫忙嗎?

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

    }
}

一個長遠的答案。 陀螺儀規格可能會指定CS變低的時間,您可以開始發送數據。 因此,在cs = 0之后嘗試延遲;

其次,當您打算閱讀WHO_AM_I注冊時,為什么還要設置MSB。 MSB = 1用於寫而不是讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM