2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MPU9250にArduinoからSPIで接続する方法

Posted at
mpu9250_spi.ino

# include <SPI.h>

//チップセレクトピンに10を使用
# define CS 10

void setup() {
  Serial.begin(115200);

  SPI.begin();

    /* MPU9250のSPIでのRead/Writeは1MHz。 */
  SPI.setClockDivider(SPI_CLOCK_DIV16);

  /* この二つの設定があまり理解できてない... */
  SPI.setBitOrder(MSBFIRST);//SPIバスの入出力に使用するビットオーダー(LSBFIRST or MSBFIRST)
  SPI.setDataMode(SPI_MODE0);//SPIの転送モード

  /* チップセレクトピンをアウトプットに設定 */
  pinMode(CS, OUTPUT);

}

uint8_t readByte(uint8_t regAddr) {
  uint8_t data;;
  digitalWrite(CS, LOW);
  SPI.transfer(regAddr | 0x80);
  data = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  return data;
}

void writeByte(uint8_t regAddr, uint8_t value) {
  digitalWrite(CS, LOW);
  SPI.transfer(regAddr | 0x80);
  SPI.transfer(value);
  digitalWrite(CS, HIGH);
}

0x75(WHO_AM_I)を読んで0x71が読めれば正常。
あとは0x6Bに0x00、0x37に0x02を書き込む。
この状態で0x3Bからの14バイトに加速度 X,Y,Z, ジャイロ X,Y,Z のデータが出力される。

9250にはDMP (Digital Motion Processor) という補正済みのデータ出力してくれる機能がついている。6050でDMPを使用しているサンプルはたくさんあるけど9250でDMPを使ってるサンプルが見つけられない...。6050のサンプルを読みつつ9250でのDMPの使い方を模索中...

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?