3 axis gyroscope & 3 axis accelerometer
Dependents: sgam-lib sgam_mdw_NUCLEOF429ZI_impl
Fork of MPU6050 by
Usage
#include "mbed.h" #include "MPU6050.h" DigitalOut myled(LED1); Serial pc(USBTX, USBRX); I2C i2c(I2C_SDA, I2C_SCL); MPU6050 mpu(i2c); int16_t ax, ay, az; int16_t gx, gy, gz; int main() { pc.printf("MPU6050 test\n\n"); pc.printf("MPU6050 initialize \n"); mpu.initialize(); pc.printf("MPU6050 testConnection \n"); bool mpu6050TestResult = mpu.testConnection(); if(mpu6050TestResult) { pc.printf("MPU6050 test passed \n"); } else { pc.printf("MPU6050 test failed \n"); } while(1) { wait(1); mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //writing current accelerometer and gyro position pc.printf("%d;%d;%d;%d;%d;%d\n",ax,ay,az,gx,gy,gz); } }
Diff: I2Cdev.cpp
- Revision:
- 2:8c562a8fed36
- Parent:
- 1:1e0baaf91e96
diff -r 1e0baaf91e96 -r 8c562a8fed36 I2Cdev.cpp --- a/I2Cdev.cpp Wed May 08 00:34:55 2013 +0000 +++ b/I2Cdev.cpp Fri Sep 12 09:28:14 2014 +0800 @@ -5,14 +5,8 @@ #include "I2Cdev.h" -#define useDebugSerial -I2Cdev::I2Cdev(): debugSerial(USBTX, USBRX), i2c(I2C_SDA,I2C_SCL) -{ - -} - -I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): debugSerial(USBTX, USBRX), i2c(i2cSda,i2cScl) +I2Cdev::I2Cdev(I2C &interface) : i2c(interface) { } @@ -254,18 +248,31 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) { - i2c.start(); - i2c.write(devAddr<<1); - i2c.write(regAddr); - for(int i = 0; i < length; i++) { - i2c.write(data[i]); + int len = length + 1; + char *buf = (char*)malloc(len); + buf[0] = regAddr; + for (int i = 0; i < length; i++) { + buf[i + 1] = data[i]; } - i2c.stop(); + i2c.write(devAddr << 1 , buf, len); + + free (buf); return true; } bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data) { + int len = length * 2 + 1; + char *buf = (char*)malloc(len); + buf[0] = regAddr; + for (int i = 0; i < length; i++) { + uint16_t word = data[i]; + buf[2*i + 1] = word >> 8; + buf[2*i + 2] = word & 0xFF; + } + i2c.write(devAddr << 1 , buf, len); + + free (buf); return true; }