GY-521をlpcxpresso1768で使ってみる
6軸加速度センサーのGY-521をlpcxpresso1768で使ってみました。
xpressoはmbedと互換があるので、ベースとなるプログラムはmbedのデベロッパーのページから拾ってきました。
https://developer.mbed.org/users/onehorse/code/MPU6050IMU/
便利な時代です。
これをmbedのオンラインコンパイラで読み込んでそのままxpresso用にエクスポートします。
デバッグとしてxpressoからシリアルに出力します。
mbedでは
Serial pc(USBTX,USBRX);
でしたがXpressoにはminiBはついていないので
Serial pc(p13, p14);
とします。
よし動かそうとxpressoで読み込んで使おうとしたところここで問題が。
MPU6050というセンサーのアドレスの値を以下のコードで読み取るのですが
uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
whoamiの戻り値が0x68になるべきところ0x7aとなっていました。
ヘッダファイルやデータシートを見てもわからない。
ということで
// Read the WHO_AM_I register, this is a good test of communication
uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
if (whoami == 0x68) // WHO_AM_I should always be 0x68
{
pc.printf("MPU6050 is online...");
wait(1);
これを
// Read the WHO_AM_I register, this is a good test of communication
uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r");
if(whoami){
pc.printf("MPU6050 is online...");
wait(1);
このようにしました。そうしたら値が返ってきました。
モジュールのアドレスはあまり関係ないのですかね。