LPC 1114 + 6dof Imu sensor Mbed

0 nhận xét

Summary:

Use : LPC 1114 mirocontroller and 6dof imu sensor.

Fig.1 LPC 1114 microcontroller and 6dof imu sensor

Code

#include "mbed.h"
#define acc_add ((uint8_t) 0xA6)
#define gyro_add ((uint8_t) 0xD0)
Serial device(dp16, dp15);  // tx, rx
I2C i2c(dp5, dp27);//sda,scl
int main() {
    i2c.frequency(100000);
//acc init
    char cmd[2];
    cmd[0] = 0x31;
    cmd[1] = 0x09;
    i2c.write(acc_add, cmd, 2);
    wait(0.1);
    cmd[0] = 0x2D;
    cmd[1] = 0x08;
    i2c.write(acc_add, cmd, 2);
    wait(0.1);
//gyro init
    cmd[0] = 0x16;
    cmd[1] = 0x1A;
    i2c.write(gyro_add, cmd, 2);          
    wait(0.1);
    cmd[0] = 0x15;
    cmd[1] = 0x09;
    i2c.write(gyro_add, cmd, 2);
    wait(0.1);
    char data[6];
    device.baud(9600);
    int16_t acc_data_raw[3];
    int16_t gyro_data_raw[3];
    float acc_data[3],gyro_data[3];
    while(1){
        cmd[0]=0x32;
        i2c.write(acc_add, cmd, 1);
        wait(0.001);
        i2c.read(acc_add,data, 6);
        for(int i=0;i<3;i++){
            acc_data_raw[i]= ((data[2*i+1] << 8) | data[2*i]);
            acc_data[i]= (float)(acc_data_raw[i])*0.0383;
        }
        cmd[0]=0x1D;
        i2c.write(gyro_add, cmd, 1);
        wait(0.001);
        i2c.read(gyro_add,data, 6);
        for(int i=0;i<3;i++){
            gyro_data_raw[i]= ((data[2*i] << 8) | data[2*i+1]);
            gyro_data[i]=(float)(gyro_data_raw[i])/14.375;
        }
        device.printf ("ax=%2.2lf ay=%2.2lf az=%2.2lf vx=%2.2lf vy=%2.2lf vz=%2.2lf\n",acc_data[0],acc_data[1],acc_data[2],gyro_data[0],gyro_data[1],gyro_data[2]);
        wait(0.2);
    }
}



Facebook Comments
Blogger Comments
Share knowledge

Copyright © 2016 - Share knowledge ®