ICM20602_I2C(invensense)---stm32f767zi
Fork of NTOUEE-mbed-I2C_MPU6500 by
mpu6500.cpp@0:95e46ae24046, 2016-10-21 (annotated)
- Committer:
- rkuo2000
- Date:
- Fri Oct 21 02:52:40 2016 +0000
- Revision:
- 0:95e46ae24046
mbed I2C reading MPU6500
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rkuo2000 | 0:95e46ae24046 | 1 | #include "mbed.h" |
rkuo2000 | 0:95e46ae24046 | 2 | #include "mpu6500.h" |
rkuo2000 | 0:95e46ae24046 | 3 | |
rkuo2000 | 0:95e46ae24046 | 4 | I2C mpu6500_i2c(PC_9, PA_15); // I2C0_SDA, I2C0_SCL |
rkuo2000 | 0:95e46ae24046 | 5 | |
rkuo2000 | 0:95e46ae24046 | 6 | void MPU6500_WriteByte(uint8_t MPU6500_reg, uint8_t MPU6500_data) |
rkuo2000 | 0:95e46ae24046 | 7 | { |
rkuo2000 | 0:95e46ae24046 | 8 | char data_out[2]; |
rkuo2000 | 0:95e46ae24046 | 9 | data_out[0]=MPU6500_reg; |
rkuo2000 | 0:95e46ae24046 | 10 | data_out[1]=MPU6500_data; |
rkuo2000 | 0:95e46ae24046 | 11 | mpu6500_i2c.write(MPU6500_slave_addr, data_out, 2, 0); |
rkuo2000 | 0:95e46ae24046 | 12 | } |
rkuo2000 | 0:95e46ae24046 | 13 | |
rkuo2000 | 0:95e46ae24046 | 14 | uint8_t MPU6500_ReadByte(uint8_t MPU6500_reg) |
rkuo2000 | 0:95e46ae24046 | 15 | { |
rkuo2000 | 0:95e46ae24046 | 16 | char data_out[1], data_in[1]; |
rkuo2000 | 0:95e46ae24046 | 17 | data_out[0] = MPU6500_reg; |
rkuo2000 | 0:95e46ae24046 | 18 | mpu6500_i2c.write(MPU6500_slave_addr, data_out, 1, 1); |
rkuo2000 | 0:95e46ae24046 | 19 | mpu6500_i2c.read(MPU6500_slave_addr, data_in, 1, 0); |
rkuo2000 | 0:95e46ae24046 | 20 | return (data_in[0]); |
rkuo2000 | 0:95e46ae24046 | 21 | } |
rkuo2000 | 0:95e46ae24046 | 22 | |
rkuo2000 | 0:95e46ae24046 | 23 | void MPU6500::initialize() |
rkuo2000 | 0:95e46ae24046 | 24 | { |
rkuo2000 | 0:95e46ae24046 | 25 | MPU6500_WriteByte(MPU6500_PWR_MGMT_1, 0x00); // CLK_SEL=0: internal 8MHz, TEMP_DIS=0, SLEEP=0 |
rkuo2000 | 0:95e46ae24046 | 26 | MPU6500_WriteByte(MPU6500_SMPLRT_DIV, 0x07); // Gyro output sample rate = Gyro Output Rate/(1+SMPLRT_DIV) |
rkuo2000 | 0:95e46ae24046 | 27 | MPU6500_WriteByte(MPU6500_CONFIG, 0x06); // set TEMP_OUT_L, DLPF=2 (Fs=1KHz) |
rkuo2000 | 0:95e46ae24046 | 28 | MPU6500_WriteByte(MPU6500_GYRO_CONFIG, 0x18); // bit[4:3] 0=+-250d/s,1=+-500d/s,2=+-1000d/s,3=+-2000d/s |
rkuo2000 | 0:95e46ae24046 | 29 | MPU6500_WriteByte(MPU6500_ACCEL_CONFIG, 0x01);// bit[4:3] 0=+-2g,1=+-4g,2=+-8g,3=+-16g, ACC_HPF=On (5Hz) |
rkuo2000 | 0:95e46ae24046 | 30 | } |
rkuo2000 | 0:95e46ae24046 | 31 | |
rkuo2000 | 0:95e46ae24046 | 32 | int16_t MPU6500::getAccelXvalue() |
rkuo2000 | 0:95e46ae24046 | 33 | { |
rkuo2000 | 0:95e46ae24046 | 34 | uint8_t LoByte, HiByte; |
rkuo2000 | 0:95e46ae24046 | 35 | LoByte = MPU6500_ReadByte(MPU6500_ACCEL_XOUT_L); // read Accelerometer X_Low value |
rkuo2000 | 0:95e46ae24046 | 36 | HiByte = MPU6500_ReadByte(MPU6500_ACCEL_XOUT_H); // read Accelerometer X_High value |
rkuo2000 | 0:95e46ae24046 | 37 | return((HiByte<<8) | LoByte); |
rkuo2000 | 0:95e46ae24046 | 38 | } |
rkuo2000 | 0:95e46ae24046 | 39 | |
rkuo2000 | 0:95e46ae24046 | 40 | int16_t MPU6500::getAccelYvalue() |
rkuo2000 | 0:95e46ae24046 | 41 | { |
rkuo2000 | 0:95e46ae24046 | 42 | uint8_t LoByte, HiByte; |
rkuo2000 | 0:95e46ae24046 | 43 | LoByte = MPU6500_ReadByte(MPU6500_ACCEL_YOUT_L); // read Accelerometer X_Low value |
rkuo2000 | 0:95e46ae24046 | 44 | HiByte = MPU6500_ReadByte(MPU6500_ACCEL_YOUT_H); // read Accelerometer X_High value |
rkuo2000 | 0:95e46ae24046 | 45 | return ((HiByte<<8) | LoByte); |
rkuo2000 | 0:95e46ae24046 | 46 | } |
rkuo2000 | 0:95e46ae24046 | 47 | |
rkuo2000 | 0:95e46ae24046 | 48 | int16_t MPU6500::getAccelZvalue() |
rkuo2000 | 0:95e46ae24046 | 49 | { |
rkuo2000 | 0:95e46ae24046 | 50 | uint8_t LoByte, HiByte; |
rkuo2000 | 0:95e46ae24046 | 51 | LoByte = MPU6500_ReadByte(MPU6500_ACCEL_ZOUT_L); // read Accelerometer X_Low value |
rkuo2000 | 0:95e46ae24046 | 52 | HiByte = MPU6500_ReadByte(MPU6500_ACCEL_ZOUT_H); // read Accelerometer X_High value |
rkuo2000 | 0:95e46ae24046 | 53 | return ((HiByte<<8) | LoByte); |
rkuo2000 | 0:95e46ae24046 | 54 | } |
rkuo2000 | 0:95e46ae24046 | 55 | |
rkuo2000 | 0:95e46ae24046 | 56 | int16_t MPU6500::getGyroXvalue() |
rkuo2000 | 0:95e46ae24046 | 57 | { |
rkuo2000 | 0:95e46ae24046 | 58 | uint8_t LoByte, HiByte; |
rkuo2000 | 0:95e46ae24046 | 59 | LoByte = MPU6500_ReadByte(MPU6500_GYRO_XOUT_L); // read Accelerometer X_Low value |
rkuo2000 | 0:95e46ae24046 | 60 | HiByte = MPU6500_ReadByte(MPU6500_GYRO_XOUT_H); // read Accelerometer X_High value |
rkuo2000 | 0:95e46ae24046 | 61 | return ((HiByte<<8) | LoByte); |
rkuo2000 | 0:95e46ae24046 | 62 | } |
rkuo2000 | 0:95e46ae24046 | 63 | |
rkuo2000 | 0:95e46ae24046 | 64 | int16_t MPU6500::getGyroYvalue() |
rkuo2000 | 0:95e46ae24046 | 65 | { |
rkuo2000 | 0:95e46ae24046 | 66 | uint8_t LoByte, HiByte; |
rkuo2000 | 0:95e46ae24046 | 67 | LoByte = MPU6500_ReadByte(MPU6500_GYRO_YOUT_L); // read Accelerometer X_Low value |
rkuo2000 | 0:95e46ae24046 | 68 | HiByte = MPU6500_ReadByte(MPU6500_GYRO_YOUT_H); // read Accelerometer X_High value |
rkuo2000 | 0:95e46ae24046 | 69 | return ((HiByte<<8) | LoByte); |
rkuo2000 | 0:95e46ae24046 | 70 | } |
rkuo2000 | 0:95e46ae24046 | 71 | |
rkuo2000 | 0:95e46ae24046 | 72 | int16_t MPU6500::getGyroZvalue() |
rkuo2000 | 0:95e46ae24046 | 73 | { |
rkuo2000 | 0:95e46ae24046 | 74 | uint8_t LoByte, HiByte; |
rkuo2000 | 0:95e46ae24046 | 75 | LoByte = MPU6500_ReadByte(MPU6500_GYRO_ZOUT_L); // read Accelerometer X_Low value |
rkuo2000 | 0:95e46ae24046 | 76 | HiByte = MPU6500_ReadByte(MPU6500_GYRO_ZOUT_H); // read Accelerometer X_High value |
rkuo2000 | 0:95e46ae24046 | 77 | return ((HiByte<<8) | LoByte); |
rkuo2000 | 0:95e46ae24046 | 78 | } |